我必须显示一些我从服务器收到的数据作为json对象
{"rowndx":"0","rows":"25","rowstotal":"100","rowsdata":[
["00","DEVICE001","T0_IHOME","1","***","1","10"],
["01","DEVICE002","NO_DEVICE","1","***","1","10"],
["02","DEVICE003","NO_DEVICE","0","***","1","10"],
.....
Run Code Online (Sandbox Code Playgroud)
在表格中显示收到的数据之前,我想在必要时进行更改,在数字中添加单位或用单词替换数字(例如0 - > OFF 1-> ON)为此,我在ajax选项中关联"成功"我的编码功能.但是,在这种情况下,"Loading ..."消息始终可见,并且不允许其他操作.我将我的重新编码过程移动到"完整"的ajax选项,这次它似乎工作.但我不明白我的错误是什么,我不知道我的程序是否可行.这是我的表ajax配置
url : "devtbl.json",
mtype : "POST",
datatype : "json",
postData : ......
ajaxGridOptions: {
type : 'post',
contentType: 'application/json',
async : false,
complete : DEVparse_serverdata,
error : function() { alert('Something bad happened. Stopping');},
},
jsonReader : {
root : "tablerows",
page : "currentpage",
total : "totalpages",
records : "totalrecords",
cell : "",
id : "0",
userdata : "userdata",
repeatitems : true
},
Run Code Online (Sandbox Code Playgroud)
和我的编码功能
function DEVparse_serverdata(js , textStatus) {
var jsontablereply = {} ;
var rowsxpage_int = parseInt(UB.rowsxpage.DEVtable) ;
var jsonreply = jQuery.parseJSON(js.responseText) ;
jsontablereply.currentpage = "" + (1 + (parseInt(jsonreply.rowndx) / rowsxpage_int));
jsontablereply.totalpages = "" + parseInt((parseInt(jsonreply.rowstotal) + (rowsxpage_int-1)) / rowsxpage_int) ;
jsontablereply.totalrecords = jsonreply.rowstotal;
jsontablereply.tablerows = [] ;
$.each(jsonreply.rowsdata, function(ndx, row) {
var rowarray = [] ;
rowarray[0] = row[0] ;
rowarray[1] = row[1] ;
rowarray[2] = row[2] ;
rowarray[3] = row[3] ;
rowarray[4] = row[4] ;
switch (row[2]) {
case "NO_DEVICE":
rowarray[5] = "***" ;
break ;
case "T0_IHOME":
rowarray[5] = "T=" + row[5] + "°C" ;
break ;
}
jsontablereply.tablerows[ndx] = rowarray ;
}) ; // each
jQuery("#DEVtbl")[0].addJSONData(jsontablereply);
}
Run Code Online (Sandbox Code Playgroud)
(我是Jquery的初学者,我的编码风格很差)
您必须有许多可能性来实现您的要求.
在许多情况下,可以使用预定义的格式化程序:"select"或formatter: case的"checkbox"0 ->OFF 1-> ON.
另一种可能性是使用自定义格式化程序和unformatter.自定义格式化程序只是回调,jqGrid将在构建相应列的单元格的HTML片段时使用它.如果您需要共同显示某些文本,格式化程序将如下所示
formatter: function (cellValue) { return $.jgrid.htmlEncode(cellValue); }
Run Code Online (Sandbox Code Playgroud)
自定义格式化程序的优点是,您不仅可以对源文本进行多次修改,还可以根据其他列的信息构建单元格(请参阅rawData下面的参数)
formatter: function (cellValue, options, rawData, action) {
// options is the the object defined as
// {rowId:rowId, colModel:cm, gid:gridId, pos:colpos }
// rawData contains the representation of the WHOLE row
// the most problem of the value is that it is in the same
// format as the input data. So if will be array of items
// in your case or if could be XML fragment for the row.
// Additional problem one will have in case of usage of
// loadonce:true. Ath the first load the rawData will be
// array of strings and on later could be named object
// with the properties corresponds to the column names.
// action is typically non-important and is 'add' or 'edit'
}
Run Code Online (Sandbox Code Playgroud)
例如,第5列的自定义格式化程序可以是
formatter: function (cellValue, options, rawData, action) {
switch (rawData[2]) {
case "NO_DEVICE":
return "***";
case "T0_IHOME":
return "T=" + $.jgrid.htmlEncode(cellValue) + "°C" ;
default:
return $.jgrid.htmlEncode(cellValue);
}
}
Run Code Online (Sandbox Code Playgroud)
另一个选择是使用beforeProcessing回调.它大部分都接近您当前代码的逻辑.在beforeProcessing回调内部,您可以在jqGrid 处理数据之前对服务器上的数据进行任何修改.
| 归档时间: |
|
| 查看次数: |
2397 次 |
| 最近记录: |