简体   繁体   English

jqgrid子网格如何在子网格中显示json值

[英]jqgrid subgrids how to display json values in subgrid

I want to use jqgrid with subgrids.我想将 jqgrid 与子网格一起使用。 I have created a jqgrid table.我创建了一个 jqgrid 表。 However, I want to display the descriptions and symbols in the subgrid .但是,我想在 subgrid 中显示描述和符号。

I have also included JSOn data and my code snippet and the fiddle demo here我还包括了 JSOn 数据和我的代码片段以及这里小提琴演示

Can I create subgrids from the following JSON data with jqgrid with out having nested JSON data?我可以使用 jqgrid 从以下 JSON 数据创建子网格而不使用嵌套的 JSON 数据吗? Is there any example or demo I can refer to?有没有我可以参考的示例或演示?

$(document).ready(function() {
    var jsonData = {
      "Name": "Julie Brown",
      "Account": "C0010",
      "LoanApproved": "12/5/2015",
      "LastActivity": "4/1/2016",
      "PledgedPortfolio": "4012214.00875",
      "MaxApprovedLoanAmt": "2050877.824375",
      "LoanBalance": "1849000",
      "AvailableCredit": "201877.824375",
      "Aging": "3",
      "Brokerage": "My Broker",
      "Contact": "Robert L. Johnson",
      "ContactPhone": "(212) 902-3614",
      "RiskCategory": "Yellow",
      "rows": [{
        "ClientID": "C0010",
        "Symbol": "WEC",
        "Description": "Western Electric Co",
        "ShareQuantity": "20638",
        "SharePrice": "21.12",
        "TotalValue": "435874.56",
        "LTVCategory": "Equities",
        "LTVRatio": "50%",
        "MaxLoanAmt": "217937.28"
      }, {
        "ClientID": "C0010",
        "Symbol": "BBB",
        "Description": "Bins Breakers and Boxes",
        "ShareQuantity": "9623",
        "SharePrice": "74.29125",
        "TotalValue": "714904.69875",
        "LTVCategory": "Equities",
        "LTVRatio": "50%",
        "MaxLoanAmt": "357452.349375"
      }, {
        "ClientID": "C0010",
        "Symbol": "GPSC",
        "Description": "Great Plains Small Cap Stock",
        "ShareQuantity": "49612",
        "SharePrice": "14.24",
        "TotalValue": "706474.88",
        "LTVCategory": "Mutual Funds - Small Cap",
        "LTVRatio": "40%",
        "MaxLoanAmt": "282589.952"
      }]
    },
    mmddyyyy = "";
  /*********************************************************************/


  $("#output").jqGrid({
    url: "/echo/json/",
    mtype: "POST",
    datatype: "json",
    postData: {
          json: JSON.stringify(jsonData)
    },
    colModel: [
      /**    { name: 'ClientID', label:'ClientID',width: 80, key: true },****/
      {
        name: 'Symbol',
        width: 65
      }, {
        name: 'Description',
        width: 165
      }, {
        name: 'ShareQuantity',
        align: 'right',
        width: 85,
        classes: "hidden-xs", labelClasses: "hidden-xs",
        formatter: 'currency',
        formatoptions: {
          prefix: " ",
          suffix: " "
        }
      }, {
        name: 'SharePrice',
        label: 'Share Price',
        align: 'right',
        width: 100,
        classes: "hidden-xs", labelClasses: "hidden-xs",
        template: "number",
        formatoptions: {
          prefix: " $",
          decimalPlaces: 4
        }
      },
      /*{ label: 'Value1', 
                  name: 'Value1', 
                  width: 80, 
                  sorttype: 'number', 
                  formatter: 'number',
                  align: 'right'
              }, */
      {
        name: 'TotalValue',
        label: 'Total Value',
        width: 160,
        sorttype: 'number',
        align: "right",
        formatter: 'currency',
        formatoptions: {
          prefix: " $",
          suffix: " "
        }
      }, {
        name: 'LTVRatio',
        label: 'LTV Ratio',
        width: 70,
        sorttype: 'number',
        align: "right",
        formatter: 'percentage',
        formatoptions: {
          prefix: " ",
          suffix: " "
        }
      }, {
        name: 'LTVCategory',
        label: 'LTV Category',
        classes: "hidden-xs", labelClasses: "hidden-xs",
        width: 120,
        width: 165
      },

      {
        name: 'MaxLoanAmt',
        label: 'MaxLoanAmount',
        width: 165,
        sorttype: 'number',
        align: "right",
        formatter: 'currency',
        formatoptions: {
          prefix: " $",
          suffix: " "
        }
      }

    ],
    additionalProperties: ["Num1"],
    /*beforeProcessing: function (data) {
        var item, i, n = data.length;
        for (i = 0; i < n; i++) {
            item = data[i];
            item.Quantity = parseFloat($.trim(item.Quantity).replace(",", ""));
            item.LTVRatio = parseFloat($.trim(item.LTVRatio *10000).replace(",", ""));
            item.Value = parseFloat($.trim(item.Value).replace(",", ""));
            item.Num1 = parseInt($.trim(item.Num1).replace(",", ""), 10);
            item.Num2 = parseInt($.trim(item.Num2).replace(",", ""), 10);
        }
    }, */
    iconSet: "fontAwesome",
    loadonce: true,
    rownumbers: true,
    cmTemplate: {
      autoResizable: true,
      editable: true
    },
    autoResizing: {
      compact: true
    },
    autowidth: true,
    height: 'auto',
    forceClientSorting: true,
    sortname: "Symbol",
    footerrow: true,
    caption: "<b>Collateral Value</b> <span class='pull-right' style='margin-right:20px;'>Valuation as of: " + mmddyyyy + "</span>",


    loadComplete: function() {
      var $self = $(this),
        sum = $self.jqGrid("getCol", "Price", false, "sum"),
        sum1 = $self.jqGrid("getCol", "MaxLoanAmt", false, "sum");
      //ltvratio =  $self.jqGrid("getCol","LTVRatio:addas", "Aved Loan Amount");
      $self.jqGrid("footerData", "set", {
        LTVCategory: "Max Approved Loan Amount:",
        Price: sum,
        MaxLoanAmt: sum1
      });
    }
  });

  $("#output").jqGrid('filterToolbar', {stringResult: true, searchOnEnter: false, defaultSearch : "cn"});


    $(window).on("resize", function () {
        var newWidth = $("#output").closest(".ui-jqgrid").parent().width();
        $("#output").jqGrid("setGridWidth", newWidth, true);
    });
});

If I correctly understand your requirements, it's relatively easy.如果我正确理解您的要求,那就相对容易了。 If you want to display "Symbol" and "Description" in subgrid, then you would like to remove the corresponding columns from the main grid.如果要在子网格中显示"Symbol""Description" ,则需要从主网格中删除相应的列。 You use loadonce: true option to fill the local data with the data return from the server.您使用loadonce: true选项从服务器返回的数据填充本地data The item, which represent every row of input data, will be filled with columns and the properties from additionalProperties option.该项目代表输入数据的每一行,将填充列来自additionalProperties选项的属性。 Thus you should add因此你应该添加

additionalProperties: ["Symbol", "Description"]

after removing "Symbol" and "Description" from the columns.从列中删除"Symbol""Description"后。

Now you should add subGrid: true option to create the "subrgid" column with "+" symbol, which allows to open the grid.现在你应该添加subGrid: true选项来创建带有“+”符号的“subrgid”列,它允许打开网格。 On opening jqGrid create the div for the subrgid and the callback subGridRowExpanded is responsible to fill the grid with data.打开 jqGrid 时,为 subrgid 创建 div,回调subGridRowExpanded负责用数据填充网格。 One can create subgrid inside of the div, but one can place any HTML fragments in any form.可以在 div 内创建子网格,但可以以任何形式放置任何 HTML 片段。 For example例如

subGridRowExpanded: function (subgridDivId, rowid) {
    var item = $(this).jqGrid("getLocalRow", rowid);
    $("#" + $.jgrid.jqID(subgridDivId)).html("Symbol: <em>" + item.Symbol +
        "</em><br/>Description: <em>" + item.Description + "</em>");
}

The resulting demo https://jsfiddle.net/OlegKi/615qovew/75/ displays the data like on the picture below结果演示https://jsfiddle.net/OlegKi/615qovew/75/显示如下图所示的数据

在此处输入图片说明

You are absolutely free in the design of information displayed in the "subgrid"-div.您可以完全自由地设计“subgrid”-div 中显示的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM