This is a migrated thread and some comments may be shown as answers.

[Solved] Filtering MVC Grid using OData - Contains

3 Answers 88 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jim
Top achievements
Rank 1
Jim asked on 02 Apr 2012, 10:04 PM
I have MVC Grid where I am using OData. Everything is working fine except when I try to filter using "Contains" at which point nothing is returned. All the other filter types work fine. If it makes any difference I am using JQuery and JSON.

Jim

3 Answers, 1 is accepted

Sort by
0
Dadv
Top achievements
Rank 1
answered on 04 Apr 2012, 02:41 PM
Hi,
Can you provide some code? or a sample?
0
Jim
Top achievements
Rank 1
answered on 05 Apr 2012, 08:21 PM
I think I have the problem figured out. In the telerik.grid.odata.js the following function is used to filter AJax calls using OData.

function filter(grid) {
        var filteredColumns = $.grep(grid.columns, function (c) { return c.filters; });
        return $.map(filteredColumns, function (c) {
            var member = c.member.replace(/\./g, '/');
 
            return '(' + $.map(c.filters, function (filter) {
                var operator = filter.operator;
 
                var value = encodeFilterValue(c, filter.value);
 
                debugger;
                if (/substringof|startswith|endswith/.test(operator))
                    return operator + '(' + member + ', ' + value + ')';
                else
                    return member + ' ' + operator + ' ' + value;
            }).join(' and ') + ')';
 
        }).join(' and ');
    }


If you look at the 'if' statement a test is being made to determine the type of filter operator that is being used. For example, the 'startswith' operator is used to see if the item Starts With a given 'value'. The 'member' is the column in the Grid. For 'startswith' and 'endswith' the signature of the OData call is 'member' and then 'value'. The problem is with 'substringof' (which is used to test the item to see if it Contains the 'value') the signature is 'value' and then 'member'. The following is what I used to correct the problem.

function filter(grid) {
    var filteredColumns = $.grep(grid.columns, function (c) { return c.filters; });
    return $.map(filteredColumns, function (c) {
        var member = c.member.replace(/\./g, '/');
 
        return '(' + $.map(c.filters, function (filter) {
            var operator = filter.operator;
 
            var value = encodeFilterValue(c, filter.value);
 
            if (/startswith|endswith/.test(operator))
                return operator + '(' + member + ', ' + value + ')';
            else if (/substringof/.test(operator))
                return operator + '(' + value + ', ' + member + ')';
            else
                return member + ' ' + operator + ' ' + value;
        }).join(' and ') + ')';
 
    }).join(' and ');
}

Jim

0
Dadv
Top achievements
Rank 1
answered on 06 Apr 2012, 08:11 AM
I suggest you to send a PITS request with this code
Tags
Grid
Asked by
Jim
Top achievements
Rank 1
Answers by
Dadv
Top achievements
Rank 1
Jim
Top achievements
Rank 1
Share this question
or