We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

search on multiple words

Dear,


How would I do the same (query 2 in particular) in the following grid? It seems that the used function ej.predicate() is not available?

<ejs-grid id="Grid" dataSource="ViewBag.datasource" toolbar="@(new List<string>() {"Search" })" allowPaging="true"> <e-grid-pagesettings pageCount="5"></e-grid-pagesettings> <e-grid-columns> <e-grid-column field="CategoryName" headerText="Category Name" textAlign="Right" width="160"></e-grid-column> <e-grid-column field="ProductName" headerText="Product Name" width="170"></e-grid-column> <e-grid-column field="QuantityPerUnit" headerText="Quantity Per Unit" textAlign="Right" width="170"></e-grid-column> <e-grid-column field="UnitsInStock" headerText="Units in Stock" textAlign="Right" width="170"></e-grid-column> <e-grid-column field="Discontinued" headerText="Discontinued" width="150" displayAsCheckBox="true" textAlign="Center" type="boolean"></e-grid-column> </e-grid-columns> </ejs-grid>


1 Reply

TS Thavasianand Sankaranarayanan Syncfusion Team September 5, 2019 07:09 AM UTC

Hi Koen, 
 
Greetings from Syncfusion support. 
 
We can search the text value based on typing the values in search textbox instead of click on search button using created event and we have bind actionBegin event and prevent the default search action and customize the filter query based on the multiple keywords(search). Please refer the below code example and sample for more information.  
 
[index.cshtml]  
<ejs-grid id="Grid" dataSource="ViewBag.datasource" allowPaging="true" toolbar="@(new List<string>() {"Search" })"created="created" actionBegin="actionBegin" actionComplete="actionComplete">      
    <e-grid-columns>          
        .    .     .    .          
    </e-grid-columns>  
</ejs-grid>  
<script>  
    var debounceTimer = null;  
    var gquery, val;  
    function created(e) {          
        document.getElementById("Grid_searchbar").addEventListener('keyup', (event) => {  
            clearTimeout(debounceTimer); // you can customize as per your requirement(Searh action trigger while KEYPRESS)  
            debounceTimer = setTimeout(() => { searchFun(event); }, 500);  
        })   
    }  
  
    function actionBegin(args) {          
        if (args.requestType == "searching") {  
            var grid = document.getElementsByClassName("e-grid")[0].ej2_instances[0];  
            const keys = args.searchString.split(','); //Split your search text and get the values  
            var flag = true;  
            var predicate;  
            if (keys.length > 1) {  
                if (this.searchSettings.key !== '') {  
                    val = args.searchString;  
                    // preparing filter query  
                    keys.forEach((key) => {  
                        this.getColumns().forEach(col => {  
                            if (flag) {  
                                predicate = new ej.data.Predicate(col.field, 'contains', key);  
                                flag = false;  
                            } else {  
                                var pre = new ej.data.Predicate(col.field, "contains", key);  
                                predicate = predicate.or(pre);                                                            }  
                        });  
                    });  
                    gquery = this.query;  
                    this.query = new ej.data.Query().where(predicate);                      
                    this.searchSettings.key = '';  
                    this.refresh();  
                }  
            }  
        }  
    }  
  
    function actionComplete(e) {  
        if (e.requestType === 'refresh') {  
            this.query = gquery;  
            document.getElementById(this.element.id + '_searchbar').value = val;  
        }  
    }  
    function searchFun(event) {  
        var grid = document.getElementsByClassName("e-grid")[0].ej2_instances[0];  
        var value = event.target.value;  
        grid.search(value);  
        clearTimeout(debounceTimer);  
    }  
</script>  
 
 
 
Note: We need to the predicate as ej.data.Perdicate() to use in EJ 2 Grid component.  
 
Refer the help documentation. 
 


 
Regards, 
Thavasianand S. 


Loader.
Live Chat Icon For mobile
Up arrow icon