Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 序列化jqGrid';s将数据编辑为JSON_Jquery_Json_Jqgrid - Fatal编程技术网

Jquery 序列化jqGrid';s将数据编辑为JSON

Jquery 序列化jqGrid';s将数据编辑为JSON,jquery,json,jqgrid,Jquery,Json,Jqgrid,在用户在jqGrid中编辑数据之后,我想将格式化的JSON字符串返回给服务器以供使用。 以下是服务器期望接收的内容示例: { "aptitudeArrayList":[ { "skill":"VITS", "value":2, "id":2, "color":"RED" }, { "skill":"GBFM", "value":6, "id":1, "color":"GRE

在用户在jqGrid中编辑数据之后,我想将格式化的JSON字符串返回给服务器以供使用。 以下是服务器期望接收的内容示例:

{
  "aptitudeArrayList":[
    {
      "skill":"VITS",
      "value":2,
      "id":2,
      "color":"RED"
    },
    {
      "skill":"GBFM",
      "value":6,
      "id":1,
      "color":"GREEN"
    },
    {
      "skill":"JSON",
      "value":4,
      "id":3,
      "color":"RED"
    },
    {
      "skill":"CASTLEROCK",
      "value":7,
      "id":4,
      "color":"RED"
    }
  ],
  "cell":12412,
  "line":"FIRST_LINE",
  "name":"Barop",
  "id":1,
  "region":"The end",
  "available":true
}
以下是我的ColModel的一部分:

...
{
    name:'cell',
    label:'Cell #',
    width:80,
    align:'center',
    editable:true,
    editrules:{required:true, integer:true},
    editoptions:{size:10, maxlength:10}
},
{
    name:'available',
    label:'Available',
    formatter:'checkbox',
    width:46,
    align:'center',
    editable:true,
    edittype:'checkbox',
    editoptions:{value:"true:false"}
},
{
    name:"vits.value",
    label:'VITS',
    width:300,
    align:'center',
    editable:true,
    jsonmap:"aptitudeArrayList.0.value"
},
{
    name:"vits.color",
    editable:true,
    jsonmap:"aptitudeArrayList.0.color"
}
...
以下是编辑选项:(我注释掉了postdata,因为它现在还不需要)

调用编辑后,数据将发送到此函数:

$.extend($.jgrid.edit, {
    closeAfterEdit:true,
    closeAfterAdd:true,
    ajaxEditOptions:{ contentType:"application/json" },
    mtype:'PUT',
    serializeEditData:function (data) {
        delete data.oper;
        return JSON.stringify(data);
    }
});
上面的代码返回此JSON:

{
  "id":"1",
  "name":"Barop",
  "region":"The end",
  "line":"FIRST_LINE",
  "cell":"12412",
  "available":"true",
  "vits.value":"2",
  "vits.color":"RED"
  ...
}
当我附加调试器时,我发现
数据
包含:
数据对象{id=“1”、name=“Barop”、region=“the end”、…vits.value=“2”、vits.color=“RED”}
似乎是从jqGrid中的
名称
属性获取密钥


如何将数据序列化为服务器所需的格式?

在我看来,解决问题的最简单方法似乎是修改您使用的
SerializedEditData
。它可以是关于以下内容

serializeEditData: function (data) {
    return JSON.stringify({
        id: parseInt(data.id, 10),
        name: data.name,
        line: data.line,
        cell: parseInt(data.cell, 10),
        available: data.available === 'true',
        aptitudeArrayList: [
            value: data.value,
            color: data.color
        ]
    });
}
此外,我建议您最好不要在
name
属性中使用点或任何其他。您可以将
name:“vits.value”
重命名为
name:“value”
或使用其他
索引
属性

name: "value",
index: "vits.value",
jsonmap: "aptitudeArrayList.0.value"

这正是我需要的,谢谢!通读你对其他问题的回答对我也有很大帮助。@Jaym:不客气!我很高兴看到我的其他答案也能帮助你。
name: "value",
index: "vits.value",
jsonmap: "aptitudeArrayList.0.value"