【问题标题】:Angular ui-grid adding new row dynamicallyAngular ui-grid 动态添加新行
【发布时间】:2017-12-07 02:42:36
【问题描述】:

我对新添加的行有疑问。它继承网格中的行(按钮和行为)。该代码正在添加一个新行,但我想要一个具有 AddOverride() 函数中行为的新行。就像一个带有保存和取消按钮的按钮。当我添加新行时。新行 > 有更新和删除按钮。当我尝试在新添加的行上单击更新时,它正在调用服务进行更新。

-

HTML

   <button  id="addNew" ng-click="addOverride()" type="button" class="btn 
btn-primary">Add Rank</button>      
                <div ui-grid-auto-resize ui-grid="gridOptions" ui-grid-
pagination class="gridRank" style="height: 370px !important;"></div>

填充网格

$scope.gridOptions = {             
            columnDefs: [
                { 
                    field: 'rankID', displayName: '', visible: false
                },
                {
                    name: 'rankDesc', field: 'rankDesc', displayName: 'Rank Desc',
                    cellTemplate: '<div  ng-if="!row.entity.editrow">{{COL_FIELD}}</div><div ng-if="row.entity.editrow"><input type="text" style="height:30px" ng-model="MODEL_COL_FIELD"</div>', width: 350

                },
                 {
                     field: 'createTime', displayName: 'Create Time', cellFilter: 'date:\'yyyy-MM-dd\'', width: 200
                 },
                {
                    field: 'createUserID', displayName: 'Create UserID', enableFiltering: false, enableSorting: false, enableColumnMenu: false, width: 130
                },                   
                 {
                     field: 'updateTime', displayName: 'Update Time', cellFilter: 'date:\'yyyy-MM-dd\'', width: 200
                 },
                {
                    field: 'updateUserID', displayName: 'Update UserID', enableFiltering: false, enableSorting: false, enableColumnMenu: false, width: 130
                },
            {
                name: 'Action', field: 'edit', enableFiltering: false, enableSorting: false, enableColumnMenu: false,
                cellTemplate: '<div><button ng-show="!row.entity.editrow"  ng-click="grid.appScope.edit(row.entity)"><span class="glyphicon glyphicon-edit"></span></button>' + ' ' + //Edit Button                                   
                               '<button ng-show="row.entity.editrow" ng-click="grid.appScope.saveRow(row.entity)"><span class="glyphicon glyphicon-ok"></span></button>' + ' ' +//Save Button
                               '<button ng-show="row.entity.editrow"  ng-click="grid.appScope.cancelEdit(row.entity)"><span class="glyphicon glyphicon-remove"></span></button>' + ' ' + //Cancel Button
                               '<button ng-show="!row.entity.editrow" ng-click="grid.appScope.Delete(row.entity)"><span class="glyphicon glyphicon-trash"></span></button>' + ' ' + //Delete Button
                               '</div>', width: 75
            }],
            data: response.data.resultLists.cookRankList,
            paginationPageSizes : [10, 20, 30],
            paginationPageSize : 10,
            multiSelect: false
        };

添加新行

$scope.addOverride = function () {
            var emptyData = [
                 {
                     field: 'rankID', displayName: '', visible: false
                 },
                  {
                      name: 'rankDesc', field: 'rankDesc', displayName: 'Rank Desc',
                      cellTemplate: '<div  ng-if="!row.entity.editrow">{{COL_FIELD}}</div><div ng-if="row.entity.editrow"><input type="text" style="height:30px" ng-model="MODEL_COL_FIELD"</div>', width: 350

                  },
                  {
                      field: 'createTime', displayName: 'Create Time', cellFilter: 'date:\'yyyy-MM-dd\'', width: 200
                  },
                  {
                      field: 'createUserID', displayName: 'Create UserID', enableFiltering: false, enableSorting: false, enableColumnMenu: false, width: 130
                  },
                  {
                           field: 'updateTime', displayName: 'Update Time', cellFilter: 'date:\'yyyy-MM-dd\'', width: 200
                  },
                  {
                           field: 'updateUserID', displayName: 'Update UserID', enableFiltering: false, enableSorting: false, enableColumnMenu: false, width: 130
                  },
                  {
                    name: 'Action', field: 'add', enableFiltering: false, enableSorting: false, enableColumnMenu: false,
                    cellTemplate: '<div><button  ng-click="grid.appScope.saveRow(row.entity)"><span class="glyphicon glyphicon-ok"></span></button>' + ' ' +//Save Button
                                   '<button ng-click="grid.appScope.cancelEdit(row.entity)"><span class="glyphicon glyphicon-remove"></span></button>' + ' ' + //Cancel Button
                                   '</div>', width: 75
                }

            ];
            $scope.gridOptions.data.unshift(emptyData);
         };

【问题讨论】:

    标签: angularjs angular-ui-grid ui-grid


    【解决方案1】:

    当您添加一个新行时,它应该是一个具有与网格中其他对象相同属性的单个对象。您将列定义推入一行,因此网格很混乱。

    您的 add 函数应该被缩短为简单地将一个新对象推送到您的数据数组中:

    $scope.addOverride = function () {
       $scope.gridOptions.data.unshift({
            rankID: "",
            // shortened
        });
    };
    

    然后,对于您的模板,您可以创建两个变量来保持整洁。或者只是把它们放在一起。基本上,您希望您的模板在某些最初不会出现的字段上进行切换。我猜createTime 就是这样一个领域。通常这将是一个 id。如果需要,您还可以创建一个新的隐藏字段。

    一个轻量级的例子:

      var cellTemplateReg = '<div ng-if="row.entity.createTime">Existing</div>';
      var cellTemplateNew = '<div ng-if="!row.entity.createTime">New</div>';
    
    columnDefs: [ //shortened
      {
          name: 'Action,
          field: 'action',
          enableFiltering: false,
          enableSorting: false,
          enableColumnMenu: false,
          width: 200,
          cellTemplate: cellTemplateReg + cellTemplateNew
      }
    ]
    

    【讨论】:

    • 嗨,Brian,谢谢,但您没有回答我的问题。正如您在上面的代码中看到的,我没有为其他列添加可编辑字段。仅适用于 RankDesc。我不需要其他列的可编辑字段。我想要的只是在新添加的字段中有一个“保存”和“取消”按钮,而不是有一个“更新”和“删除”按钮,因为它是一个新添加的行,所以没有什么要更新或删除的。并且似乎新添加的行继承了上面“填充网格”columndefs 的按钮,而不是从 AddOverride() 中添加一个带有保存和取消按钮的新行。
    • function () { $scope.gridOptions.data.unshift({ rankID: "", rankDesc: "", createTime: "", createUserID: "", updateTime: "", updateUserID: " ", cellTemplate: '
      ' + ' ' + '' + ' ' }); };
    • 我理解你想要做什么。对于尚未提交的行,您需要不同的操作集。我确实将示例中的列名更新为您的 action 列名。您实际上正在做的是将列定义添加到您的数据中;网格不会对此做任何事情。它会将其视为更多嵌套字段。一列是水平的,一行是垂直的。您不能像这样定位特定行。我提供的解决方案展示了如何为列设置模板,并使模板本身有条件。
    • 嗨,布莱恩。我认为我们不在同一页面上。您已经添加了一个用于在操作中添加字段的字段。但是我添加新行的按钮在网格之外。 (检查上面的 html)另外,我的可编辑列已经有了那个单元格模板,它只是 RankDesc。我所需要的只是有一个保存和取消的新按钮。我将如何实现这一目标?你能给我一个使用 jsfiddle 或 plnker 的样本吗?我如何在影响新添加的行的填充网格中定义我的 columndefs 有什么问题吗?谢谢。
    • 您的问题表明您有一个位于网格外部的按钮,用于添加新行。当您单击它时,应将新行添加到网格中,并且该行在 Action 列中应该有不同的按钮,因为该记录尚未创建(因此您将保存或取消与删除或更新)。我提议的是这样做。如果这不是您想要的,您必须更新问题,因为这就是我正在收集的内容。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签