子网格的JSON格式

时间:2013-04-30 09:23:12

标签: json jqgrid

你好,任何人都可以告诉我包含子网格值的本地数据的json格式。 我使用了以下格式,但子网格数据未加载到子网格表中。

var data=[{"Left_ssr_id":CaGM33098,"Mismatches":"6","Gaps":"1","AlignLen":"1119","Identity":"99.37","right_ssr_start":17308507,"Qstart":"33","query":"AB-Contig743","subject":"Ca7","Send":"17305359","Score":"2113","Evalue":"0.0","Left_ssr_end":17293825,"right_ssr_end":17308542,"right_ssr_id":CaGM33099 ,"Sstart":"17304241","Qend":"1150","Left_ssr_start":17293811,subgrid:[{"Right_ssr_start":17308507,"Right_ssr_end":17308542},{"Right_ssr_start":17308506,"Right_ssr_end":17308543}]}]

用于上传数据的代码。 Grid正在获取数据,但子网格仅显示标题和空行。

$(document).ready(function(){
$("#blastform").submit(function(){
     jQuery.ajax({
         data: $(this).serialize(),
         url: this.action,
         type: this.method,
     dataType: "json",
         error: function() {
             $(thistarget).html("<span class='error'>Failed to submit form!</span>");
         },
         success: function(res) {
    var data=res;
$("#datatable").jqGrid({
    datatype:"local",
    data:data,
    colNames:['QueryID','SubjectID', . . . 'Right_nearest_distance'],
    colModel:[ {name:'query',index:'query', width:55},
        {name:'subject',index:'subject', width:80},
        .,
        .,
        {name:'right_near_dist',index:'right_near_dist', width:50, sortable:false}, ],
    rowNum:100,
    rowList:[100,200,500],
    pager: $('#pager2'),
    sortname: 'QueryID',
    viewrecords: true,
    sortorder: 'desc',
    loadonce: true,
    altRows: true,
    pgbuttons: true,
    subGrid : true,
    subGridUrl: 'local',
    subGridRowExpanded: function(subgrid_id, row_id)
    {
    var subgrid_table_id, pager_id;
    subgrid_table_id = subgrid_id+"_t";
    pager_id = "p_"+subgrid_table_id;
    $("#"+subgrid_id).html("<table id='"+subgrid_table_id+"' class='scroll'></table><div id='"+pager_id+"' class='scroll'></div>");
    jQuery("#"+subgrid_table_id).jqGrid({
    datatype: "local",
    colNames: ['ssr_start','ssr_end'],
    colModel: [ {name:"Right_ssr_start",index:"Right_ssr_start",width:80,key:true},
            {name:"Right_ssr_end",index:"Right_ssr_end",width:130},
             ], 
    rowNum:20,
    pager: pager_id,
    sortname: 'ssr_start',
    sortorder: "asc",
    height: '100%' });
    jQuery("#"+subgrid_table_id).jqGrid('navGrid',"#"+pager_id,{edit:false,add:false,del:false}) },
    caption: 'Blast Results'
    });
    $("#datatable").jqGrid('navGrid','#pager2', {position: 'right'});
    jQuery("#datatable").jqGrid('navGrid','#blastresults',{edit:false,add:false,del:false});

         }
     })
     return false;
     }
);
);

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我不建议您进行单独的ajax调用并使用datatype: "local", data: data。你会有很多缺点。 jqGrid为案例提供datatype: "json"类型。可以使用其他选项(如jsonReaderjsonmap来通知jqGrid如何从服务器响应中获取有关网格数据的信息。

您发布的var data的值提供了错误的数据。您使用未定义的变量CaGM33098CaGM33099。如果您使用服务器响应中的cut&amp; pased,则会在服务器上生成错误的JSON响应。此外,如果subgrid数组中的项目未包含在data中,则"会很奇怪。服务器可能会从this.action(这里是this ???)返回以下数据

[
    {
        "Left_ssr_id": "CaGM33098",
        "Mismatches": "6",
        "Gaps": "1",
        "AlignLen": "1119",
        "Identity": "99.37",
        "right_ssr_start": 17308507,
        "Qstart": "33",
        "query": "AB-Contig743",
        "subject": "Ca7",
        "Send": "17305359",
        "Score": "2113",
        "Evalue": "0.0",
        "Left_ssr_end": 17293825,
        "right_ssr_end": 17308542,
        "right_ssr_id": "CaGM33099",
        "Sstart": "17304241",
        "Qend": "1150",
        "Left_ssr_start": 17293811,
        "subgrid": [
            {
                "Right_ssr_start": 17308507,
                "Right_ssr_end": 17308542
            },
            {
                "Right_ssr_start": 17308506,
                "Right_ssr_end": 17308543
            }
        ]
    }
]

The old answerthe answer显示了如何实现将嵌套数据显示为子网格。 Another answer显示了如何使用userdata直接从服务器加载嵌套数据 。我建议你使用最后一种方法。