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

Search date

Hi,

I need to search for a date, via the grid's built-in lookup feature, on a date column.

How should I proceed?

I've seen that normal search doesn't work as it should for this type of column.


5 Replies

AG Ajith Govarthan Syncfusion Team August 10, 2021 03:29 PM UTC

Hi Pio Luca, 

Thanks for contacting Syncfusion support. 

Query: I need to search for a date, via the grid's built-in lookup feature, on a date column.How should I proceed? I've seen that normal search doesn't work as it should for this type of column. 

We have checked your query and found that you want to perform the searching operation for the date columns in your grid application. By default,  our EJ2 Grid searches the records of dataSource by comparing the given search key with each record’s original value. Here, original value refers to the primitive type (date or number) of the properties. 

So, search action cannot sort out results based on the column format for date columns. However, we can provide an alternate way to search the Grid with custom logic over the toolbar element’s event. We have prepared sample in that we have used a custom search functionality for date and other columns. For your convenience we have attached the sample, please refer them for your reference. 

Code Example: 
Index.cshtml 

<div class="control-section"> 
    @{ 
        List<object> template = new List<object>(); 
        template.Add(new { template = "#toolbarSearch" }); 
    } 
 
    @Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.datasource).Toolbar(template).Load("onLoad").Columns(col => 
    { 
 
    col.Field("OrderID").HeaderText("Order ID").Width("120").IsPrimaryKey(true).Filter(new { type = "CheckBox" }).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add(); 
    col.Field("CustomerID").HeaderText("Customer Name").Width("170").Add(); 
    col.Field("OrderDate").HeaderText("Order Date").Width("130").Type("date").EditType("datepickeredit").Format("dd/MM/yyyy").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add(); 
    col.Field("Freight").HeaderText("Freight").Width("120").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add(); 
    col.Field("ShipCountry").EditType("dropdownedit").HeaderText("Ship Country").Width("150").Add(); 
 
    }).Height("400").AllowPaging().AllowFiltering().FilterSettings(filter => filter.Type(Syncfusion.EJ2.Grids.FilterType.Menu)).EditSettings(edit => { edit.AllowEditing(true).AllowAdding(true).AllowDeleting(true); }).Render() 
</div> 
 
<div id="toolbarSearch"> 
    <div class="e-input-group"> 
        <input class="e-input" name='input' id="toolSearch" type="text" placeholder="Search" /> 
        <span class="e-input-group-icon e-search-icon e-icons"></span> 
    </div> 
</div> 
<script> 
    function onLoad() { 
 
 
        // Keyup event bound to the input element 
        var grid = document.getElementById("Grid").ej2_instances[0]; 
        var gridInitialData = grid.dataSource.slice(0); 
        grid.element.addEventListener('keyup', (args) => { 
            if ((args.target).getAttribute('id') === 'toolSearch' && args.key === "Enter") { 
                let gridObj = grid; 
                // Regex for checking date of format – “dd/MM/yyyy” 
                var regex = /^([0-2][0-9]|(3)[0-1])(\/)(((0)[0-9])|((1)[0-2]))(\/)\d{4}$/; 
                // Test regex with the entered value 
                let input = (args.target); 
                if (regex.test(input.value)) { 
                    var count = 0; 
                    var predicates; 
                    // Value is split to form JavaScript’s Date object 
                    var dateSplit = input.value.split("/"); 
                    var dateVal = new Date(parseInt(dateSplit[2]), (parseInt(dateSplit[1]) - 1), parseInt(dateSplit[0])); 
                    while (count < gridObj.columns.length) { 
                        // Predicate is generated for all columns with type as ‘date’ 
                        let col = gridObj.columns[count]; 
                        if (col.type === "date") { 
                            // Predicates are generated with the column field name and date object value 
                            predicates = (predicates === undefined) ? new ej.data.Predicate(col.field, "contains", dateVal) : predicates.or(col.field, "contains", dateVal); 
                        } 
                        count++; 
                    } 
                    // Data manager is executed with grid’s datasource and query generated based on the predicates 
                    new ej.data.DataManager({ json: (gridObj.dataSource) }).executeQuery(new ej.data.Query().where(predicates)).then((e) => { 
                       // The returned result is assigned to the grid datasource 
                        gridObj.dataSource = e.result; 
                    }); 
                } else if (input.value === "") { 
                    if (gridObj.searchSettings.key === "") { 
                        var data = gridInitialData; 
                        // If the input value and search key is empty the entire data source is assigned to the grid 
                        gridObj.dataSource = ej.data.DataUtil.parse.parseJson(data); 
                    } else { 
                        // If the input value is empty but the grid contains a search key, then another search is performed with empty value to remove the search 
                        gridObj.search(""); 
                    } 
                } else { 
                    // Grid search method is called with the input value 
                    gridObj.search(input.value); 
                } 
            } 
        }); 
    } 
</script> 
 



Please get back to us if you need further assistance. 

Regards, 
Ajith G. 



PL Pio Luca Valvona August 10, 2021 05:26 PM UTC

Hi Ajith,

thank you for support.

Perfect! It works.

But now I have two related question:


    1. How should I go about having the same style as the built-in search?

    built-in:

    gridSearch.png

    custom:

    customSearch.png

    2. If I want add built-in print functionality in toolbar, how should I do?




AG Ajith Govarthan Syncfusion Team August 12, 2021 03:39 AM UTC

Hi Pio Luca, 

Thanks for the update. 

Query: thank you for support.Perfect! It works. 
 
We are happy to hear that the provide solution works fine at our end. 

Query: How should I go about having the same style as the built-in search? 
 
Based on your query you are facing style difference behavior with custom and built-in search box. So, before we proceed to your query, please share the below details to provide you the prompt solution at the earliest. 

  1. Please share the exact searchbar difference that you are facing in your Grid application.

  1. Share the video demonstration of the reported behavior.
 
  1. Do you want to add focus effects like built-in  search bar?
 
Query: If I want add built-in print functionality in toolbar, how should I do? 
 
Based on your query you want to add print toolbar button along with custom search bar in your Grid application. So, we have prepared sample in that we have added the elements for print toolbar item along with custom search bar and we have printed the grid with help of toolbarClick event. 

In the toolbarClick event the grid component data is printed when the print button is clicked. For your convenience we have attached the sample, please refer them for your reference. 

Code Example: 
Index.cshtml 

<div class="control-section"> 
    @{ 
        List<object> template = new List<object>(); 
        template.Add(new { template = "#toolbarSearch" }); 
    } 
 
    @Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.datasource).Toolbar(template).ToolbarClick("toolbarClick").Load("onLoad").Columns(col => 
    { 
 
    col.Field("OrderID").HeaderText("Order ID").Width("120").IsPrimaryKey(true).Filter(new { type = "CheckBox" }).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add(); 
    col.Field("CustomerID").HeaderText("Customer Name").Width("170").Add(); 
    col.Field("OrderDate").HeaderText("Order Date").Width("130").Type("date").EditType("datepickeredit").Format("dd/MM/yyyy").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add(); 
    col.Field("Freight").HeaderText("Freight").Width("120").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add(); 
    col.Field("ShipCountry").EditType("dropdownedit").HeaderText("Ship Country").Width("150").Add(); 
 
    }).Height("400").AllowPaging().AllowFiltering().FilterSettings(filter => filter.Type(Syncfusion.EJ2.Grids.FilterType.Menu)).EditSettings(edit => { edit.AllowEditing(true).AllowAdding(true).AllowDeleting(true); }).Render() 
</div> 
 
<div id="toolbarSearch"> 
    <div class="e-toolbar-item e-toolbar-print"  title="Print"><button class="e-tbar-btn e-tbtn-txt e-control e-btn e-lib" type="button" id="Grid_print" tabindex="-1" aria-label="Print" style="width: auto;"><span class="e-btn-icon e-print e-icons e-icon-left"></span><span class="e-tbar-btn-text">Print</span></button></div> 
    <div class="e-input-group"> 
        <input class="e-input" name='input' id="toolSearch" type="text" placeholder="Search" /> 
        <span class="e-input-group-icon e-search-icon e-icons"></span> 
    </div> 
 
</div> 
<script> 
    function toolbarClick(args) { 
 
        var element = ej.grids.parentsUntil(args.originalEvent.target, "e-toolbar-print", false); 
 
        if (element) { 
            var grid = document.getElementById("Grid").ej2_instances[0]; 
            grid.print(); 
        } 
 
    } 
</script> 
 



Please get back to us if you need further assistance. 

Regards, 
Ajith G. 



PL Pio Luca Valvona replied to Ajith Govarthan August 12, 2021 09:11 AM UTC

Hi Ajith,

thank you.

Query: How should I go about having the same style as the built-in search? 

The main differences are the size of the textbox and button and the color of the built-in feature is lighter. I take this opportunity to ask you if the same functionality of the date search, I can also have it by clicking the button with the magnifying glass icon and not only if I press Enter.



AG Ajith Govarthan Syncfusion Team August 13, 2021 04:47 PM UTC

Hi Pio Luca, 

Thanks for update. 

Query: The main differences are the size of the textbox and button and the color of the built-in feature is lighter.  
 
Based on your query you want to change the size and color of the searchbar and button element.  So, we have prepared sample in that we have overridden the search bar element’s width using CSS properties. For color issue we have checked our default buttons and its icons which is having same color properties and there is no color difference in our sample. So, for your convenience we have attached the screenshot please refer them for your reference. 

Code Example: 
Index.cshtml 

<style> 
    .c-input-div{ 
        width:auto;  // changed the width to auto to resize the width. 
 
    } 
</style> 

Screenshot: 
 

 

In the above screenshot the icons color codes are similar to our default toolbar icons color. 


Query: I take this opportunity to ask you if the same functionality of the date search, I can also have it by clicking the button with the magnifying glass icon and not only if I press Enter. 

Based on your query you want to perform search with magnifying glass icon. So, we have prepared sample in that we have performed the search operation when click the magnifying glass icon by using the click event to the magnifying glass icon. For your convenience we have attached the sample, please refer them for your reference. 



Code Example: 
Index.cshtml 

<div class="control-section"> 
    @{ 
        List<object> template = new List<object>(); 
       template.Add(new { template = "#toolbarSearch" }); 
    } 
 
    @Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.datasource).Toolbar(template).ToolbarClick("toolbarClick").Load("onLoad").Columns(col => 
    { 
 
    col.Field("OrderID").HeaderText("Order ID").Width("120").IsPrimaryKey(true).Filter(new { type = "CheckBox" }).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add(); 
    col.Field("CustomerID").HeaderText("Customer Name").Width("170").Add(); 
    col.Field("OrderDate").HeaderText("Order Date").Width("130").Type("date").EditType("datepickeredit").Format("dd/MM/yyyy").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add(); 
    col.Field("Freight").HeaderText("Freight").Width("120").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add(); 
    col.Field("ShipCountry").EditType("dropdownedit").HeaderText("Ship Country").Width("150").Add(); 
 
    }).Height("400").AllowPaging().AllowFiltering().FilterSettings(filter => filter.Type(Syncfusion.EJ2.Grids.FilterType.Menu)).EditSettings(edit => { edit.AllowEditing(true).AllowAdding(true).AllowDeleting(true); }).Render() 
</div> 
 
<div id="toolbarSearch"> 
    <div class="e-toolbar-item e-toolbar-print"  title="Print"><button class="e-tbar-btn e-tbtn-txt e-control e-btn e-lib" type="button" id="Grid_print" tabindex="-1" aria-label="Print" style="width: auto;"><span class="e-btn-icon e-print e-icons e-icon-left"></span><span class="e-tbar-btn-text">Print</span></button></div> 
    <div class="e-input-group c-input-div" > 
        <input class="e-input" name='input' id="toolSearch" type="text" placeholder="Search" /> 
        <span class="e-input-group-icon e-search-icon e-icons"></span> 
    </div> 
 
</div> 
<script> 
    function onLoad(){ 
        var grid = document.getElementById("Grid").ej2_instances[0]; 
 
        // Keyup event bound to the input element 
       
        grid.element.addEventListener('keyup', (args) => { 
            customSearch(args, false); 
        }); 
 
        grid.element.addEventListener("click", (args) => { 
            if (args.target.classList.contains("e-search-icon")) { 
                customSearch(args,true);                            
            } 
        }); 
    } 
 
    function customSearch(args, isSearchIconClicked) { 
        var grid = document.getElementById("Grid").ej2_instances[0]; 
        var gridInitialData = grid.dataSource.slice(0); 
        if ((args.target).getAttribute('id') === 'toolSearch' && args.key === "Enter" || isSearchIconClicked ) { 
            let gridObj = grid; 
            // Regex for checking date of format – “dd/MM/yyyy” 
            var regex = /^([0-2][0-9]|(3)[0-1])(\/)(((0)[0-9])|((1)[0-2]))(\/)\d{4}$/; 
            // Test regex with the entered value 
            var searchElement = document.getElementById("toolSearch"); 
 
            let input = isSearchIconClicked ? searchElement : (args.target); 
            if (regex.test(input.value)) { 
                var count = 0; 
                var predicates; 
                // Value is split to form JavaScript’s Date object 
                var dateSplit = input.value.split("/"); 
                var dateVal = new Date(parseInt(dateSplit[2]), (parseInt(dateSplit[1]) - 1), parseInt(dateSplit[0])); 
                while (count < gridObj.columns.length) { 
                    // Predicate is generated for all columns with type as ‘date’ 
                    let col = gridObj.columns[count]; 
                    if (col.type === "date") { 
                        // Predicates are generated with the column field name and date object value 
                        predicates = (predicates === undefined) ? new ej.data.Predicate(col.field, "contains", dateVal) : predicates.or(col.field, "contains", dateVal); 
                    } 
                    count++; 
                } 
                // Data manager is executed with grid’s datasource and query generated based on the predicates 
                new ej.data.DataManager({ json: (gridObj.dataSource) }).executeQuery(new ej.data.Query().where(predicates)).then((e) => { 
                    // The returned result is assigned to the grid datasource 
                    gridObj.dataSource = e.result; 
                }); 
            } else if (input.value === "") { 
                if (gridObj.searchSettings.key === "") { 
                    var data = gridInitialData; 
                    // If the input value and search key is empty the entire data source is assigned to the grid 
                    gridObj.dataSource = ej.data.DataUtil.parse.parseJson(data); 
                } else { 
                    // If the input value is empty but the grid contains a search key, then another search is performed with empty value to remove the search 
                    gridObj.search(""); 
                } 
            } else { 
                // Grid search method is called with the input value 
                gridObj.search(input.value); 
            } 
        } 
 
    } 
 
    function toolbarClick(args) { 
 
        var element = ej.grids.parentsUntil(args.originalEvent.target, "e-toolbar-print", false); 
 
        if (element) { 
            var grid = document.getElementById("Grid").ej2_instances[0]; 
            grid.print(); 
        } 
 
    } 
</script> 
 
<style> 
    .c-input-div{ 
        width:auto; // changed the search bar width here. 
 
    } 
</style> 


Please get back to us if you need further assistance. 

Regards, 
Ajith G. 




Loader.
Live Chat Icon For mobile
Up arrow icon