javascript - jqGrid - 将数据库记录嵌套属性设置为行 ID

标签 javascript json jqgrid

在我的网格中,我配置了 jsonReader“id”属性来获取我的数据库记录作为行 ID。 像这样:

JSON:

{
    id: 1,
    price: 99.99,
    data: {
        dataId: 2
    }
}

JSON 阅读器

 ...
 jsonReader: {
     root: 'list',
     total: 'count',
     id: 'id' 
 },
 ...

如果我想将“dataId”设置为行 ID 怎么办?我尝试通过在字符串中使用点表示法来定义它,但它不起作用。这是我的网格选项:

var gridOptions = {
    height: 250,                
    colNames: ['Nome', 'Preço'],
    colModel: [{
        name:'ipvodAsset.title',
        index:'ipvodAsset.title', 
        width: 200, 
        sorttype:'string', 
        searchoptions: { sopt:['eq','ne','lt','le','gt','ge','bw','bn','in','ni','ew','en','cn','nc'] }
    }, {
        name:'price',
        index:'price', 
        width:200, 
        sorttype:'currency', 
        formatter: 'currency',
        searchoptions: { sopt:['eq','ne','gt','ge','bw','bn','cn','nc'] }
    }],
    jsonReader: { id: 'data.data.id' },
    prmNames: {'order': 'order'},
    gridView: true,
    sortorder: 'asc',
    sortname: 'title',
    viewrecords : true,
    rowNum: 10,
    rowList:[10,20,30],
    altRows: true,
    dataType="local",
    data = [
        { "id": 1, "price": 99.99, "data": { "dataId": 2 } },
        { "id": 2, "price": 99.99, "data": { "dataId": 3 } },
        { "id": 3, "price": 99.99, "data": { "dataId": 4 } },
        { "id": 4, "price": 99.99, "data": { "dataId": 5 } }
    ]
};

最佳答案

首先我应该提到 JSON 和对象初始化之间存在很大差异。语法

var data = [
    { "id": 1, "price": 99.99, "data": { "dataId": 2 } },
    { "id": 2, "price": 99.99, "data": { "dataId": 3 } },
    { "id": 3, "price": 99.99, "data": { "dataId": 4 } },
    { "id": 4, "price": 99.99, "data": { "dataId": 5 } }
];

声明变量data并初始化它。变量data 的类型为object。由于所有属性 idpricedata 的名称不包含空格、点和其他特殊字符,因此可以将上面的代码重写为

var data = [
    { id: 1, price: 99.99, data: { dataId: 2 } },
    { id: 2, price: 99.99, data: { dataId: 3 } },
    { id: 3, price: 99.99, data: { dataId: 4 } },
    { id: 4, price: 99.99, data: { dataId: 5 } }
];

另一种构造

var myJsonString = '[ { "id": 1, "price": 99.99, "data": { "dataId": 2 } }, { "id": 2, "price": 99.99, "data": { "dataId": 3 } }, { "id": 3, "price": 99.99, "data": { "dataId": 4 } }, { "id": 4, "price": 99.99, "data": { "dataId": 5 } } ]';

声明并初始化string类型的变量myJsonString(!!!)。使用 jQuery.parseJSON 可以轻松地将字符串 myJsonString 转换为对象 dataJSON.parse甚至使用 eval JavaScript 的函数。

因此,myJsonString 初始化右侧使用的字符串是 JSON 字符串,但不是第一个示例中的初始值设定项。

您发布的代码如 dataType="local" 而不是 datatype: "local"gridView: true 而不是 gridview:true。例如,如果您使用 dataType: "local" 而不是 datatype: "local",则该选项将被忽略,并使用默认的 "xml"将使用datatype 的值。它不会是您所需要的。

尽管如此,问题依然存在:如何读取此类数据并从 data.dataId 分配 rowid 值。

因为您使用datatype: "local",所以需要使用localReader而不是jsonReader:

localReader: { id: "data.dataId" }

而不是

jsonReader: { id: 'data.data.id' }

在您的代码中使用。

对应的demo为here 。显示

enter image description here

您可以轻松验证(使用 Chrome、Internet Explorer 或 Firefox 的开发人员工具)网格的 rowid 是 2,3,4,5 而不是 1,2,3,4:

enter image description here

关于javascript - jqGrid - 将数据库记录嵌套属性设置为行 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27848321/

相关文章:

javascript - 如何使用 JavaScript 获取您在 Twitter 上关注的人数?

json - 有条件地将 JSON 解码为结构的干净方法

jquery - 为什么 editurl : 'clientData' when deleting a second row from jqGrid? 出现 404 错误

jquery - 如何检测 jqGrid 的大小已调整?

JavaScript 'function undefined' 使用 jsfiddle 时出错

javascript - 在 Chart JS 中仅显示饼图中那些大于 5% 的百分比的标签

java - Android 中的 JSON 对象

php - 尝试在本地打开 PHP 应用程序

php - 具有高级(列)过滤的 CRUD?

javascript - d3.js 散布饼图标签