Author: Craig Stuntz
Having introduced jqGrid and written LINQ extension methods to make supplying data to the grid easy, we’re now ready to put together a demo application. The solution I’m going to build demonstrates sorting and paging. In a future post, I will enhance it to demonstrate search, formatting, and editing data. I’ve made the demo application available for download, but be advised that I intend to update it in the next few days; it’s currently a work in progress.
Rather than make you set up the Northwind database, I’ve written a quick, mock repository which supplies random data for the application. But the data persists for the lifetime of the app, so I’ll be able to demonstrate sorting, paging, etc. This way you can just run the application, without having to set up SQL Server.
So let’s write some code display a grid.
The data I’m going to display in the grid is a fairly useless type:
1234567 <strong>public class </strong>SomeEntity<br>{<br><strong>public int </strong>Id { <strong>get; set; </strong>}<br><strong>public int </strong>IntProperty { <strong>get; set; </strong>}<br><strong>public string </strong>StringProperty { <strong>get; set</strong>; }<br><strong>public </strong>DateTime DateProperty { <strong>get; set; </strong>}<br>}
We need two actions. The first will display the page containing the grid. The second supplies data for one page of the grid. Remember, the grid can page through large datasets. It will only request one page of data at a time.
1 2 3 4 5 6 7 8 9 10 11 12 |
<strong>public </strong>ActionResult GridDemo()<br> {<br> <strong>return </strong>View();<br> }<br> <br> <strong>public </strong>ActionResult GridDemoData(<strong>int </strong>page, <strong>int </strong>rows, <strong>string </strong>search, <strong>string </strong>sidx, <strong>string </strong>sord)<br> {<br> <strong>var </strong>repository = <strong>new </strong>Repository();<br> <strong>var </strong>model = repository.SelectAll().ToJqGridData(page, rows, sidx + " " + sord, search,<br> <strong>new</strong>[] { "IntProperty", "StringProperty", "DateProperty" });<br> <strong>return </strong>Json(model);<br> } |
The first action simply returns a view containing the HTML table which will eventually become the grid. It doesn’t need a model, because the grid will request its data asynchronously. The second action handles the data requested by the grid.
In order to make this work, we need HTML elements for the grid and its pager in the view, along with a reference to the JavaScript which will turn those elements into the grid.
123456789 <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"><br><h2>GridDemo</h2><br><table id="grid" class="scroll" cellpadding="0" cellspacing="0"></table><br><div id="pager" class="scroll" style="text-align:center;"></div><br></asp:Content><br><br><asp:Content ID="Script" ContentPlaceHolderID="ScriptContent" runat="server"><br><script language="javascript" type="text/javascript" src="<%= Url.Content("~/Scripts/Home.GridDemo.js") %>"></script><br></asp:Content>
The JavaScript file is where the grid itself is configured.
12345678910111213141516171819202122 $(document).ready(<strong>function</strong>() {<br>GridDemo.Home.GridDemo.setupGrid($("#grid"), $("#pager"));<br>});<br><br>GridDemo.Home.GridDemo = {<br>setupGrid: <strong>function</strong>(grid, pager) {<br>grid.jqGrid({<br>colNames: ['Int', 'String', 'Date'],<br>colModel: [<br>{ name: 'IntProperty', index: 'IntProperty' },<br>{ name: 'StringProperty', index: 'StringProperty' },<br>{ name: 'DateProperty', index: 'DateProperty' },<br>],<br>pager: pager,<br>sortname: 'IntProperty',<br>rowNum: 10,<br>rowList: [10, 20, 50],<br>sortorder: "asc",<br>url: "GridDemoData"<br>}).navGrid(pager, { edit: <strong>false</strong>, add: <strong>false</strong>, del: <strong>false</strong>, search: <strong>false </strong>});<br>}<br>};
Important to note here are:
colNames
, the grid column captions the user sees.
colModel
,which includes the propertyname
, which is the field name in the returned JSON data which supplies the data for that column, andindex
, which is the name which will be sent to theGridDemoData
action as thesidx
(field name to sort on) argument when you click on a grid header. There are a number of other properties you can set here which control editing, with, formatting, etc. See the documentation for full details. Importantly,colNames
andcolModel
must have the same number of entries, and be in the same order.
url
, which tells the grid where to get its data. You’ll notice that the value I’ve passed is the name of the second action.
As I mentioned in the last post, I set a number of the grid options in a script referenced by the Site.Master, in order to prevent having to set them on every grid I create. The configuration idea for the grid on an individual page should be only the things that are unique to that page. So there is a fair bit of configuration for the grid which is not included here. In addition, the Site.Master contains references to the grid’s JavaScript and CSS files.
Finally, it’s worth mentioning that when you add jqGrid to a project you need to edit the jQuery.jqGrid.js file to tell the grid where to find the rest of the JS files it needs. Once you look at this file, it is obvious what you need to change.
At this point, I’m going to make the demo application available for download, so that you can experiment with it yourself. However, I’ll caution that I intend to update it when I write the next post in this series. In other words, the current download is a work in progress. It will change in the next few days.
Download the Solution (587 KB)
So let’s run the solution and see what we get:
OK, the hideous and eyestrain-inducing clash between the default MVC theme and the jQuery UI “lightness” theme included with the grid download notwithstanding, this is actually not bad. Sorting and paging both work, and performance is very good.
But what’s the deal with the data in that date column? It turns out that there is no standard representation of a date in JSON, so Microsoft invented their own. The grid doesn’t know about this special format. We’ll clean that up in the next post in this series and also cover search. Stay tuned!
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition