In our application we want the filter on a date column to prompt the user for a start date and an end date, with the filter returning rows where the field in question falls between (or on) those two dates.

在我们的应用程序中,我们希望日期列上的过滤器提示用户输入开始日期和结束日期,过滤器返回有关字段位于这两个日期之间(或之上)的行。

Initial Approach

初步方法

Our initial approach was to restrict date types to use gte and lte operators, and add the "extra : true" filterable option on the column. This came close, but presented the following problems: A) Each date input could use either the gte (Start) or lte (End) operator, providing undesired flexibility and the option for the user to create a filter that would never return results, and B) Presented a logical comparison (And / Or) that we don't want.

我们最初的方法是限制日期类型以使用gte和lte运算符,并在列上添加“extra:true”可过滤选项。这很接近,但是出现了以下问题:A)每个日期输入可以使用gte(Start)或lte(End)运算符,提供不期望的灵活性以及用户创建永远不会返回结果的过滤器的选项,以及B)提出我们不想要的逻辑比较(和/或)。

Better Approach

更好的方法

This question has an answer by Matthew Erwin that gets us very close: it allows us to completely re-style the filter entirely, so we can present simply a Start Date input and an End date input. However, what I can't get working is associating the right filter operation with the right input (gte for the Start date, lte for the End date). My custom filter is as follows:

这个问题得到了Matthew Erwin的答案,它让我们非常接近:它允许我们完全重新设置过滤器的样式,因此我们可以简单地提供一个开始日期输入和一个结束日期输入。但是,我无法正常工作的是将正确的过滤操作与正确的输入相关联(开始日期为gte,结束日期为lte)。我的自定义过滤器如下:

    $scope.dateFilter = {
    extra: true,
    operators: {},
    ui: function (element) {
        var parent = element.parent();
        while (parent.children().length > 1)
            $(parent.children()[0]).remove();

        parent.prepend(
            "Start Date:<br/><span class=\"k-widget k-datepicker k-header\">" +
            "<span class=\"k-picker-wrap k-state-default\">" +
            "<input data-bind=\"value: filters[0].value\" class=\"k-input\" type=\"text\" data-role=\"datepicker\"" +
            " style=\"width: 100%\" role=\"textbox\" aria-haspopup=\"true\" aria-expanded=\"false\" aria-disabled=\"false\" " +
            " aria-readonly=\"false\" aria-label=\"Choose a date\">" +
            "<span unselectable=\"on\" class=\"k-select\" role=\"button\">" +
            "<span unselectable=\"on\" class=\"k-icon k-i-calendar\">select</span></span></span></span>" +

            "<br/>End Date:<br/>" +
            "<span class=\"k-widget k-datepicker k-header\"><span class=\"k-picker-wrap k-state-default\">" +
            "<input data-bind=\"value: filters[1].value\" class=\"k-input\" type=\"text\" data-role=\"datepicker\"" +
            " style=\"width: 100%\" role=\"textbox\" aria-haspopup=\"true\" aria-expanded=\"false\" " +
            " aria-disabled=\"false\" aria-readonly=\"false\" aria-label=\"Choose a date\">" +
            "<span unselectable=\"on\" class=\"k-select\" role=\"button\">" +
            "<span unselectable=\"on\" class=\"k-icon k-i-calendar\">select</span></span></span></span>"
        );
    }
};

With this approach, the Odata filter option is generated for each of the dates, however it uses the eq Equal To operator, so no values are ever returned. We aren't building filters specifically on the data source.

使用此方法,将为每个日期生成Odata过滤器选项,但它使用eq Equal To运算符,因此不会返回任何值。我们没有专门针对数据源构建过滤器。

Is there a simple way I can associate each of those date inputs with a specific filter operator? Is there a better way to approach this subject? It seems like filtering dates based on a Start - End range would be commonly desired.

有没有一种简单的方法可以将每个日期输入与特定的过滤器运算符相关联?有没有更好的方法来解决这个问题?似乎通常需要基于开始 - 结束范围过滤日期。

Other Details

其他详情

We are using AngularJS, and WebAPI with Odata.

我们正在使用AngularJS,以及使用Odata的WebAPI。

2 个解决方案

#1


12

After working with Telerik, I came to an answer. The thread that I opened can be found here, but I'll also summarize in this answer.

在与Telerik合作之后,我找到了答案。我打开的帖子可以在这里找到,但我也会在这个答案中总结一下。

The ultimate solution was to:

最终的解决方案是:

  • Use the "Messages" option of the column "filterable" option to customize the filter display message.
  • 使用“filterable”列中的“Messages”选项可自定义过滤器显示消息。
  • Use the "Extra" option of the column "filterable" option to get the extra Date selector in the filter menu.
  • 使用“filterable”列选项的“Extra”选项可以在过滤器菜单中获取额外的Date选择器。
  • Configure the "Operators" option in the grid filterable option to set what operators can be used for dates (gte, lte) and what text is displayed for each (Begin Date, End Date).
  • 在网格可过滤选项中配置“运算符”选项,以设置可用于日期(gte,lte)的运算符以及为每个运算符显示的文本(开始日期,结束日期)。
  • Use the filterMenuInit event to configure the filter controls.
  • 使用filterMenuInit事件配置筛选器控件。

End Result

最终结果

Column Filterable

列可过滤

The following filterable options were used:

使用了以下可过滤选项:

filterable: { "extra": "true", "messages": { "info": "Show items between dates:" }}

Extra gives us the second date selector, and the "info" message customizes the text displayed at the top of the filter menu.

Extra为我们提供了第二个日期选择器,“info”消息自定义了过滤器菜单顶部显示的文本。

Grid Filterable

网格可过滤

I used the "operators" option in the grid-level "filterable" option to make date filters only provide the gte and lte operators, and to customize the text for those operators. This is what the operators configuration object wound up looking like:

我在网格级“可过滤”选项中使用“运算符”选项,使日期过滤器仅提供gte和lte运算符,并自定义这些运算符的文本。这是运算符配置对象看起来像:

"date": {
                "gte": "Begin Date",
                "lte": "End Date"
            }

Because we want this to apply for all dates, we put that in a factory and reuse it in each angular controller / view.

因为我们希望这适用于所有日期,所以我们将它放在工厂中并在每个角度控制器/视图中重复使用它。

filterMenuInit Event

filterMenuInit事件

By providing a handler for the filterMenuInit event, you can access and configure the individual controls in the filter menu as it is created. The handler function that I created looks like this:

通过为filterMenuInit事件提供处理程序,您可以在创建过滤器菜单时访问和配置其中的各个控件。我创建的处理函数如下所示:

function (e) {
            if (e.sender.dataSource.options.schema.model.fields[e.field].type == "date") {
                var beginOperator = e.container.find("[data-role=dropdownlist]:eq(0)").data("kendoDropDownList");
                beginOperator.value("gte");
                beginOperator.trigger("change");
                beginOperator.readonly();

                var logicOperator = e.container.find("[data-role=dropdownlist]:eq(1)").data("kendoDropDownList");
                logicOperator.readonly();

                var endOperator = e.container.find("[data-role=dropdownlist]:eq(2)").data("kendoDropDownList");
                endOperator.value("lte");
                endOperator.trigger("change");
                beginOperator.readonly();
            }

Specifically, for any date field, this function sets the first and last dropdown operators to "gte" and "lte" respectfully (Those are the dropdowns for the first date operator and the second date operator), and sets all of the dropdowns to read-only so the user can't change them (the only other dropdown, which is at index 1, is the logical comparison - only And makes sense, so we don't let users change it.)

具体来说,对于任何日期字段,此函数将第一个和最后一个下拉运算符设置为“gte”和“lte”(这些是第一个日期运算符和第二个日期运算符的下拉列表),并将所有下拉列表设置为读取 - 因此用户无法更改它们(唯一的另一个下拉列表,即索引1,是逻辑比较 - 仅有意义,因此我们不允许用户更改它。)

This function applies this configuration for any fields that are of "date" type. I did it this way so that I could create this function once, put it in an Angular factory, and then reuse it for any grid that I needed. If you don't want to apply this as a blanket configuration across all of your date columns, you can change the conditional to check for fields by name. Example:

此函数将此配置应用于“日期”类型的任何字段。我是这样做的,这样我就可以创建一次这个函数,把它放在一个Angular工厂中,然后将它重用于我需要的任何网格。如果您不想在所有日期列中将其应用为总括性配置,则可以更改条件以按名称检查字段。例:

if (e.field == "fieldName")

Hopefully this will be helpful to someone else. This doesn't give you ultimate customization of the UI in the filter menu, but it does let you simply set up a filter between two dates. I'm sure someone clever could merge this with my original strategy (replacing the markup for the filter menu entirely) to come up with something completely customized.

希望这对其他人有帮助。这并不能让您在过滤器菜单中对UI进行最终自定义,但它确实让您只需在两个日期之间设置过滤器。我确信聪明的人可以将其与我的原始策略(完全替换过滤器菜单的标记)合并,以提出完全自定义的内容。

更多相关文章

  1. HTML显示日期时间代码 - [js 特效代码]
  2. phpcms日期--汉字与数字的转换
  3. PHP日期添加1年到当前日期。
  4. 将STR_TO_DATE格式化为日期
  5. 更新日期字段时为空 - MySQL PHP
  6. 使用date_default_timezone_set和日期的可能的PHP bug ?
  7. 关于PHP 读取EXCEL时间(不是日期)的问题
  8. Laravel 5验证日期为php Y格式。g 2015 ?
  9. 将纪元时间转换为日期PHP

随机推荐

  1. Unity Android使用相机拍摄照片并在其上
  2. Android开发如何验证输入的手机号码是否
  3. opencv实时视频帧在android中没有显示sob
  4. 通过数据库接口获取到的中文数据是问号怎
  5. 尽管在清单文件中指定了权限,但是ACCESS_F
  6. Android快速开发框架andbase
  7. Android开发一些实用的类、方法及接口(新
  8. Android 监听各个Acitivity的生命周期
  9. viewpager 分页请求数据库并展示
  10. 现在做Android开发比较好?还是Android测试