jqGrid没有显示我的数据?

时间:2013-11-11 18:45:31

标签: jquery jqgrid

这就是我的数据结构:

[
  {
    "email": "ww@ww.com",
    "name": "ww",
    "password": "qLfsebHMKv7dNgExtR",
    "active": false,
    "role": "admin",
    "createdAt": "2013-10-22T11:48:32.719Z",
    "updatedAt": "2013-10-22T11:48:32.719Z",
    "id": "52666610a6a311308b000001"
  },
  {
    "email": "qq@qq.com",
    "name": "QQ",
    "password": "twfubGHoQkYDVup",
    "active": true,
    "role": "expert",
    "createdAt": "2013-10-22T11:38:47.578Z",
    "updatedAt": "2013-10-22T11:38:47.578Z",
    "id": "526663c788101c9f89000001"
  }
]

这是我的js:

$(function () {
    $('#list').jqGrid({
      url: '/api/v1/users',
      datatype: 'json',
      mtype: 'GET',
      colNames: ['id','email','name', 'password', 'active','role','createdAt','updatedAt'],
      colModel: [
      { name: 'id', width: 80 },
      { name: 'email', width: 80 },
      { name: 'name', width: 80, align: 'right' },
      { name: 'password', width: 80, align: 'right' },
      { name: 'active', width: 80, align: 'right' },
      { name: 'role', width: 80, align: 'right' },
      { name: 'createdAt', width: 80, align: 'right' },
      { name: 'updatedAt', width: 80, sortable: false }
      ],
      jsonReader: { 
        repeatitems: false,
        id: "id",
        root: function (obj) { return obj; },
        page: function (obj) { return 1; },
        total: function (obj) { return 1; },
        records: function (obj) { return obj.length; }
      },
      pager: '#pager',
      rowNum: 10,
      rowList: [10, 20, 30],
      sortname: 'invid',
      sortorder: 'desc',
      viewrecords: true,
      gridview: true,
      autoencode: true,
      caption: 'My first grid'
    }); 
  }); 

我根本无法工作,更糟糕的是禁用整个页面! 我在这里错过了什么?


更新1: 我补充说:

  loadComplete: function (data) {
    console.log("OK");
    console.log(data);
  },
  loadError: function (jqXHR, textStatus, errorThrown) {
    console.log('HTTP status code: ' + jqXHR.status + 'n' +
    'textStatus: ' + textStatus + 'n' +
    'errorThrown: ' + errorThrown);
    console.log('HTTP message body (jqXHR.responseText): ' + 'n' + jqXHR.responseText);
  }

loadComplete 始终返回确定,但数据是一个空数组 []


更新2: 我检查了jqGrid发送的请求,并将其附加到我的API:

/api/v1/users?_search=false&nd=1384254700817&rows=20&page=1&sidx=&sord=asc

当我点击这个url时,它会返回那个empaty数组。知道如何改变这种行为吗?

1 个答案:

答案 0 :(得分:0)

在加载/加载网格时禁用页面意味着存在一些CSS问题。

首先,检查是否添加了ui.jqgrid.css。阻止可能发生在没有包含它的情况下。

您能够点击网址吗?

网格中的另一个错误是:Sortname。您已根据列模型指定了“invid',这是一个无效的列名。”

所以,纠正它。如果您使用的是最新版本的Jqgrid。无需包含以下代码:

jsonReader: { 
        repeatitems: false,
        id: "id",
        root: function (obj) { return obj; },
        page: function (obj) { return 1; },
        total: function (obj) { return 1; },
        records: function (obj) { return obj.length; }
      },

你可以忽略上面的代码...... Jqgrid会照顾它。

如果您需要用户进行排序和分页,则需要指定LoadOnce:true

这将使您的网格数据成为本地数据,排序和分页工作。

如果您没有指定,则需要注意排序,过滤和分页。

跳这有助于......

**UPdated:**


[
  {
    "email": "ww@ww.com",
    "name": "ww",
    "password": "qLfsebHMKv7dNgExtR",
    "active": "false",
    "role": "admin",
    "createdAt": "2013-10-22T11:48:32.719Z",
    "updatedAt": "2013-10-22T11:48:32.719Z",
    "id": "52666610a6a311308b000001"
  },
  {
    "email": "qq@qq.com",
    "name": "QQ",
    "password": "twfubGHoQkYDVup",
    "active":"true",
    "role": "expert",
    "createdAt": "2013-10-22T11:38:47.578Z",
    "updatedAt": "2013-10-22T11:38:47.578Z",
    "id": "526663c788101c9f89000001"
  }
]
相关问题