jQWidgets Forums

jQuery UI Widgets Forums Grid Overriding the filter's evalutate method

This topic contains 2 replies, has 2 voices, and was last updated by  mcseemcher 8 years, 10 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author

  • mcseemcher
    Participant

    Hello.

    I would like to customize row filter to make it split given string into array of strings and evaluate each one of it on the row.

    So I found this (http://www.jqwidgets.com/custom-data-filtering-with-jquery-grid-plug-in/) nice manual, which seems to execute some “Evaluate” method on a given row. But how do I manipulate the given string? Is there a way to get filter’s value?

    Or maybe I’m doing this all wrong. Another crazy solution that come to mind is to split given string and create a group of filter, consisting of filter each working with piece of given string.

    Obviously, I overcomplicate this. Can you help me?


    Christopher
    Participant

    Hi mcseemcher,

    yes, there’s a way to get the value of the filter field. This is how to do it:

    var source =
    {
    localdata: data,
    datatype: “array”,
    filter: function (filters, data, length) {
    var filtereddata = new Array();
    var filterslength = filters.length;
    for (row = 0; row < length; row++) {
    var datarow = data[row];
    var filterresult = undefined;
    // loop through the filters and evaluate the value.
    for (j = 0; j < filterslength; j++) {
    var appliedFilterValue = filters[j].filter.getfilters()[0].value;
    var filter = filters[j].filter;
    var value = datarow[filters[j].datafield];
    var result = filter.evaluate(value);
    if (filterresult == undefined) filterresult = result;
    else {
    if (filter.operator == ‘or’) {
    filterresult = filterresult || result;
    }
    else {
    filterresult = filterresult && result;
    }
    }
    }
    if (filterresult) {
    filtereddata[filtereddata.length] = datarow;
    }
    }
    return filtereddata;
    }
    };

    the “appliedFilterValue” contains the value of the first filter input field. In order to accomplish what you want, you need to split this string by some criteria, for example: appliedFilterValue.split(‘ ‘);. After that you need to create new filters with each the new values you just got from splitting the initial value. Then you need to call “evaluate” on each of the new Filters you’ve created to check if any row’s value matches the requirement.

    Best Regards,
    Christopher


    mcseemcher
    Participant

    Christopher,

    thank you, I did exactly as you said.

    Here’s final code, for anyone who’s looking:

    filter: function (filters, data, length) {
    				        var filtereddata = new Array();
    				        var filterslength = filters.length;
    				        for (row = 0; row < length; row++) {
    				            var datarow = data[row];
    				            var filterresult = undefined;
    				            // loop through the filters and evaluate the value.
    				            for (j = 0; j < filterslength; j++) {
    				                var filter = filters[j].filter;
    				                var value = datarow[filters[j].datafield];
    				                var appliedFilterValue = filters[j].filter.getfilters()[0].value;
    				                if (appliedFilterValue[appliedFilterValue.length-1] === ",") appliedFilterValue = appliedFilterValue.slice[0,-1]
    				                var valarray = appliedFilterValue.split(',');
    				                if (valarray.length > 1){
    				                	var SplittedFilterGroup = new $.jqx.filter();
    
    				                	for (k = 0; k < valarray.length; k++) {
    										var YetAnotherFilter = SplittedFilterGroup.createfilter('stringfilter', valarray[k], 'contains');
    										var result = YetAnotherFilter.evaluate(value);
    										if (filterresult == undefined) filterresult = result;
    						                else {
    						                        filterresult = filterresult || result;
    						                	}
    						                if (filterresult == true) break;	
    				                	}
    				                }
    				                else {						                
    					                var result = filter.evaluate(value);
    					                if (filterresult == undefined) filterresult = result;
    					                else {
    					                    if (filter.operator == 'or') {
    					                        filterresult = filterresult || result;
    					                    }
    					                    else {
    					                        filterresult = filterresult && result;
    					                    }
    					                }
    						        }        
    				            }
    				            if (filterresult) {
    				                filtereddata[filtereddata.length] = datarow;
    				            }
    				        }
    				        return filtereddata;
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.