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

[Solved] Loading Correct Page for Selected Row

1 Answer 62 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Colin McDonald
Top achievements
Rank 1
Colin McDonald asked on 13 Dec 2010, 05:50 PM
Hello,

Currently when I load my Telerik MVC Grid, I specify which row should be selected on load using the following:
        .RowAction(row =>
        {
            row.Selected = row.DataItem.pk_retrofit_id.ToString().Equals(ViewData["SelectedId"]);
        })

My issue arises when the selected row is not within the first page (I have paging enabled).  Is there a way to load the grid on the page containing the selected row?

I know I could possibly figure out which page the row would exist in, then set the 'paging.PageTo' property...this feels a bit "hacked-out".  Is there a better solution?

Thanks,

Colin

1 Answer, 1 is accepted

Sort by
0
Colin McDonald
Top achievements
Rank 1
answered on 20 Dec 2010, 10:32 PM
I have temporarily remedied this issue using the following code (in case anyone else on the net runs into this issue):

Controller:
        public ActionResult _BuildingList(string selectedId)
        {
            int bldgId = String.IsNullOrWhiteSpace(selectedId) ? -1 : int.Parse(selectedId);
            int pageToLoad = 1;
            int recordsPerPage = 5;

            if(bldgId > 0)
            {
                var building = lightingRepo.GetBuilding(bldgId);
                int index = lightingRepo.GetBuildingList(project).ToList().IndexOf(building);
                pageToLoad = (int) Math.Ceiling(((double)index + 1)/recordsPerPage);
            }

            ViewData["PageToLoad"] = pageToLoad;
            ViewData["RecordsPerPage"] = recordsPerPage;
            ViewData["SelectedId"] = selectedId;
            return View(lightingRepo.GetBuildingListViewModel(project));
        }
        public ActionResult _BuildingList(string selectedId)
        {
            int bldgId = String.IsNullOrWhiteSpace(selectedId) ? -1 : int.Parse(selectedId);
            int pageToLoad = 1;
            int recordsPerPage = 5;

            if(bldgId > 0)
            {
                var building = lightingRepo.GetBuilding(bldgId);
                int index = lightingRepo.GetBuildingList(project).ToList().IndexOf(building);
                pageToLoad = (int) Math.Ceiling(((double)index + 1)/recordsPerPage);
            }

            ViewData["PageToLoad"] = pageToLoad;
            ViewData["RecordsPerPage"] = recordsPerPage;
            ViewData["SelectedId"] = selectedId;
            return View(lightingRepo.GetBuildingListViewModel(project));
        }
        public ActionResult _BuildingList(string selectedId)
        {
            int bldgId = String.IsNullOrWhiteSpace(selectedId) ? -1 : int.Parse(selectedId);
            int pageToLoad = 1;
            int recordsPerPage = 5;

            if(bldgId > 0)
            {
                var building = lightingRepo.GetBuilding(bldgId);
                int index = lightingRepo.GetBuildingList(project).ToList().IndexOf(building);
                pageToLoad = (int) Math.Ceiling(((double)index + 1)/recordsPerPage);
            }

            ViewData["PageToLoad"] = pageToLoad;
            ViewData["RecordsPerPage"] = recordsPerPage;
            ViewData["SelectedId"] = selectedId;
            return View(lightingRepo.GetBuildingListViewModel(project));
        }
public ActionResult _BuildingList(string selectedId)
{
    int bldgId = String.IsNullOrWhiteSpace(selectedId) ? -1 : int.Parse(selectedId);
    int pageToLoad = 1;
    int recordsPerPage = 5;
 
    if(bldgId > 0)
    {
        var building = lightingRepo.GetBuilding(bldgId);
        int index = lightingRepo.GetBuildingList(project).ToList().IndexOf(building);
        pageToLoad = (int) Math.Ceiling(((double)index + 1)/recordsPerPage);
    }
 
    ViewData["PageToLoad"] = pageToLoad;
    ViewData["RecordsPerPage"] = recordsPerPage;
    ViewData["SelectedId"] = selectedId;
    return View(lightingRepo.GetBuildingListViewModel(project));
}



View:
<%
    int pageSize = (ViewData["RecordsPerPage"] as int?) ?? 1;
    int pageToLoad = (ViewData["PageToLoad"] as int?) ?? 1;
 %>
 
<h3>1. Select Building:</h3>
 
<%= Html.Telerik().Grid(Model)
    .Name("Buildings")
    .Columns(columns =>
    {
        columns.Bound(b => b.pk_bldg_id).Hidden(true);
        columns.Bound(b => b.bldg_code).Title("Bldg Code");
        columns.Bound(b => b.bldg_name).Title("Bldg Name");
        columns.Bound(b => b.site_category).Title("Category");
        columns.Bound(b => b.location_1).Title("Location");
    })
    .Pageable(paging => { paging.PageSize(pageSize); paging.Enabled(true); paging.PageTo(pageToLoad); })
    .Resizable(resizing => resizing.Columns(true))
    .Sortable()
    .Selectable(select => select.Enabled(true))
    .Filterable()
    .DataBinding(dataBinding => dataBinding.Ajax().Select("_GetBuildingList", "Retrofits"))
    .ClientEvents(events => events.OnRowSelected("OnBldgSelected"))
    .RowAction(row =>
    {
        row.Selected = row.DataItem.pk_bldg_id.ToString().Equals(ViewData["SelectedId"]);
    })
%>
Tags
Grid
Asked by
Colin McDonald
Top achievements
Rank 1
Answers by
Colin McDonald
Top achievements
Rank 1
Share this question
or