Moving on slightly from my last post on the Kendo UI Grid we’re going to take a wee look at paging and accessing the results of the filter in javaScript.
pageable : true
By default paging is turned off. This means that when the grid is rendered you get all the data displayed in one go. If the amount of data is small then this
may be fine. However, if the amount of data runs into the hundreds of rows (or more) then you’ll probably want to turn paging on in order to make the display of the data more manageable for the user and potentially to reduce the amount of data send to the browser (but that part is for another day – in this example I’ll be using the same data set as previously which is loaded all at once).
may be fine. However, if the amount of data runs into the hundreds of rows (or more) then you’ll probably want to turn paging on in order to make the display of the data more manageable for the user and potentially to reduce the amount of data send to the browser (but that part is for another day – in this example I’ll be using the same data set as previously which is loaded all at once).
To enable paging add to the configuration
pageable : true
and also remember to add in to the dataSource
part of the configuration thepageSize
that you want.
If you forget to put the
pageSize
in then the grid will display with all the elements, but the paging navigation bar will display a message such as “NaN – NaN of 150 items”scrollable : false
By default the grid is scrollable. This is useful if you have something to scroll, such as the virtualised scrolling feature. But for the paging in this example, the scroll bar is simply displayed but not enabled.
To turn off the scrollbar, in the configuration set
scrollable : false
and the scroll bar will be removed.Getting the filtered results in JavaScript
It is possible to get the results of the filter out of the grid. It isn’t actually a direct feature of the grid (or the dataSource) but it is possible in a round about sort of way.
Essentially, what needs to happen is that filter object in the grid is used to query the data all over again to produce a second result set that can be used directly in JavaScript.
In the example below, I’ve got the results of the filter being rendered into a unordered list block.
It works but first getting hold of the grid’s data source, getting the filter and the data, creating a new query with the data and applying the filter to it. While this does result in getting the results of the filter it does have the distinct disadvantage of processing the filter operation twice.
function displayFilterResults() { // Gets the data source from the grid. var dataSource = $("#MyGrid").data("kendoGrid").dataSource; // Gets the filter from the dataSource var filters = dataSource.filter(); // Gets the full set of data from the data source var allData = dataSource.data(); // Applies the filter to the data var query = new kendo.data.Query(allData); var filteredData = query.filter(filters).data; // Output the results $('#FilterCount').html(filteredData.length); $('#TotalCount').html(allData.length); $('#FilterResults').html(''); $.each(filteredData, function(index, item){ $('#FilterResults').append('
The results look like this:
The filter results in 12 of 150 rows returned. National Galleries of Scotland (Edinburgh sites) : 1281465 Edinburgh Castle (Historic Scotland) : 1210248 Kelvingrove Art Gallery & Museum (Glasgow) : 1070521 Royal Botanic Garden Edinburgh : 707244 Gallery of Modern Art (Glasgow Museums) : 490872 People's Palace (Glasgow Museums) : 245770 Burrell Collection (Glasgow Museums) : 187756 Museum of Transport (Glasgow Museums) : 160571 St Mungo Museum of Religious Art (Glasgow Museums) : 143017 Provand's Lordship (Glasgow Museums) : 107044 Scotland Street School Museum (Glasgow Museums) : 49346 Glasgow Museums Resource Centre : 9059
Full grid configuration
Here is the full configuration of the grid for this example:
$(function(){ var data = getData(); // From the bva-data.js file $('#MyGrid').kendoGrid({ dataSource: { data: data, pageSize: 10, schema: { model: { fields: { Site: {type: "string" }, Visitors: {type: "number" }, FreeCharge: {type: "string" }, Change: {type: "number" } } } } }, filterable: true, columnMenu: false, sortable: true, pageable: true, scrollable: false, columns: [ { field: "Site" }, { field: "Visitors" }, { field: "FreeCharge" }, { field: "Change", template: "#= kendo.toString(Change, \"p\") #" } ], dataBound: function(e) { displayFilterResults(); } }); });
reference: http://colinmackay.scot/2012/07/23/kendo-ui-paging-and-accessing-the-filtered-results-in-javascript/
No comments:
Post a Comment