Custom Filter in kendo grid passing null values wh

2019-08-20 04:07发布

问题:

I am using kendo Grid Server side filtering.All columns are working fine,I have a columns named purchase date.I have used here custom filtering using Date Picker as shown below.But while filtering value of datePicker in not passing to server side..Please suggest

field: "purchase_date",
                              width: "120px",
                              title: "Purchase Date",
                              template: '#= kendo.toString(purchase_date, "MM-dd-yyyy") #',
                              filterable:
                              {
                                  ui: function (element) {
                                      element.kendoDatePicker(); // initialize a Kendo UI DateTimePicker
                                  },
                                  extra: false,
                                  operators: {
                                      date: {
                                          eq: "Equals to",
                                          lt: "Less than",
                                          gt: "Greater than"
                                      }
                                  }

                              }

回答1:

It seems that date fields are not always correctly send to the server in a understandable format.

For example a date filter is send to the server as : take=5&skip=0&page=1&pageSize=5&filter[logic]=and&filter[filters][0][field]=HireDate&filter[filters][0][operator]=eq&filter[filters][0][value]=Tue Nov 12 00:00:00 UTC+0100 2013

In order to solve this, I am using adding custom mapping code to change the value from all date fields to a format which is understood by the server. I use the parameterMap for that.

parameterMap: function (options) {
    if (options.filter) {
       KendoGrid_FixFilter(dataSource, options.filter);
    }
    return options;
}

The custom mapping javascript function KendoGrid_FixFilter is defined as:

// kendoDataSource = kendo.data.DataSource
// filter = kendo filter
function KendoGrid_FixFilter(kendoDataSource, kendoFilter, depth) {
    if ((!kendoDataSource) || (!kendoFilter)) return;

    if (!depth) depth = 0;
    // console.log(depth + " - FixDatesInFilter:" + JSON.stringify(kendoFilter));

    $.each(kendoFilter.filters, function (idx, filter) {
        //console.log("filter = " + idx + " = " + JSON.stringify(filter));

        if (filter.hasOwnProperty("filters")) {
            depth++;
            KendoGrid_FixFilter(kendoDataSource, filter, depth)
        }
        else {
            $.each(kendoDataSource.schema.model.fields, function (propertyName, propertyValue) {
                if (filter.field == propertyName && propertyValue.type == 'date') {
                    filter.value = kendo.toString(filter.value, _DefaultDateFormat); // "MM/dd/yyyy"
                    // console.log("changed = " + filter.value);
                }
            });
        }
    });
}

For a running demo see here.