[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>
|