最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery kendo grid filter property is undefined - Stack Overflow

programmeradmin2浏览0评论

I have a jquery function that should filter the datasource based on the Service Type value of the button that was clicked.

I used this post on telerik to get my guidance:

Adding filters to Grid's source

From that, I created this buttonFilter() function:

function buttonFilter() {
    var buttonText = 'All';
    var button = $('#ReadTypeBtnGroup > button.btn.btn-default.active');
    if (button != null) {
        buttonText = button.val();
    }
    console.log('buttonFilter:: buttonText: ' + buttonText);
    var dataSource = $('#grid').data('kendoGrid').dataSource;
    if (dataSource.filter() != undefined) {
        dataSource.filter().length = 0; // remove any existing filters
    }
    if (buttonText != 'All') {
        dataSource.filter().push({ field: "serviceType", operator: 'eq', value: buttonText });
    }
    return buttonText;
}

The error I am getting is:

Uncaught TypeError: Cannot read property 'push' of undefined

The filter() property is supposed to be an array, but I am not great when it es to javascript or jquery.

What am I doing wrong?

I have a jquery function that should filter the datasource based on the Service Type value of the button that was clicked.

I used this post on telerik to get my guidance:

Adding filters to Grid's source

From that, I created this buttonFilter() function:

function buttonFilter() {
    var buttonText = 'All';
    var button = $('#ReadTypeBtnGroup > button.btn.btn-default.active');
    if (button != null) {
        buttonText = button.val();
    }
    console.log('buttonFilter:: buttonText: ' + buttonText);
    var dataSource = $('#grid').data('kendoGrid').dataSource;
    if (dataSource.filter() != undefined) {
        dataSource.filter().length = 0; // remove any existing filters
    }
    if (buttonText != 'All') {
        dataSource.filter().push({ field: "serviceType", operator: 'eq', value: buttonText });
    }
    return buttonText;
}

The error I am getting is:

Uncaught TypeError: Cannot read property 'push' of undefined

The filter() property is supposed to be an array, but I am not great when it es to javascript or jquery.

What am I doing wrong?

Share Improve this question asked Feb 2, 2021 at 17:20 user153923user153923
Add a ment  | 

3 Answers 3

Reset to default 3

You got it wrong. You can't change filter properties changing the result of filter() method. Instead, you have to use it passing parameters. That method only returns readonly values.

Example:

var filters = dataSource.filter(); // Getting current filters 
dataSource.filter(null); // Clearing filters
dataSource.filter({ field: "abc", value: "1" }); // Setting new filters

Always check the docs

@Dontvotemedown is largely correct, and that answer will work well for what you specifically want to do (i.e. clear filters pletely and apply your own). However, if you want to manually add a filter to existing filters, your original path was close to correct. I found this answer in my original search, then this when I couldn't add to an undefined filter. My full solution for adding a filter, either to an undefined filter set, or along with an existing one:

var grid = $("#ActivityGrid").data("kendoGrid");
var dataSource = grid.dataSource;
var gridFilter = dataSource.filter();
var upingFilter = {
    field: "ActivityDate",
    operator: "gte",
    value: new Date(),
    FilterName: "UpingOnly"
};
if ($("#UpingOnlyCheckbox")[0].checked) {
    if (gridFilter == undefined) {
        dataSource.filter(upingFilter);
    }
    else {
        gridFilter.filters.push(upingFilter);
        dataSource.filter(gridFilter);
    }

}

I had the same problem. If no filters exist, dataSource.filter() returns undefined which is a problem if you want to add filters. But using something like

dataSource.filter({ field: "abc", value: "1" });

results in a datasource read operation which was undesired in my case. Therefore I manipulated the datasource properties.

var grid = $("#grid").data("kendoGrid");
var dataSource = grid.dataSource;
var filterConfig = dataSource.filter();
if (typeof filterConfig == 'undefined') {     //no filters exist
    filterConfig = { filters: [], logic: "and" };
    grid.dataSource._filter = filterConfig;
}
发布评论

评论列表(0)

  1. 暂无评论