麻烦在jqgrid上显示json数据并进行格式化 [英] trouble to displaying json data on jqgrid and formatting

查看:284
本文介绍了麻烦在jqgrid上显示json数据并进行格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我需要在jqgrid上显示json数据.我得到的数据具有以下格式:

I have a problem where I need to display json data on a jqgrid. The data I get is of the following format:

{"data":{"data":"\tat org.aaa.aaa.aaa.aaa.aaa.aaa(aaa.java:512)[147:org.aaa.aaa.aaa:9.1.1]\n\tat aaa.aaa.aaa.aaa.aaa.aaa(aaa.java:1789)[146:org.aaa:9.1.1]\n"}}

我显示数据的javascript是:

My javascript to display the data is:

 $("#grid").jqGrid({
  url: '/getdata',
      datatype: "json",
  mtype: "GET",
  colNames:['data'],
  colModel:[
     {name:'data', index:'data', align:'center'}
  ],
   jsonReader : {
          repeatitems: false,
      id: "0",
      cell: "",
       root: "logs",
      page: function() { return 1; },
      total: function() { return 1; },
      records: function(obj) { return obj.length; }
    },
   loadonce: true,      
   viewrecords: true,
   autowidth: true,
   multiselect: false,
   ignoreCase: true,
   sortable: true,
   height: 600, 
   rowNum: 999
 });

我尝试了几种组合,但是使用此代码无法将数据显示在jqgrid上. jqgrid显示一个空表.我想我在这里错过了一些东西.

I tried a few combinations, but could not get the data to be displayed on the jqgrid with this code. The jqgrid displays an empty table. I guess I am missing something here.

我还必须格式化数据,以便每次我们点击'\ n'时,都将其显示在新行中.我想我可以在格式化程序的列中使用"addrowdata"来做到这一点.是吗?

I also have to format the data so that every time we hit '\n', we display it in a new row. I guess I can use 'addrowdata' in the formatter for the column to do that. Is that right?

任何指针都将不胜感激.

Any pointers are greatly appreciated.

谢谢

阿莎

推荐答案

我不建议您使用addRowData.使用beforeProcessing将从服务器返回的数据修改为jqGrid可以读取的格式更为有效.例如,您可以将data.data部分除以\n并填充相应项的数组:

I don't recommend you to use addRowData. It's more effectively to use beforeProcessing to modify the data returned from the server to the format which jqGrid can read. You can for example to split the data.data part by \n and fill the array of the corresponding items:

autoencode: true,
beforeProcessing: function (data) {
    var items = data.data.data.split("\n"), i, l, item;
    data.logs = [];
    for (i = 0, l = items.length; i < l; i++) {
        item = $.trim(items[i]);
        if (item.length > 0) {
            data.logs.push([item]);
        }
    }
}

我在上面的代码中包含了 jQuery.trim 的调用,以删除不需要的或每行开头或末尾的其他空格字符.

I included the call of jQuery.trim in the above code to remove unneeded \t or other white space characters at the beginning or at the end of every line.

我另外添加了autoencode: true选项,强烈建议您选择该选项,以确保包含HTML特殊字符的文本(例如<>&等)将正确显示在网格.

I included additionally autoencode: true option which is strict recommended to be sure that the texts which included special characters for HTML (like <, >, & and so on) will be correctly displayed inside of the grid.

此外,您还应该将jsonReader更改为例如以下

Additionally you should change jsonReader to for example the following

jsonReader: {
    root: "logs",
    cell: "",
    id: function () {
        return function () {
            return $.jgrid.randId();
        }
    },
    page: function() { return 1; },
    total: function() { return 1; },
    records: function(obj) { return obj.logs.length; }
}

id的值似乎很棘手,但是该实现为每一行的id属性生成了真正唯一的值.

The value of id seems tricky, but the implementation generate really unique values for id attribute of every line.

您将在此处找到相应的演示.

The corresponding demo you will find here.

这篇关于麻烦在jqgrid上显示json数据并进行格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆