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

Retain the selected record page or filter after edit or view on grid

3 Answers 399 Views
Grid
This is a migrated thread and some comments may be shown as answers.
DHHS
Top achievements
Rank 1
DHHS asked on 18 Aug 2015, 06:29 PM

Hi,

 I have grid with more than 100 records. I want to edit a record. so I have used filter or manually went to that page (say 5th page) and clicked edit or view. My edit and view buttons are custom buttons where it takes to another page. when i click save or cancel on edit or view pages, it will navigate to page where i have grid. The grid is by default selecting the first record. But i want to retain the filter or page number where the record is and should go to that page automatically. If it is in first page i am doing it on data bind, manually selecting the record and it is working fine. If the record is in the 5th page or so , I am selecting the record but the grid is going to default to first page.

 

function grid_dataBinding(e) {
        
        var grid = this;
        var count = 0;
        var value = $("#myDiv").data('value');   
        if (value > 0) {
            $.each(grid.tbody.find('tr'), function () {
                var model = grid.dataItem(this);
                if (model.id == value) {//some condition
                    $('[data-uid=' + model.uid + ']').addClass('k-state-selected');
                    count++;
                    return;
                }
            });
        }
// If i dont have any selected id, I am defaulting it to first record
        else {
            this.element.find('tbody tr:first').addClass('k-state-selected');
        }
// If i dont find the selected id in the grid am defaulting it to first record
        if (count == 0)
            this.element.find('tbody tr:first').addClass('k-state-selected');
}

3 Answers, 1 is accepted

Sort by
0
Alexander Popov
Telerik team
answered on 20 Aug 2015, 01:54 PM
0
DHHS
Top achievements
Rank 1
answered on 20 Aug 2015, 10:08 PM

Hi Alexander, 

Thanks for the reply. I figured that after some search. Sorry my bad.

 

But i have got another problem. I am setting options in data bound of the grid and storing it in my session object. when user clicks some button, I take it to another page and when he comes back to the page where grid is, in document ready method, I am trying to get the options. But I think , before going to document ready method, it is going to data bind and default options are getting set and in document ready method i am getting default settings back.

function onDataBound(e) {  
    var grid = $("#grid").data("kendoGrid");
    var dataSource = grid.dataSource;      
    var state = {
        page: dataSource.page(),
        pageSize: dataSource.pageSize(),
        sort: dataSource.sort(),
        filter: dataSource.filter()
    };
 
    $.ajax({
        url: '@Url.Action("Save", "Contractor")',
        data: {
            data: JSON.stringify(state)
        }
    });
 
$(document).ready(function () {
   // var value = $("#myDiv2").data('value');
    //alert(value);
    var grid = $("#grid").data("kendoGrid");
    var dataSource = grid.dataSource;
 
    $.ajax({
        url: '@Url.Action("Load", "Contractor")',
        success: function (state) {
            alert(state);
            if (state != null && state != "" && typeof state != 'undefined') {
                state = JSON.parse(state);
                var options = grid.options;
                options.dataSource.page = state.page;
                options.dataSource.pageSize = state.pageSize;
                options.dataSource.sort = state.sort;
                options.dataSource.filter = state.filter;                  
                grid.destroy();
                $("#grid")
                   .empty()
                   .kendoGrid(options);
            }
        }
    });
});
 
 
   [ValidateInput(false)]
    public ActionResult Save(string data)
    {
        Session["data"] = data;
        return new EmptyResult();
    }
 
 
    public ActionResult Load()
    {
        if (Session["data"] != null)
            return Json(Session["data"], JsonRequestBehavior.AllowGet);
 
        return Json("", JsonRequestBehavior.AllowGet);
    }

0
Alexander Popov
Telerik team
answered on 24 Aug 2015, 07:58 AM
Hello,

I am not sure I understand when and how the Grid is initialized for the first time. You could however, try getting the saved state and if there is none - initialize the Grid with default options, otherwise use the saved ones. 

Regards,
Alexander Popov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Grid
Asked by
DHHS
Top achievements
Rank 1
Answers by
Alexander Popov
Telerik team
DHHS
Top achievements
Rank 1
Share this question
or