使用多级JSON数据加载jqGrid子网格

时间:2018-07-19 18:09:31

标签: jqgrid free-jqgrid

我有一个jqGrid可以从API加载子网格数据。子网格中的所有内容都可以正常加载,除了第二层深度的数据。

在我的主网格配置中,与子网格相关的选项设置为:

def get_order(_list):
    order = _list[0]
    for sublist in _list[1:]:
        if not sublist:
            continue
        if len(sublist) == 1:
            if sublist[0] not in order:
                order.append(sublist[0])
            continue
        new_order = order.copy()
        for index, value in enumerate(sublist):
            inserted = False
            new_order_index = None
            if value in new_order:
                new_order_index = new_order.index(value)
                new_order.remove(value)
            for previous_value in sublist[:index][::-1]:
                if previous_value in new_order:
                    insert_index = new_order.index(previous_value) + 1
                    print('inserting', value, 'at position', insert_index, 'after', previous_value)
                    new_order.insert(insert_index, value)
                    inserted = True
                    break
            if inserted:
                continue
            for next_value in sublist[index:]:
                if next_value in new_order:
                    insert_index = new_order.index(next_value)
                    print('inserting', value, 'at position', insert_index, 'before', next_value)
                    new_order.insert(insert_index, value)
                    inserted = True
                    break
            if inserted:
                continue
            if new_order_index is None:
                print('appending', value)
                new_order.append(value)
            else:
                print('leaving', value, 'at position', new_order_index)
                new_order.insert(new_order_index, value)
        order = new_order
    return order

if __name__ == '__main__':
    test_list = [['b', 'c'], ['a', 'c'], ['b', 'a'], ['a', 'c', 'd'], ]
    order = get_order(test_list)
    #>>> inserting a at position 1 before c
    #>>> inserting c at position 2 after a
    #>>> inserting d at position 3 after c
    #>>> inserting a at position 1 before c
    #>>> inserting c at position 2 after a
    #>>> inserting b at position 0 before a
    #>>> inserting a at position 1 after b
    print(order)
    #>>> ['b', 'a', 'c', 'd']

正在为子网格加载的数据是

    jsonReader: {
      // instruct subgrid to get the data as name:value pair
      subgrid: {
        repeatitems: false
      }
    },
    subGrid: true,
    subGridUrl: 'http://localhost:8081/api/addendums/',
    subGridModel: [{
      name: ['staff name', 'CDCR Staff', 'CDCR Staff Phone', 'notificationDate', 'comment'],
      mapping: ['staff.name', 'CDCRStaff', 'CDCRStaffPhone', 'notificationDate', 'comment'],
      width: [10, 55, 200, 80, 80]
    }],
    subGridOptions: {
      // configure the icons from theme rolloer
      plusicon: "fa fa-plus",
      minusicon: "fa fa-minus",
      openicon: ""
    }

子网格的CDCRStaff,CDCRStaffPhone,notificationDate和comment属性很好,但是它不能映射到staff对象内部的name属性。这是子网格功能的限制还是设置有误?

1 个答案:

答案 0 :(得分:0)

我建议您更改数据格式,以使项目具有扁平结构,并在mapping中不使用点号分隔的名称。仅当您无法更改数据时,才可以在定义为函数的jsonReader.subgrid.root中规范输入数据。相应的代码可能与以下内容有关:

jsonReader: {
    subgrid: {
        root: function (data) {
            data.rows.forEach(function (item) {
                if (item.staff != null) {
                    item["staff.name"] = item.staff.name;
                }
            });
            return data.rows;
        },
        repeatitems: false
    }
}