When I use remote sorting, but it cannot work well.
I developed a query page. First I left the igGird's dataSource property to null, so I can get an empty table to display.
After user click query button, I'll get data from controller to populate the grid.
User can sort data by any column, I used columnSorted event to achieve it. But after I populated the gird using the returned data, the sort direction missed.
My main code as bellow:
-----------cshtm----------------
<script type="text/javascript">
var queryObject = { pageIndex: 0, pageSize: 5, columnKey: "", direction: "" };
$.ig.loader({ scriptPath: '@Url.Content("~/Scripts/")', cssPath: '@Url.Content("~/Content/")', resources: "igGrid.Sorting,igGrid.Paging" });
$.ig.loader(function () { $("#grid1").igGrid({ dataSource: null, autoGenerateColumns: false, responseDataKey: "Records", responseTotalRecCountKey: "TotalRecordsCount", columns: [ { headerText: "Serial No", key: "SerialNo" }, { headerText: "User Name", key: "UserName" }, { headerText: "Nick Name", key: "NickName" } ], features: [ { name: "Paging", pageSizeList: [5, 10, 15], pageSize: 5, type: "remote", recordCountKey: 'TotalRecordsCount', pageIndexChanged: function (ui, args) { queryObject.pageIndex = args.pageIndex; getData(queryObject) }, pageSizeChanged: function (ui, args) { queryObject.pageSize = args.pageSize; queryObject.pageIndex = 0; getData(queryObject) } }, { name: "Sorting", type: "remote", mode: "single", columnSorted: sortData, columnSettings: [ { columnKey: "SerialNo", allowSorting: true }, { columnKey: "UserName", allowSorting: true }, { columnKey: "NickName", allowSorting: true } ] } ] }); }); </script><table id="grid1"></table><input type="button" name="btnquery" value="Query" onclick="BLOCKED SCRIPTgetData(queryObject);" /><script type="text/javascript"> //Handler code function sortData(event, args) {
if (!args.columnKey || !args.direction) { return false; }
queryObject.columnKey = args.columnKey; queryObject.direction = args.direction;
getData(queryObject); }
function getData(queryObject) { var url = '@Url.Action("igGridLoaderData")'; // $("#grid1").igGrid("option", "dataSourceType", "json"); // $("#grid1").igGrid("option", "dataSource", url);
$.ajax({ type: "POST", url: url, data: queryObject, success: function (data) { $("#grid1").igGrid("option", "dataSourceType", "json"); $("#grid1").igGrid("option", "dataSource", data); $("#grid1").igGridPaging("pageSize", queryObject.pageSize); $("#grid1").igGridPaging("pageIndex", queryObject.pageIndex); } }); }</script>
------------Controller---------------------
private IList<TestModel> _testData = new List<TestModel>() { new TestModel(){SerialNo=1, UserName="UserName1", NickName="NickName1"}, new TestModel(){SerialNo=2, UserName="UserName2", NickName="NickName2"}, new TestModel(){SerialNo=3, UserName="UserName3", NickName="NickName3"}, new TestModel(){SerialNo=4, UserName="UserName4", NickName="NickName4"}, new TestModel(){SerialNo=5, UserName="UserName5", NickName="NickName5"}, new TestModel(){SerialNo=6, UserName="UserName6", NickName="NickName6"}, new TestModel(){SerialNo=7, UserName="UserName7", NickName="NickName7"}, new TestModel(){SerialNo=8, UserName="UserName8", NickName="NickName8"}, new TestModel(){SerialNo=9, UserName="UserName9", NickName="NickName9"}, new TestModel(){SerialNo=10, UserName="UserName10", NickName="NickName10"}, new TestModel(){SerialNo=11, UserName="UserName11", NickName="NickName11"}, new TestModel(){SerialNo=12, UserName="UserName12", NickName="NickName12"}, new TestModel(){SerialNo=13, UserName="UserName13", NickName="NickName13"}, new TestModel(){SerialNo=14, UserName="UserName14", NickName="NickName14"}, new TestModel(){SerialNo=15, UserName="UserName15", NickName="NickName15"}, new TestModel(){SerialNo=16, UserName="UserName16", NickName="NickName16"} };
public JsonResult igGridLoaderData(int pageIndex, int pageSize, string columnKey, string direction) { PagedList<TestModel> pagedList = new PagedList<TestModel>(_testData, pageIndex, pageSize);
var q = from v in pagedList orderby v.SerialNo descending select new { v.SerialNo, v.UserName, v.NickName };
var o = new { TotalRecordsCount = pagedList.TotalRecordsCount, Records = q, timezoneOffset = 0 };
return Json(o, JsonRequestBehavior.AllowGet);
}
Hi,
i am not sure why you need to handle the events and call GetData, when the grid should do that for you out of the box? Also when you do GetData, you are basically rebinding the grid. It's already rebound before this event is called, so you end up with two data binding calls basically. about the resetting of the indicator, i am a bit hazy on the exact steps of the scenario. could you try first using the grid without the extra custom code. it should work fine, you just need to set the grid data source to '@Url.Action("igGridLoaderData")'. or use the MVC Wrapper for the grid, instead of manually writing the JavaScript code.
Thanks
Angel
I don't want load data when page first load. It's a query page, I'll load data when user clicked query button.
If is set the grid data source to '@Url.Action("igGridLoaderData")', it'll load data immediately.
Hello Lostonzhang,
We will research possible solutions for this scenario and update you when more information is available.
Thank you.
What I can recommend you in this situation is to apply Data Source from your controller as required and use page load or grid render events to clear all rows present in the grid. Thus your grid will appear empty on initial load and then you can rebind it again and use remote paging and sorting as specified.
I hope that this approach will be useful in your scenario.
I'm evaluating the product and am also having issues with maintaining sorting state. Our grid data is repopulated via ajax calls and then the data is bound like such
$("#grid").igGrid("dataSourceObject", grid.data).igGrid("dataBind");
When the data bind completes then the sort state is reverted to the default state set when I initialized the grid using the following feature setting.
{ name: "Sorting", type: "local", columnSettings: [ {columnKey: 'NAME', currentSortDirection:'asc'} ], columnSorted: function (evt, ui) { grid.sorting.column = ui.columnKey; grid.sorting.direction = ui.direction; } }
I tried to track the changes by getting the values from the columnSorted event as you can see above, but then I can not find the correct location in which to apply them again. I tried the dataRendered event, but this resulted in an infintite loop.
Is there a way to maintain the sort after binding data?