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

Cannot access grid dataSource for filtering

2 Answers 197 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Gibran
Top achievements
Rank 1
Gibran asked on 07 Mar 2017, 12:00 AM
Hello,

I trying to programatically filter my data in my grid. Here's my code:

[code]
var ds =  {
                    type: "odata",
                    transport: {
read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Customers"
                    },
                    pageSize: 20,
   group: {
field: "ContactTitle",
dir: "asc",
template: "Yo, I'm a template"
}
                };

var g = $("#grid").kendoGrid({

  dataSource: ds,
                height: 550,
scrollable: true,
resizable: true,
                groupable: true,
                sortable: true,
groupable: true,
                pageable: {
                    refresh: true,
                    pageSizes: true,
                    buttonCount: 5
                },
                columns: [{
                    template: "<div class='customer-photo'" +
                                    "style='background-image: url(../content/web/Customers/#:data.CustomerID#.jpg);'></div>" +
                                "<div class='customer-name'>#: ContactName #</div>",
                    field: "ContactName",
                    title: "Contact Name",
                    width: 240
                }, {
                    field: "ContactTitle",
                    title: "Contact Title",
groupHeaderTemplate: "Yo, I'm a group for #= value #"
                }, {
                    field: "CompanyName",
                    title: "Company Name"
                }, {
                    field: "Country",
                    width: 150
                }]
            });

g.data().dataSource.filter({
"field" : "ContactName",
"operator" : "contains",
"value" : "Bernardo Batista"
});
[/code]

But this is giving me an error on the line:

g.data().dataSource.filter({...

The error message is: Uncaught TypeError: Cannot read property 'filter' of undefined.

How can I get my datasource from my grid? And then, how do I apply a filter to it?

Thank you.

2 Answers, 1 is accepted

Sort by
0
Accepted
Viktor Tachev
Telerik team
answered on 08 Mar 2017, 12:55 PM
Hello Gibran,

In order to get reference to the Grid you need to use .data("kendoGrid"). Then you can access the dataSource and apply the relevant filter.

The sample below illustrates the approach. Give it a try and let me know how it works for you.



Regards,
Viktor Tachev
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 visualization (charts) and form elements.
0
Gibran
Top achievements
Rank 1
answered on 08 Mar 2017, 06:06 PM

Thanks Viktor,

That worked.

Tags
Grid
Asked by
Gibran
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Gibran
Top achievements
Rank 1
Share this question
or