Angularjs Kendo Master-detail网格从Master应用默认ID添加新的详细记录

时间:2017-06-16 14:58:19

标签: javascript angularjs asp.net-mvc kendo-grid

我试图从主网格中的扩展行的ID字段中获取值,以便在“详细信息”网格中添加新记录时填充“详细信息”网格上的相应字段。我尝试了以下方法:

$scope.detailGridOptions = function (dataItem) {
  return {
      dataSource: {
          type: 'aspnetmvc-ajax',
          transport: {
              read: "/Home/GetTaggedContractorsByMajor"
              ,
              create: "/Home/AddContractorToTag"
          },
          serverPaging: true,
          serverSorting: true,
          serverFiltering: true,
          schema: {
              data: "Data",
              total: "Total",
              model: {
                id: "ContractorID",
                fields: {
                    CompanyID: { editable: true, nullable: true, type: "string" },
                    TagID: { editable: true, nullable: true, type: "number" },
                    vchCompanyName: { editable: false, nullable: false, type: "string"},
                    Date: { editable: false, nullable: false, type: "date" },
                    AddedBy: { editable: false, nullable: false, type: "string" }
          }
      }
          },
          pageSize: 5,
          filter: { field: "TagID", operator: "eq", value: dataItem.TagID }
      },
      scrollable: false,
      sortable: true,
      pageable: true,
      editable: "popup",
      edit: function (e) {
              var uid = $(this.element[0]).closest('.k-detail-row').prev().attr('data-uid');
          var selector = kendo.format("tr[data-uid='{0}']", uid);
          var currentGridElement = $(selector).closest("[data-role=grid]");
          var parentRow = currentGridElement.closest(".k-detail-row").prev();
          var parentGrid = parentRow.closest("[data-role=grid]").data("kendoGrid");
          var parentModel = parentGrid.dataItem(parentRow);
          return {
              TagID: parentModel.TagID
          };
      },
      toolbar: ["create"],
      columns: [
      {
          field: "TagID"
        , hidden: true
      }
      ,
          {
              field: "CompanyID",
              title: "CompanyID"
              , editor: companyDropDownEditor
          }
      ,
        {
            field: "vchCompanyName",
            title: "Company Name"
        }
        ,
          {
              field: "Date",
              title: "Date Added"
          }
          ,
          {
              field: "AddedBy",
              title: "Added By"
          }
      ]

  };

};

“parentGrid”变量始终为null。但是,如果将网格的名称直接用作$scope.tagsGrid,我可以获取网格,但行var parentModel = $scope.tagsGrid.dataItem(parentRow);给出错误“无法获取未定义或空引用的属性'TagID'”。我想即使我能够使代码的那部分工作,我仍然缺少一些尝试手动设置这样的ID

          return {
              TagID: 1
          };

也没有设置ID。如果无法自动为新的详细信息行设置ID字段,是否可以从Master获取ID值并将其作为参数发送到我的创建URL?

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

我能够通过对编辑事件功能进行一些调整来实现这一点:

edit: function (e) {
          var grid = $scope.tagsGrid;
          var uid = e.model.uid;
          var selector = kendo.format("tr[data-uid='{0}']", uid);
          var currentGridElement = $(selector).closest("[data-role=grid]");
          var parentRow = currentGridElement.closest(".k-detail-row").prev();
          var parentModel = grid.dataItem(parentRow);
          e.model.TagID = parentModel.TagID;
      },