Search box kendo grid

1 Answer 6112 Views
General Discussions
Devon
Top achievements
Rank 1
Devon asked on 18 May 2016, 02:02 PM

I'm trying to create a simple search box  that will search in all the fields found within the grid.

I tried using the filterable option to get a search box. But I ended up getting search box/operators for every single column. All I want is a single search box in my grid that searches through all columns/fields. How can I do this? Also I setting the showOperators false for specific columns didn't work for me. The operators are always visible.

I can post additional code if necessary but it's out of context for the problem I'm experiencing.

<p></p><p>filterable: {<br>     mode: 'row',<br>    showOperators: false<br>}</p>

 

1 Answer, 1 is accepted

Sort by
0
Patrick | Technical Support Engineer, Senior
Telerik team
answered on 18 May 2016, 05:28 PM
Hello Devon,

Please take a look at the following Progress Dojo which illustrates a Kendo Grid with a Search Box in the Toolbar.

Here are the main points:
1.  The toolbar is using a template which contains the label, textbox, and buttons.  Here is the code used:
    <script id="template" type="text/x-kendo-template">
          <label class="search-label" for="searchBox">Search Grid:</label>
          <input type="search" id="searchBox" class="k-textbox" style="width: 250px"/>
          <input type="button" id="btnSearch" class="k-button" value="Search"/>
          <input type="button" id="btnReset" class="k-button" value="Reset"/>
    </script>
 
$("#grid").kendoGrid({      
         ...
          toolbar: [
            { template: kendo.template($("#template").html()) }
          ],
         ...
     });

2. The search button's click event sets the Kendo Grid's Datasource Filters depending on what is passed in the textbox.  The reset button clears the filter.  Here's the function used for the search:
$("#btnSearch").click(function () {
          var searchValue = $('#searchBox').val();
          $("#grid").data("kendoGrid").dataSource.filter({
            logic  : "or",
            filters: [
              {
                field   : "ContactName",
                operator: "contains",
                value   : searchValue
              },
              {
                field   : "ContactTitle",
                operator: "contains",
                value   : searchValue
              },
              {
                field   : "CompanyName",
                operator: "contains",
                value   : searchValue
              },
              {
                field   : "Country",
                operator: "contains",
                value   : searchValue
              }
            ]
          });
        });

I hope this helps!

Regards,
Patrick
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Samy
Top achievements
Rank 1
commented on 29 Apr 2017, 04:51 PM

Hello, I have this following code and the search toolbar doesn't work

<!DOCTYPE html>
<html>
<head>
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/kendo/2017.1.223/kendo.all.min.js"></script>
    <script src="~/Scripts/knockout-3.4.2.js"></script>
    <script src="~/Scripts/knockout-kendo.js"></script>
    <script src="~/Scripts/knockout-kendo.min.js"></script>



    <link rel="stylesheet" href="~/Content/kendo/2017.1.223/kendo.common.min.css" />
    <link rel="stylesheet" href="~/Content/kendo/2017.1.223/kendo.default.min.css" />
    <link rel="stylesheet" href="~/Content/kendo/2017.1.223/kendo.blueopal.min.css" />
</head>

<body>
  
        <script id="template" type="text/x-kendo-template">
            <label class="search-label" for="searchBox">Search Ouvrage:</label>
            <input type="search" id="searchBox" class="k-textbox" style="width: 250px" />
            <input type="button" id="btnSearch" class="k-button" value="Search" />
            <input type="button" id="btnReset" class="k-button" value="Reset" />
        </script>

        <div id="grid" data-bind="kendoGrid"> </div>

        <script>
            var categories = [];
            var auteurs = [];
            var ouvrages = [];

            function Remplir_categories() {//Grid
                $.ajax({
                    url: "http://localhost:31871/api/CategorieSets",
                    dataType: "json",
                    async: false,
                    success: function (result) {
                        for (var i = 0; i < result.length; i++) {
                            categories.push({ value: result[i]["Id"], text: result[i]["Nom"] });
                        }
                    },
                });
            }
            Remplir_categories();
            function Remplir_auteurs() {//Grid
                $.ajax({
                    url: "http://localhost:31871/api/AuteurSets",
                    dataType: "json",
                    async: false,
                    success: function (result) {
                        for (var i = 0; i < result.length; i++) {
                            auteurs.push({ value: result[i]["Id"], text: result[i]["Nom"] });
                        }
                    },
                });
            }
            Remplir_auteurs();

            /* function auteurDropDownEditor(container, options) {

                 container.kendoDropDownList({
                     autoBind: true,
                     dataSource: auteurs,
                     dataTextField: "text",
                     dataValueField: "value",

                 });

             };
            /* function categoryDropDownEditor(container, options) {

                 container.kendoDropDownList({
                     autoBind: true,
                     dataSource: categories,
                     dataTextField: "text",
                     dataValueField: "value"


                 });
                 return categories.value
             };*/
            var OuvragesSetsDataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: "http://localhost:31871/api/OuvrageSets",
                        dataType: "json"

                    },
                    schema: {
                        model: {
                            id: "Id",
                            fields: {
                                Id: { editable: false, type: "number" },
                                Titre: { validation: { required: true } },
                                Description: { validation: { required: true } },
                                Prix: { validation: { required: true } },
                                Date_de_publication: { validation: { required: true } },
                                Image: { validation: { required: true } },
                                AuteurId: { validation: { required: true } },
                                CategorieId: { validation: { required: true } },
                                Auteur: { validation: { required: true } }
                            }
                        }
                    }
                }
            });
            /*  function Remplir_ouvrages() {//Grid
                   $.ajax({
                       url: "http://localhost:31871/api/OuvrageSets",
                       dataType: "json",
                       async: false,
                       success: function (result) {
                           for (var i = 0; i < result.length; i++) {
                               if (result[i]["Titre"] == "test") {
                                   ouvrages.push(result[i]);
                               }
                           }
                           OuvragesSetsDataSource = ouvrages
                       },
                   });

               }*/
            // Remplir_ouvrages();
            //
            $("#btnSearch").click(function () {
                var searchValue = $('#searchBox').val();

                //Setting the filter of the Grid
                kendoGrid.dataSource.filter({
                    filters: [
                        {
                            field: "Titre",
                            operator: "contains",
                            value: searchValue
                        }

                    ]
                });
            });

            //Clearing the filter
            $("#btnReset").click(function () {
                $("#grid").data("kendoGrid").dataSource.filter({});
            });

            var ViewModel = function () {
                var self = this;
                this.items = ko.observableArray([]);
              

                ko.bindingHandlers.kendoGrid.options = {
                    groupable: true,
                    scrollable: true,
                    toolbar: [
                        { template: kendo.template($("#template").html()) }
                    ],
                    sortable: true,
                    pageable: true,
                    
                    dataSource: OuvragesSetsDataSource,
                    height: 500,
                    columns: [
                        {
                            field: "Id",
                            title: "Id"
                        },
                        {
                            field: "Titre",
                            title: "Titre"
                        },
                        {
                            field: "Description",
                            title: "Description"
                        },
                        {
                            field: "Date_de_publication",
                            title: "Date_de_publication"
                        },
                        {
                            field: "Prix",
                            title: "Prix"
                        },
                        {
                            field: "Image",
                            title: "Image",
                        },

                        {
                            field: "AuteurId",
                            title: "Auteur",
                            values: auteurs,
                            dataTextField: "text",
                            dataValueField: "value"

                        },
                        {
                            field: "CategorieId",
                            title: "Categorie",
                            values: categories,
                            dataTextField: "text",
                            dataValueField: "value"
                        },

                        {
                            command: ["add"]
                        }
                    ]

                };



            }
            ko.applyBindings(new ViewModel());
        </script>
  
</body>


</html>

Stefan
Telerik team
commented on 03 May 2017, 06:26 AM

Hello Samy,

Please have in mind that based on the provided code, the search button should work only on the Titre column, as the filter is set only for that column:

filters: [
    {
        field: "Titre",
        operator: "contains",
        value: searchValue
    }
 
]

Also, please have in mind that the filtering will not work as expected for the DropDownList columns as the filter is made based on the value, but the Grid is displaying the text.

If additional assistance is needed, please provide a fully runnable example, or modify the one send by my colleague and I will gladly assist.

Regards,
Stefan
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data (charts) and form elements.
Saeed
Top achievements
Rank 1
commented on 30 Apr 2019, 01:50 PM

Hi Patrick

Thanks, Your reply was very helpful. Could you kindly mention how do we reset it as well. when we click the reset button?

Thanks

S

Patrick | Technical Support Engineer, Senior
Telerik team
commented on 30 Apr 2019, 02:25 PM

Hello Saeed,

We have a great Knowledge Base article which gives a solution to how to clear all filters in the Kendo UI Grid.  In the example, it uses a button to clear the Grid's datasource filter by setting it as empty.  

$("#grid").data("kendoGrid").dataSource.filter({});

I hope this helps with your Grid's filtering implementation.  Please let me know if you have any questions regarding the approach.

Regards,
Patrick
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Milehigh JT
Top achievements
Rank 1
commented on 18 Dec 2020, 05:16 PM

This is a nice feature.

How can I make it work with a grid that has drop down lists? The id is in the grid datasource, but the searchable text is in the dropdown. The id is a foreign key to the dropdown list which is passed in via the model.

Patrick | Technical Support Engineer, Senior
Telerik team
commented on 18 Dec 2020, 08:19 PM

Hello,

This can be done using the same approach as mentioned in this knowledge base article.  Please take a look at this Progress Kendo UI Dojo which demonstrates a Kendo UI Grid with DropDownList filters and a clear all filters button. 

Regards,
Patrick
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Milehigh JT
Top achievements
Rank 1
commented on 18 Dec 2020, 08:39 PM

Hi, I don't see a search box in the demo you have referenced. Is this a different topic? 

I have a grid. It has a row. That row has an id. That id is a foreign key. That foreign key is used to select a value in dropdown list. When I search I want to be able to search the text that is displayed in the dropdown. Is this possible?

This is similar to what my column looks like. I would want to search on Category Name, not Category ID:

columns.ForeignKey(p => p.CategoryID, (System.Collections.IEnumerable)ViewData["categories"], "CategoryID", "CategoryName").Title("Category").Width(150);

Thanks.

 

Patrick | Technical Support Engineer, Senior
Telerik team
commented on 21 Dec 2020, 04:03 PM

Hello JT,

The Search panel can be configured to search for a specific field using the search.fields property.  So, for example, if the Search Panel is utilized in our Foreign Key Column live demos(UI for ASP.NET MVC / UI for ASP.NET Core), here's how you would filter only for the Category.CategoryName field:

@(Html.Kendo().Grid<GridExample.Models.ProductViewModel>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.ProductName);
        columns.ForeignKey(p => p.CategoryID, (System.Collections.IEnumerable)ViewData["categories"], "CategoryID", "CategoryName")
            .Title("Category").Width(200);
        columns.Bound(p => p.UnitPrice).Width(200);
        columns.Command(command => command.Destroy()).Width(150);
    })
    .ToolBar(toolBar =>
    {
        toolBar.Create();
        toolBar.Save();
        toolBar.Search();
    })
    .Search(e => e.Field(f => f.Category.CategoryName))
    //...
)

Regards,
Patrick
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
General Discussions
Asked by
Devon
Top achievements
Rank 1
Answers by
Patrick | Technical Support Engineer, Senior
Telerik team
Share this question
or