asp.net - Use jqgrid action formatter -


i fill grid ashx file , work fine. when add action in grid ,data of cell shift right , last column show null.

before add action

enter image description here

after add action

enter image description here

grid in firebug:

enter image description here

ashx:

  public void processrequest(httpcontext context) {             httprequest request = context.request;             httpresponse response = context.response;             string _search = request["_search"];             string numberofrows = request["rows"];             string pageindex = request["page"];             string sortcolumnname = request["sidx"];             string sortorderby = request["sord"];             int totalrecords;   collection<user> users = getdummyusers(numberofrows, pageindex, sortcolumnname, sortorderby, out totalrecords);             string output = buildjqgridresults (users, convert.toint32 (numberofrows), convert.toint32 (pageindex), convert.toint32 (totalrecords));             response.write (output);         } 

create users:

 private collection<user> getdummyusers(string numberofrows, string pageindex, string sortcolumnname, string sortorderby, out int totalrecords) {             var data = new collection<user> {                 new user(){ userid = 1,username = "bill gates", firstname = "bill", lastname = "gates",emailid = "test@microsoft.com",  },                 new user(){ userid = 1,username = "bill gates", firstname = "bill", lastname = "gates",emailid = "test@microsoft.com",  },                 new user(){ userid = 1,username = "bill gates", firstname = "bill", lastname = "gates",emailid = "test@microsoft.com",  },             };             totalrecords = data.count;             return data;         } 

convetto json:

 private string buildjqgridresults(collection<user> users, int numberofrows, int pageindex, int totalrecords) {             jqgridresults result = new jqgridresults ();             list<jqgridrow> rows = new list<jqgridrow> ();             foreach (user user in users) {                 jqgridrow row = new jqgridrow ();                 row.id = user.userid;                 row.cell = new string[5];                 row.cell[0] = user.userid.tostring ();                 row.cell[1] = user.username;                 row.cell[2] = user.firstname;                 row.cell[3] = user.lastname;                 row.cell[4] = user.emailid;                 rows.add (row);             }             result.rows = rows.toarray ();             result.page = pageindex;             result.total = (totalrecords + numberofrows - 1) / numberofrows;             result.records = totalrecords;             return new javascriptserializer ().serialize (result);         } 

grid:

     url: 'jqgridhandler.ashx',             datatype: 'json',             autowidth: true,             height: 100,             colnames: ['action', 'id', 'username', 'firstname', 'lastname', 'emailid'],             colmodel: [                          {                              name: 'act', width: 100, align: 'center', sortable: false, formatter: 'actions',                              formatoptions: {                                  keys: true,                                   deloptions: true,                                  delbutton:true,                                  editbutton:false                              }                          },                            { name: 'userid', width: 100, sortable: true, },                         { name: 'username', width: 100, sortable: true },                         { name: 'firstname', width: 100, sortable: true },                         { name: 'lastname', width: 100, sortable: true },                         { name: 'emailid', width: 100, sortable: true },             ],             rownum: 20,             loadonce: true,             rowlist: [5, 10, 20],             recordpos: "left",             ignorecase: true,             toppager: true,             viewrecords: true,             sortorder: "desc",             scrolloffset: 1,         });     }); 

jqgrid treating actions real column , expects data it. easiest way out of add empty cell row on server side:

private string buildjqgridresults(collection<user> users, int numberofrows, int pageindex, int totalrecords) {     jqgridresults result = new jqgridresults ();     list<jqgridrow> rows = new list<jqgridrow> ();     foreach (user user in users) {         jqgridrow row = new jqgridrow ();         row.id = user.userid;         row.cell = new string[6];         row.cell[0] = string.empty;         row.cell[1] = user.userid.tostring ();         row.cell[2] = user.username;         row.cell[3] = user.firstname;         row.cell[4] = user.lastname;         row.cell[5] = user.emailid;         rows.add (row);     }     result.rows = rows.toarray ();     result.page = pageindex;     result.total = (totalrecords + numberofrows - 1) / numberofrows;     result.records = totalrecords;     return new javascriptserializer ().serialize (result); } 

as alternative can use jqgrid in repeatitems: false mode. first need change jqgrid initialization script:

$("#usersgrid").jqgrid({     url: 'jqgridhandler.ashx',     datatype: 'json',     autowidth: true,     height: 100,     colnames: ['action', 'id', 'username', 'firstname', 'lastname', 'emailid'],     colmodel: [         { name: 'act', width: 100, align: 'center', sortable: false, formatter: 'actions', formatoptions: { keys: true, deloptions: true, delbutton:true, editbutton:false } },         { name: 'userid', width: 100, sortable: true, },         { name: 'username', width: 100, sortable: true },         { name: 'firstname', width: 100, sortable: true },         { name: 'lastname', width: 100, sortable: true },         { name: 'emailid', width: 100, sortable: true },     ],     rownum: 20,     loadonce: true,     rowlist: [5, 10, 20],     recordpos: 'left',     ignorecase: true,     toppager: true,     viewrecords: true,     sortorder: 'desc',     scrolloffset: 1,     jsonreader : {          repeatitems: false     } }); 

now on server side can no longer use array, have use object or dictionary. show more generic approach dictionary. assuming solution based on this sample, have change jqgridresult class this:

public class jqgridresults {     public int page;     public int total;     public int records;     public list<dictionary<string, string>> rows; } 

now method can this:

private string buildjqgridresults(collection<user> users, int numberofrows, int pageindex, int totalrecords) {     jqgridresults result = new jqgridresults();     result.rows = new list<dictionary<string, string>>();     foreach (user user in users) {         dictionary<string, string> row = new dictionary<string, string>();         row.add("id", user.userid.tostring());         row.add("userid", user.userid.tostring());         row.add("username", user.username);         row.add("firstname", user.firstname);         row.add("lastname", user.lastname);         row.add("emailid", user.emailid);         result.rows.add(row);     }     result.page = pageindex;     result.total = (totalrecords + numberofrows - 1) / numberofrows;     result.records = totalrecords;     return new javascriptserializer().serialize(result); } 

this can further optimized avoid sending userid twice, not important question point of view.


Comments