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

find page for data item by uid, in sorted / filtered grid

3 Answers 492 Views
Grid
This is a migrated thread and some comments may be shown as answers.
CK
Top achievements
Rank 1
CK asked on 18 Nov 2014, 03:20 PM
In a pageable grid, I want to move to the page that contains the row for uid == x.  But how can I determine the page number for the data item with uid == x, if a sort or filter has been applied to the grid?  If there is no sort or filter, I can find the page number with the following code:

function gotoPageForUid (grid, uid) {
    var kdata = grid.dataSource.data();
    for (var i = 0; i < kdata.length; i++) {
        if (uid == kdata[i].uid) {
            var pgsize = grid.dataSource.pageSize();
            var pageForRow = 1 + Math.floor(i/pgsize);
 
           grid.dataSource.page(pageForRow);
            break;
        }
    }
}


But that code is not correct if the grid is sorted and/or filtered.  
What is the correct code?

3 Answers, 1 is accepted

Sort by
0
CK
Top achievements
Rank 1
answered on 19 Nov 2014, 05:15 AM
I should also mention that I have the dataSource bound to a javascript array, so all of the pages of data are available
0
Accepted
Nikolay Rusev
Telerik team
answered on 20 Nov 2014, 03:49 PM
Hello CK,

DataSource data method holds all data without transformations like filter/sort/page etc. DataSource view method holds only current page data filtered/sorted etc. That said if you need to find the page for item you will have to process the dataSource.data() without applying paging as this will limit the result set to the current page items only.

One way to achieve this is to use the following approach. The result data will contain filtered/sorted data without paging applied. Then you can use the data to find the item and calculate the page.

//get reference to the data source
var ds = <get ref to the data source in question here>;
 
//apply sort and filter of the original data in the data source
//as result `data`  will contain not-paged data set
var data = kendo.data.Query.process(ds.data(), {
 sort: ds.sort(),
 filter: ds.filter()
}).data;


Regards,
Nikolay Rusev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
CK
Top achievements
Rank 1
answered on 20 Nov 2014, 08:48 PM
Excellent, that works. Thank you.
Tags
Grid
Asked by
CK
Top achievements
Rank 1
Answers by
CK
Top achievements
Rank 1
Nikolay Rusev
Telerik team
Share this question
or