SilverStripe。在ModelAdmin中按日期范围搜索 [英] SilverStripe. Search by date-range in ModelAdmin

查看:103
本文介绍了SilverStripe。在ModelAdmin中按日期范围搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的DataObject中具有日期属性。

I have date-property in my DataObject.

如何在ModelAdmin中按日期范围进行搜索?

How can I search by date-range in ModelAdmin?

例如:搜索日期大于2007-13-01且小于2007-17-01的所有项目

或搜索日期在2007-13-01和2007-17-01之间的所有项目

For example: "search all items where date is more than 2007-13-01 and less than 2007-17-01"
or "search all items where date is between 2007-13-01 and 2007-17-01"

目前,我只能使用GreaterTranFilter或LessThanFilter进行搜索,而不能同时使用两者进行搜索。

For now I can search only with GreaterTranFilter or with LessThanFilter, but not with both.

class MyObject extends DataObject {
    private static $db = [
        "Date" => "Date",
    ];
    private static $summary_fields = [
        "Date" => "Date",
    ];

    private static $searchable_fields = [
        "Date" => [
            "field" => "DateField",
            "filter" => "GreaterThanFilter",
            "title" => 'Date from ...'
        ],
    ];
}

另外,搜索字段必须使用日历(日期选择器)

Additionally search field must use a calendar(datepicker)

DateField:
  default_config:
    showcalendar: true

能否举一个示例,说明如何按日期范围进行搜索?

Can you give an example how to search by date-range?

推荐答案

有一个 WithinRangeFilter ,但这不会帮助您如果仅使用配置,则距离很远。

There is a WithinRangeFilter, but it's not going to get you very far if you're using configuration only. This is something you really need to implement procedurally.

通过重载 getSearchContext(),然后重载来添加范围过滤器 getList()并检查 q 请求参数中的日期范围,然后将其应用于列表。

Add the range filters by overloading getSearchContext(), then overload getList() and check the q request param for the date ranges, and apply them to the list.

public function getSearchContext()
{
    $context = parent::getSearchContext();
    $context->getFields()->push(DateField::create('q[Start]','Start'));
    $context->getFields()->push(DateField::create('q[End]','End'));

    return $context;
}

public function getList()
{
    $list = parent::getList();
    $params = $this->getRequest()->requestVar('q');

    $filters = [];
    if(isset($params['Start'])) {
        $filters['Date:LessThanOrEqual'] = $params['Start'];
    }
    if(isset($params['End'])) {
        $filters['Date:GreaterThanOrEqual'] = $params['End'];
    }

    return $list->filter($filters);
}

这篇关于SilverStripe。在ModelAdmin中按日期范围搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆