我必须像这样将我从服务器接收到的一些数据显示为 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"],
.....
在表格中显示接收到的数据之前,我想在必要时进行更改,在数字中添加单位或用单词替换数字(例如 0 ->OFF 1-> ON) 为此,我在 ajax 选项“成功”中关联了我的编码函数。然而,在这种情况下,消息“正在加载...”始终可见,并且不允许执行其他操作。 我将我的重新编码程序移至“完整”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
},
和我的编码函数
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);
}
(我是 Jquery 的初学者,我的编码风格很差)
最佳答案
您可以通过多种方式实现您的要求。
在许多情况下,可以使用预定义的 formatter: "select"或 formatter: "checkbox"对于 0 ->OFF 1-> ON
的情况。
另一种可能性是使用 custom formatter和 unformatter .自定义格式化程序只是回调,jqGrid 在构建相应列的单元格的 HTML 片段期间将使用它。如果你需要一些文本的共同显示,格式化程序看起来像
formatter: function (cellValue) { return $.jgrid.htmlEncode(cellValue); }
自定义格式化程序的优点是您不仅可以对源文本进行任何修改,还可以根据其他列的信息构建单元格包含(参见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'
}
例如,第 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);
}
}
另一种选择是使用 beforeProcessing
回调。它大多接近你当前代码的逻辑。在 beforeProcessing
回调中,您可以在 jqGrid 处理数据之前对服务器返回的数据进行任何修改。
关于ajax - jqgrid 在表中显示之前修改从 ajax 调用返回的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13535429/