Search multiple fields

4 posts, 1 answers
  1. Etienne
    Etienne avatar
    5 posts
    Member since:
    Mar 2016

    Posted 28 Apr 2016 Link to this post

    Hello,

    I have this code that can do a partial search on the first name or last name of any rows.

    http://jsfiddle.net/EaNm4/520/

    $(document).ready(function() {
      var data = [
        {firstName: 'Bob test', lastName: 'Kelso'},
        {firstName: 'abc test', lastName: 'def'},
        {firstName: 'toto test', lastName: 'tata'},
        {firstName: 'foo test', lastName: 'var'},
     
      ];
     
      $("#grid").kendoGrid({
        dataSource: {data: data},
        height: 350,
        columns: ['firstName', 'lastName']
      });
       
      var searchOperator = function(items, filterValue) {
        items = items.toLowerCase();
        var searchText = $('#search').val().toLowerCase().split(/[\s,]+/);
        if (searchText.length) {
            var found = true;
          for(var s in searchText) {
            if (items.indexOf(searchText[s]) == -1) {
              found = false;
              break;
            }
          }
          return found;
         
         
        }
        return true;
      };
       
       
       
      $('#search').on('change', function() {
        var dataSource = $("#grid").data('kendoGrid').dataSource;
        var searchText = $(this).val();
        dataSource.filter({
            logic:"or",
          filters: [{
            field: 'firstName',
            operator: searchOperator
          },{
            field: 'lastName',
            operator: searchOperator
          }]
        });
       
      });
       
    });

     

    If you type "bob", "bob test", "bo te", or "kel" in the search box, the first rows will stay visible.

    As long as you have a partial word, you will get the rows.

    But if you search for a word in both cols, like "bob kelso" the row will not be display.

    Is there a way to be able to search multiple fields at the same time, so "bob kelso" will display the first rows?

    I was thinking maybe of a way that when the datasource is loaded, it will create a new field with all the data of all other rows, so when I do a search like that, I would simply search that field... is there a way to do something like that... or maybe you have a better suggestion?

    Thank you

  2. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2466 posts

    Posted 02 May 2016 Link to this post

    Hi Etienne,

    It will not be possible to achieve the desired search by using filter value from both columns, but you could try to add additional ID field, which could be passed to the filter function and later be used for retrieving the corresponding to that ID dataItem. Once you have the dataItem you can determine whether or not any of the words in the search box exists in it and if so, include it in the result.

    Hope this helps.


    Regards,
    Konstantin Dikov
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  3. Etienne
    Etienne avatar
    5 posts
    Member since:
    Mar 2016

    Posted 02 May 2016 in reply to Konstantin Dikov Link to this post

    Hello,

    Could you provide an exemple of a search field with an id, I didn't see anything about this in the documentation.

    Thank you

  4. Answer
    Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2466 posts

    Posted 02 May 2016 Link to this post

    Hello Etienne,

    I have tested a solution without specifying any data field in the filter and it passes the data item, which could be used for applying the required functionality. Please take a look at the following modification of your code:

    Kind Regards,
    Konstantin Dikov
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
Back to Top