Easyui datagrid dynamic columns and front-end pagination, and able Loading

Source: Internet
Author: User

The project uses an SQL statement to display results. Therefore, we need to dynamically change the column of the datagrid. It is best to use the background paging, but the method is not found, so the front-end paging is used.

Background code:

StringBuilder columns = new StringBuilder ("[["); foreach (DataColumn col in ds. tables [0]. columns) columns. appendFormat ("{field: '{0}', title: '{1}', align: 'center', width: {2 }},", col. columnName, col. columnName, 100); if (ds. tables [0]. columns. count> 0) columns. remove (columns. length-1, 1); // remove the redundant ',' number columns. append ("]"); Dictionary <string, object> objDic = new Dictionary <string, object> (); objDic. add ("columns", columns. toString (); objDic. add ("rows", ds. tables [0]); this. context. response. write (JsonConvert. serializeObject (new JSON (true, objDic ,"")));

JSON is a custom class, And objDic corresponds to its obj attribute. Concatenate the column string required by the datagrid and convert it to an object using eval on the front end.

Var opt = $ (dg ). datagrid ('options'); opt. columns = eval (result. obj. columns); // convert the returned array string into an object and assign it to the column attribute opt. title = "SQL:" + SQL; $ (dg ). datagrid (opt );

Only the DataTable queried by ado.net can be directly serialized without the total and rows attributes required by the datagrid. Therefore, you need to define an object at the front end and pay it to the datagrid.

Var data = new Object (); data. total = result. obj. rows. length; data. rows = result. obj. rows; $ (dg ). data (). datagrid. cache = data; $ (dg ). datagrid ('reload'); // $ (dg ). datagrid ('loaddata', result. obj. rows); // This is not acceptable.

Cache is not the default attribute of the datagrid. It is a data storage attribute customized for front-end paging. When the reload method is called, The Datagrid loader is triggered (it is said that this attribute is available only after datagrid1.3.1, and I use version 1.3.1 ).
The initialization code of the datagrid is as follows:

// Initialize the data list function initDatagrid () {// <summary> initialize the data list </summary> $ (dg ). datagrid ({fit: true, border: false, striped: true, pagination: true, checkOnSelect: true, selectOnCheck: true, singleSelect: true, nowrap: true, rownumbers: true, title: "data table :( select)", width: 500, height: 350, pageSize: 20, pageList: [10, 20, 30, 40, 50], loader: datagridFrontPageLoader2 // front page });}

Define the loader attribute, and then see how the DatagridFrontPageLoader2 is written.

/*** @ Author Zhao baolong ** @ requires jQuery ** the front-end page loader of the Datagrid. When loading data using loadData, use */function DatagridFrontPageLoader2 (param, success, error) {var that = $ (this); var cache = that. data (). datagrid. cache; if (cache) {success (bucket data (cache);} else {return false;} function bucket data (data) {var temp = $. extend ({}, data); var tempRows = []; var start = (param. page-1) * parseInt (param. rows); var end = start + parseInt (param. rows); var rows = data. rows; for (var I = start; I <end; I ++) {if (rows [I]) {tempRows. push (rows [I]);} else {break;} temp. rows = tempRows; return temp ;}}

First, obtain all the data through the cache attribute, and then select the data required for the current page in the bucket data function, and send it to success for loading.
This processing function is triggered by clicking the previous page, next page, and refresh buttons on the front end, instead of loading data remotely.

This function is implemented by modifying Sun Yu's method. He can load data remotely. This is not applicable to me.

/*** @ Author Sun Yu ** @ requires jQuery ** the front-end page loader of the Datagrid. Use the */function DatagridFrontPageLoader (param, success, error) to load data using URLs) {var that = $ (this); var opts = that. datagrid ("options"); if (! Opts. url) {return false;} var cache = that. data (). datagrid. cache; if (! Cache) {$. ajax ({type: opts. method, url: opts. url, data: param, dataType: "json", success: function (data) {that. data (). datagrid ['cache'] = data; success (bucket data (data) ;}, error: function () {error. apply (this, arguments) ;}}) ;}else {success (bucket data (cache) ;}function bucket data (data) {var temp = $. extend ({}, data); var tempRows = []; var start = (param. page-1) * parseInt (param. rows); var end = start + parseInt (param. rows); var rows = data. rows; for (var I = start; I <end; I ++) {if (rows [I]) {tempRows. push (rows [I]);} else {break;} temp. rows = tempRows; return temp ;}}

 

If you do not sum up, you will always forget to record it here ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.