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

Search multiple fields

3 Answers 741 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Etienne
Top achievements
Rank 1
Etienne asked on 28 Apr 2016, 01:29 PM

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

3 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 02 May 2016, 08:47 AM
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!
 
0
Etienne
Top achievements
Rank 1
answered on 02 May 2016, 01:28 PM

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

0
Accepted
Konstantin Dikov
Telerik team
answered on 02 May 2016, 02:27 PM
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!
 
Tags
Grid
Asked by
Etienne
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Etienne
Top achievements
Rank 1
Share this question
or