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

Lazy Load Initial Grid Data MVC

3 Answers 840 Views
Grid
This is a migrated thread and some comments may be shown as answers.
jwize
Top achievements
Rank 1
jwize asked on 07 May 2013, 04:33 AM
I am using code as follows 

Kendo().Grid
      ...
      .DataSource(datasource => datasource
    .Ajax()
    .Model(m => m.Id(p=> p.Id))
    .Read(read => read.Action("ClassificationTypesGridData", "Admin"))
I have seven grids that load in hidden divs. I don't want them to fetch data until the my show grid button is clicked. 

3 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 07 May 2013, 10:34 AM
Hello,

<div>
    <input type="button" id="btnSearch" onclick="BindGrid();" value="Bind" />
</div>
<div id="dvGridContianer" style="display: none;">
    @(Html.Kendo().Grid<MvcApplication1.Models.TestModels>()
        .Name("Grid")
        .Columns(columns =>
        {
            columns.Bound(p => p.ID);
            columns.Bound(p => p.Name);
        })
        .Filterable()
        .Sortable()
        .DataSource(dataSource => dataSource
            .Ajax()
                    .Read(read => read.Action("Grid_Read", "Home").Data("getParameter"))
            .Model(model => { model.Id(p => p.ID); })
        )
    )
</div>
<script type="text/javascript">
    var IsNeedToBind = false;
    function getParameter() {
        return {
            isNeedToBind: IsNeedToBind
        };
    }
 
    function BindGrid() {
        IsNeedToBind = true;
        $("#dvGridContianer").css('display','');
        $("#Grid").data("kendoGrid").dataSource.read();
        $("#Grid").data("kendoGrid").refresh();
    }
</script>
public ActionResult Grid_Read([DataSourceRequest] DataSourceRequest request, bool isNeedToBind)
        {
            List<TestModels> models = new List<TestModels>();
 
            if (isNeedToBind)
            {
                for (int i = 1; i < 6; i++)
                {
 
                    TestModels model = new TestModels();
                    model.ID = i;
                    model.Name = "Name" + i;
                    models.Add(model);
 
                }
            }
 
            return Json(models.ToDataSourceResult(request));
        }



Thanks,
Jayesh Goyani
0
jwize
Top achievements
Rank 1
answered on 14 May 2013, 12:42 AM
That is the obvious way but it seems like a hack. I was hoping for a more elegant solution. Thanks for taking the time though. 
0
Jayesh Goyani
Top achievements
Rank 2
answered on 15 May 2013, 09:53 AM
Tags
Grid
Asked by
jwize
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
jwize
Top achievements
Rank 1
Share this question
or