jQuery UI Widgets Forums Grid Filter on two columns

This topic contains 3 replies, has 4 voices, and was last updated by  TP_DTNA 7 years, 10 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
  • Filter on two columns #76051

    apj
    Participant

    How to apply filter simultaneously on two columns?

    In the below example I tried to filter two columns containing Andrew.

    I wanted to filter the row where First Name or Last Name contains ‘Andrew’ .

    But when I executed the below code it filters the row which contains both First Name and Last Name as ‘Andrew’.

    Could you please suggest how to do this?

    $(“#ClientSearch”).jqxGrid(‘clearfilters’);
    var filtergroup = new $.jqx.filter();
    var filter_or_operator = 1;
    var filtervalue = ‘Andrew’;
    var filtercondition = ‘contains’;
    var filter1 = filtergroup.createfilter(‘stringfilter’, filtervalue, filtercondition);
    filtergroup.addfilter(filter_or_operator, filter1);
    // add the filters.
    $(“#ClientSearch”).jqxGrid(‘addfilter’, ‘FirstName’, filtergroup);
    var filtergroup2 = new $.jqx.filter();
    filtervalue = ‘Andrew’;
    filtercondition = ‘contains’;
    var filter2 = filtergroup.createfilter(‘stringfilter’, filtervalue, filtercondition);
    filtergroup2.addfilter(filter_or_operator, filter2);
    $(“#ClientSearch”).jqxGrid(‘addfilter’, ‘LastName’, filtergroup2);
    // apply the filters.
    $(“#ClientSearch”).jqxGrid(‘applyfilters’);

    Filter on two columns #76054

    Peter Stoev
    Keymaster

    Hi apj,

    The Grid’s filtering displays data rows which meet all filtering conditions simultaneously as shown in the samples. You’re trying to achieve something which is not in the scope of jqxGrid’s Filtering.

    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com/

    Filter on two columns #76081

    kamelot
    Participant

    Hi apj,

    This is what jqxGrid’s filtering does. By default, it’ll be ‘AND’ operator between filter groups, that’s why yours has Firstname Andrew AND Lastname Andrew.

    You should do an ‘OR’ operator between your filtergroup and filtergroup2 to get Firstname as Andrew ‘OR’ Lastname as Andrew, like from this example:
    https://www.jqwidgets.com/jquery-widgets-documentation/documentation/jqxgrid/jquery-grid-filtering.htm

    Read the “Add filters to multiple columns through the API.” the last example code in the bottom, I used it for my Search box contain a string to filter all my grid columns.
    For example you have to include: filtergroup.operator = ‘or’; after u create your filtergroup and filtergroup2.operator = ‘or’ also after u create it.

    Hope this helps, or at least what you’re looking for 🙂

    Filter on two columns #91542

    TP_DTNA
    Participant

    Like kamelot said, after creating a filtergroup, you would assign the string ‘or’ to its “operator” property.

    var filterGroup = new $.jqx.filter();
    filterGroup.operator = ‘or’; // column-level OR, not value-level
    var filter = filterGroup.createfilter(filtertype, filtervalue, filtercondition);

    If your filtering happens client-side, this will work as you desire. If you want it to work on the server side, be careful about using the code provided in the docs/examples as it disregards this information and will always AND the columns together. If you inspect the filter data being sent, you’ll notice that this value is passed as the name of the datafield concatenated with “operator”. Simply tie that into some control flow when constructing the sql query to choose between AND and OR in your WHERE clause between columns.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.