【问题标题】:Adding Custom rows in View model in jqGrid在 jqGrid 的视图模型中添加自定义行
【发布时间】:2014-04-13 07:48:38
【问题描述】:

我正在尝试在视图模型中显示订单的订单详情。单击订单和view 传呼机按钮传递orderID。使用那个orderID,我在另一个local json array 中搜索。我正在制作一个包含搜索结果数组值的表,并将其添加为已在视图模型中使用的行之间的新行,如下图所示。

(从上图中,我颜色边框的行是新创建的行。其他行默认由jqGrid创建。)

我成功创建了其他行。但是在视图模型中单击视图下一个或上一个记录按钮时会带来一些麻烦。见下图,

(不同行的表列值重复追加到一行)

在此我通过下一个 (#nData) 和上一个 (#pData) 按钮导航记录 8 次。在执行此操作时,我的客户行文本“订单详情”的左侧<td> 和下一个自动生成的row's(总计)右侧<td> 的文本重复8 次。我每次点击下一个或上一个,它都会在我的自定义行之后附加新行。

这是我的代码,

  var myData2 = <?php echo json_encode($oParticularResult) ?>;
  //Here I'm adding custom row
  var showIdRow = function ($form) {
  var $this = $(this),
  rowid = $this.jqGrid("getGridParam", "selrow");
  var addHTML = '<tr class="FormData" id="trv_particulars"><td class="CaptionTD form-view-label ui-widget-content" 

width="30%"><b>Order Particulars</b></td>';
  addHTML += '<td class="DataTD form-view-data ui-helper-reset ui-widget-content" id="v_tot"><span>';
    addHTML += "<table border='2px'><thead><tr><td>Sl.No.</td><td>Particulars 

Name</td><td>Quantity</td><td>Amount</td></tr></thead><tbody>";
    var count = 1;
  for (i in myData2)
  {
  if(myData2[i].orderID == rowid)
  {
     addHTML += "<tr><td>"+count+"</td><td>"+myData2[i].proName+"</td><td>"+myData2[i].proQuantity+"</td><td>"+myData2

[i].proPrice+"</td></tr>";
     count++;
  }
    }
    $(addHTML).insertAfter('#trv_order_dt');
    };
    $(function () {
    var myData = <?php echo json_encode($orderResult) ?>;
  var getCellVal;
  var sel_id;
  $("#orderGrid").jqGrid({
  caption:'Order List',
  datatype:'local',
  data:myData,
  mtype:'POST',
  width:777,
  rowNum:10,
  height:100,
  sortorder:'asc',
  rowList:[10,20,30],
  rownumbers:true,
  viewrecords:true,
  gridview:false,
  autoencode:true,
  loadonce:true,
  pager:'#orderPager',
  sortname:'orderID',

  editurl:'<?php echo $this->action('editStatus'); ?>',
  colNames:['Order Number','Customer Name','Date', 'Total', 'Paid','Balance'],
  //cmTemplate:{editable:true, editrules: {required:true}},
  colModel:[
  { name:'orderID', key:true, width:30 },
  { name: 'custName', index: 'custName', width:60 },
  { name: 'order_dt', index: 'order_dt', width:60 },
  { name: 'tot', index: 'tot', width:60 },
  { name: 'amount', index: 'amount', width:60 },
  { name: 'bal', index: 'bal', width:60 }],
  }).navGrid("#orderPager",
  {add:false, edit:false, view:true, del:false, search:true, refresh:true },
  {},//edit
  {jqModal: true, viewPagerButtons: true, checkOnUpdate:true, savekey: [true,13], navkeys: [false,38,40], checkOnSubmit : 

true, reloadAfterSubmit:false, closeOnEscape:true, closeAfterEdit: true,width:600, height:300}, //add
  {jqModal: true, reloadAfterSubmit:true, closeOnEscape: true,width:600, height:300}, //del
  {jqModal: true, closeAfterSearch: true, closeOnEscape: true, multipleSearch: true, multipleGroup:false, showQuery: true, 

overlay: true, recreateFilter: true,width:600, height:300 }, //search
  {jqModal: true, closeOnEscape: true, width:500,
  recreateForm: true,
  afterclickPgButtons: showIdRow,
  beforeShowForm: showIdRow
  } //view
  );
  });

谁能指出我错过了什么地方?

我想配置subgrid,使用我的local data myData2 就像上面的条件一样。如果myData(主网格的本地json数组)和myData2(子网格的本地json数组)的orderID相同,则将下面的myData2记录显示到父网格行。我认为所有网格都是已经配置的子网格概念。但是我找不到任何正确的文档或示例。请提供任何链接以按照上述配置子网格。

解决方案:

我解决了我的问题。每次单击此模型中的下一个或上一个按钮时,此视图模型都会刷新。因为,我使用了recreateform:true,我在afterclickPgButtonsbeforeShowForm 上调用showIdRow

我认为它不会将我手动添加的行视为表单值之一。因为在创建表单时,内容是从myData(此网格的数据源)加载的。因此,当我浏览下一个或上一个按钮时,recreateform 正在处理其他行,afterclickPgButtons' and 'beforeShowForm 正在调用 showIdRow 并将下面的新行附加到已创建的行。

所以,我决定删除之前创建的一个新行,同时追加一个新行。我已经将我的自定义行 ID 分配为trv_particulars。所以,我就这样使用了我的代码,

.
.
$('#tvr_Particulars').remove();  // remove previously created custome row using row ID
$('#trv_order_dt').after(addHTML);
.
.

现在它在我的场景中工作正常。但是@Oleg 在下面的answer 中提供了替代和最佳解决方案。感谢@Oleg 花时间为我的问题创建另一个可能的解决方案。

【问题讨论】:

  • 我建议您删除#trv_particulars,而不是隐藏没有重复的 id。我的意思是你可以使用$('#tvr_Particulars').remove(); 而不是$('#tvr_Particulars').hide();
  • 哦...是的...感谢您的建议@Oleg。我根据您的建议更新了我的解决方案

标签: jquery jqgrid


【解决方案1】:

我发现您的问题很有趣,因此我创建了the demo,它演示了此类要求的可能实现。 View 的结果如下图所示:

一些实现的cmets。首先,我使用了网格的输入数据,其中包含在视图中创建“订单详情”(“订单详情”)所需的详细信息:

var mydata = [
        { id: "1",  invdate: "2007-10-21", name: "test",   note: "3note",   amount: "200.00", tax: "10.00", closed: true,  ship_via: "TN", total: "210.00",
            subgrid: [{no: "1", amount: "750"}, {no: "2", amount: "750"}, {no: "3", amount: "600"}, {no: "4", amount: "900"}] },
        { id: "2",  invdate: "2007-10-22", name: "test2",  note: "3note2",  amount: "300.00", tax: "20.00", closed: false, ship_via: "FE", total: "320.00",
            subgrid: [{no: "1", amount: "750"}, {no: "2", amount: "600"}, {no: "3", amount: "900"}] },
        ...
    ];

每个项目的subgrid属性都包含信息。

接下来,我在视图表格的表格中插入带有“订单详细信息”(“订单详情”)的自定义行,位于“关闭”列信息的行之后。我用代码

$("#trv_closed").after('<tr class="FormData">' +
    '<td class="CaptionTD form-view-label ui-widget-content" width="40%"><b>Order Particulars</b></td>' +
    '<td class="DataTD form-view-data ui-helper-reset ui-widget-content"><div style="width:100%;height:100%">' +
    '<table id="mysubgrid"><tr><td>' +
    '</td></tr></table>' +
    '</div></td></tr>');

在行"#trv_closed"之后添加行(行的id将根据“关闭”列的名称构造)。

然后可以用不同的方式对当前显示的行进行rowid。第一种方法是获取当前选定的行$this.jqGrid("getGridParam", "selrow")。另一种方式:可以从包含具有 rowid 的&lt;input&gt; 元素的隐藏列中获取信息。可以通过id="id_g" ($("#id_g").val()) 或name="id" ($form.find("input[name=id]").val()) 寻址&lt;input&gt; 元素。我在我的代码中使用了$("#id_g").val()

现在可以使用getLocalRow 获取所选行的subgrid 属性(因为我们使用datatype: "local"):

var localRowData = $this.jqGrid("getLocalRow", rowid);
// localRowData.subgrid contains the required data

所以完整的代码是

$("#list").jqGrid("navGrid", "#pager", {edit: false, add: false, del: false, view: true, search: false}, {}, {}, {}, {}, {
    recreateForm: true,
    afterclickPgButtons: function (buttonName, $form, pos) {
        showDetails.call(this, $form);
    },
    beforeShowForm: showDetails,
    labelswidth: '40%'
});

函数showDetails定义如下

var showDetails = function ($form) {
    var $this = $(this),
        rowid1 = $this.jqGrid("getGridParam", "selrow"),
        rowid = $("#id_g").val(), //id2 = $form.find("input[name=id]").val(),
        localRowData = $this.jqGrid("getLocalRow", rowid);
    if (isClosed === "Yes") {
        $("#trv_id").show();
    }
    if ($("#mysubgrid").length === 0) {
        // if not in afterclickPgButtons for example
        $("#trv_closed").after('<tr class="FormData">' +
            '<td class="CaptionTD form-view-label ui-widget-content" width="40%"><b>Order Particulars</b></td>' +
            '<td class="DataTD form-view-data ui-helper-reset ui-widget-content"><div style="width:100%;height:100%">' +
            '<table id="mysubgrid"><tr><td>' +
            '</td></tr></table>' +
            '</div></td></tr>');
         $("#mysubgrid").jqGrid({
             datatype: "local",
             data: localRowData.subgrid,
             colNames: ["#", "Amount"],
             colModel: [
                 {name: "no", width: 40, sorttype: "integer"},
                 {name: "amount", width: 70, sorttype: "integer"}
             ],
             idPrefix: "s",
             sortname: "no",
             rowNum: 1000,
             height: "auto"
         });
    } else {
        // update $("#mysubgrid") with new data
        $("#mysubgrid").jqGrid("clearGridData")
            .jqGrid("setGridParam", {data: localRowData.subgrid})
            .trigger("reloadGrid");
    }
};

【讨论】:

  • 谢谢...让我试试这个,让你知道 OP。您的演示链接页面显示 Uncaught ReferenceError: isClosed is not defined 错误。我很想知道为什么要反复添加该元素?你有什么想法吗?
  • 在点击#pData#nData 时,有什么方法可以使refreshrecreate 形成?我有一点问题要像这样生成我的 json 数组。所以只能这样尝试……但是,我希望,我可以像这样转换我的结果集……让我试试这个……
  • @CJRamki:抱歉,我修复了演示。
  • @CJRamki:你是否像我在演示中那样使用recreateForm: true 选项?它会在每次选择 View 表单时刷新 View。没有选项允许在单击"#nData"#pData 时重新创建视图表单(请参阅the source code)。 jqGrid 只需调用onclickPgButtons,然后在视图中填充标准数据(调用fillData),选择下一行/上一行并调用afterclickPgButtons。所以你必须实现你的 afterclickPgButtons 以便它在场景中工作。
  • 我解决了我的问题。请参阅我的问题中的 SOLUTION 部分。
猜你喜欢
  • 2012-07-29
  • 2016-11-18
  • 1970-01-01
  • 2013-01-31
  • 2012-02-12
  • 2013-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多