Server Side Grid Paging and Sorting

This help topic shows how to implement server-side paging and sorting with the jqxGrid widget. The Grid will request data from the server when the user navigates to a new page, changes the page's size or applies a sort order. The server-side script is going to deliver the data records in JSON format. We will obtain the data from Northwind Database and especially from the Customers table. You can download the Northwind database .sql script here and run it into MySQL to create the database.
By default, the Grid sends the following data to the server:

1. The first step is to create the file we’ll connect with. We will call the file ‘connect.php’
2. The second step is to create the file that will handle the queries. We will call the file data.php. The data.php file connects to the ‘Customers’ table from the Northwind Database and returns the data as JSON. Our goal is to send data to client in small pieces that the client requests, and respond when the page number, page size or sort order is changed by the user. In the implementation, we check for the ‘pagenum’ and ‘pagesize’ members which the Grid sends to the server and we use the values of these members to specify the range of records in the query to the database. We also make a query to find the total rows in the ‘Customers’ table. The query depends on the sortfield(Column) and the sortorder(‘ascending’ or ‘descending’). The ‘sortdatafield’ and ‘sortorder’ parameters are passed to the server by jqxGrid. The returned JSON data contains two things- the total rows in the ‘Customers’ table and the requested records.

3. The final step is to create the Grid and bind it to the ‘Customers’ table.
Let’s see how the above code works. As we need to populate the Grid on demand, we set its ‘virtualmode’ property to true. This means that the Grid will display only the records returned as array from the ‘rendergridrows’ callback function. The Grid will make requests to the server when the user clicks the ‘Next’ or ‘Previous’ buttons, changes the page’s size or sorts a column. We also set the source object’s totalrecords property to the TotalRows value returned from the server. In the source object’s initialization we set the ‘datatype’ to ‘json’ as the returned data will be JSON data and the ‘root’ member to ‘Rows’ as the records are stored in the ‘Rows’ array.