Javascript 在ui网格中添加新的空行

Javascript 在ui网格中添加新的空行,javascript,angularjs,angular-ui-grid,Javascript,Angularjs,Angular Ui Grid,我试图在ui网格中添加一个新的空行。我试着用不同的芭蕾舞曲和例子,但我发现所有的都不符合我的要求,我也无法使它适应我所寻找的。 事实上,我正在研究如何在现有ui网格中添加一个新的空行,既不使用网格外的按钮,也不使用行尾中的按钮。 我希望在网格中添加一个abutton,如下面的屏幕截图所示的+按钮 或者在渲染ui网格时自动渲染一个新的空行,在填充所有行时自动渲染一个新的空行。 我试着用单元格模板中的函数来做,但它不起作用。 非常感谢您的帮助对我来说,第一个选项听起来更像是一个CSS问题。本质上,

我试图在ui网格中添加一个新的空行。我试着用不同的芭蕾舞曲和例子,但我发现所有的都不符合我的要求,我也无法使它适应我所寻找的。 事实上,我正在研究如何在现有ui网格中添加一个新的空行,既不使用网格外的按钮,也不使用行尾中的按钮。 我希望在网格中添加一个abutton,如下面的屏幕截图所示的+按钮

或者在渲染ui网格时自动渲染一个新的空行,在填充所有行时自动渲染一个新的空行。 我试着用单元格模板中的函数来做,但它不起作用。
非常感谢您的帮助

对我来说,第一个选项听起来更像是一个CSS问题。本质上,“添加”按钮将使用某种包含+的字体库,您需要将其放置在网格的一角。也许可以从查看网格页脚开始。听起来您已经在这里看到了创建“添加行”按钮的基础知识:

第二个选项(在呈现ui网格时自动呈现新的空行,在填充所有行时自动呈现新的空行)需要JavaScript方法

我遵循的基本逻辑是:

  • (假设)从后端某处加载一些数据(在本示例中,它是一个模拟加载,返回$http或$resource的承诺)
  • 加载数据后,我们追加一个新行。我们首先等待数据;否则我们就不会将新行推到正确的位置
  • 编辑操作完成后,我们设置了一个超时,以确保其他单元格上的后续编辑不会继续触发新行。如果达到超时,我们将追加一个新行。如果后续的编辑操作发生,并且存在超时承诺(用于添加新行),我们将取消它。一旦没有编辑操作发生,并且达到了超时,我们就会推送到新行
  • 为了确保我们仅在修改“额外行”时才采取行动,当我们创建行时,将保留对当前行的引用,以便我们可以评估接收到的事件是否有意义(var newRowTimeoutPromise)

    代码中的核心逻辑如下,Plnkr中有一个示例实现:

    var extraRow = null,
            addNewTimeoutMillis = 2000,
            newRowTimeoutPromise = null;
    
        loadData().then(function(data) {
            $scope.gridOpts.data = data;
        }).finally(function() {
            // add initial empty row, and set our reference to it
            extraRow = addEmptyRow($scope.gridOpts.data);
        })
    
        $scope.gridOpts = {
            showGridFooter: true,
            onRegisterApi: function(gridApi) {
                $scope.gridApi = gridApi;
    
                // listen for cell edit completion
                gridApi.edit.on.afterCellEdit($scope, function(rowEntity, colDef, newValue, oldValue) {
    
                    // test if the edited row was the "extra row"
                    // otherwise, and edit to any row would fire a new row
    
                    // Set a timeout so we don't create a new row if the user has
                    // not finished their edit(s) on other fields
                    newRowTimeoutPromise = $timeout(function() {
                        if (rowEntity == extraRow) {
                            // add a new empty row, and set our reference to it
                            extraRow = addEmptyRow($scope.gridOpts.data);
                            newRowTimeoutPromise = null;
                        }
                    }, addNewTimeoutMillis);
                })
    
                // Listen for cell edit start, and cancel if we have a pending new
                // row add. Otherwise, each time you finish the edit on a cell,
                // this will fire.
                gridApi.edit.on.beginCellEdit($scope, function(rowEntity, colDef, newValue, oldValue) {
                    if (newRowTimeoutPromise != null) {
                      $timeout.cancel(newRowTimeoutPromise);
                    }
                })
    
            }
        };
    

    对我来说,第一个选项听起来更像是一个CSS问题。本质上,“添加”按钮将使用某种包含+的字体库,您需要将其放置在网格的一角。也许可以从查看网格页脚开始。听起来您已经在这里看到了创建“添加行”按钮的基础知识:

    第二个选项(在呈现ui网格时自动呈现新的空行,在填充所有行时自动呈现新的空行)需要JavaScript方法

    我遵循的基本逻辑是:

  • (假设)从后端某处加载一些数据(在本示例中,它是一个模拟加载,返回$http或$resource的承诺)
  • 加载数据后,我们追加一个新行。我们首先等待数据;否则我们就不会将新行推到正确的位置
  • 编辑操作完成后,我们设置了一个超时,以确保其他单元格上的后续编辑不会继续触发新行。如果达到超时,我们将追加一个新行。如果后续的编辑操作发生,并且存在超时承诺(用于添加新行),我们将取消它。一旦没有编辑操作发生,并且达到了超时,我们就会推送到新行
  • 为了确保我们仅在修改“额外行”时才采取行动,当我们创建行时,将保留对当前行的引用,以便我们可以评估接收到的事件是否有意义(var newRowTimeoutPromise)

    代码中的核心逻辑如下,Plnkr中有一个示例实现:

    var extraRow = null,
            addNewTimeoutMillis = 2000,
            newRowTimeoutPromise = null;
    
        loadData().then(function(data) {
            $scope.gridOpts.data = data;
        }).finally(function() {
            // add initial empty row, and set our reference to it
            extraRow = addEmptyRow($scope.gridOpts.data);
        })
    
        $scope.gridOpts = {
            showGridFooter: true,
            onRegisterApi: function(gridApi) {
                $scope.gridApi = gridApi;
    
                // listen for cell edit completion
                gridApi.edit.on.afterCellEdit($scope, function(rowEntity, colDef, newValue, oldValue) {
    
                    // test if the edited row was the "extra row"
                    // otherwise, and edit to any row would fire a new row
    
                    // Set a timeout so we don't create a new row if the user has
                    // not finished their edit(s) on other fields
                    newRowTimeoutPromise = $timeout(function() {
                        if (rowEntity == extraRow) {
                            // add a new empty row, and set our reference to it
                            extraRow = addEmptyRow($scope.gridOpts.data);
                            newRowTimeoutPromise = null;
                        }
                    }, addNewTimeoutMillis);
                })
    
                // Listen for cell edit start, and cancel if we have a pending new
                // row add. Otherwise, each time you finish the edit on a cell,
                // this will fire.
                gridApi.edit.on.beginCellEdit($scope, function(rowEntity, colDef, newValue, oldValue) {
                    if (newRowTimeoutPromise != null) {
                      $timeout.cancel(newRowTimeoutPromise);
                    }
                })
    
            }
        };
    

    我使用jQuery获取并更改单元格模板中特定单元格元素的样式

    这里有一个有用的例子

    以下是控制器脚本:-

    var app = angular.module('app', ['ngTouch', 'ui.grid']);
    app.controller('MainCtrl', ['$scope',
    function($scope) {
        $scope.gridOptions = {};
    
        $scope.Add = function() {
    
           $scope.gridOptions.data.push( { firstName: ' ',lastName:'',company:'' });
    $(".ui-grid-coluiGrid").prevObject["0"].activeElement.style.display="none";  
    $(".ui-grid-cell")[$scope.gridOptions.data.length-2].style.display="inline";
      };
    $scope.gridOptions.onRegisterApi = registerGridApi;
    function registerGridApi(gridApi) {
        $scope.gridApi= gridApi
    
    };   
    
     $scope.gridOptions.columnDefs = [{
            name: 'firstName',
            field: 'firstName', 
        }, {
            name: 'lastNamer',
            field: 'firstName'
        }, {
            name: 'ShowScope',
            cellTemplate: '<button id="btb" ng-click="grid.appScope.Add()">+</button>'
        }];
         $scope.gridOptions.data = [{ yourdata}];
    }
     ]);
    
    var-app=angular.module('app',['ngTouch','ui.grid']);
    app.controller('MainCtrl',['$scope',
    职能($范围){
    $scope.gridOptions={};
    $scope.Add=函数(){
    $scope.gridOptions.data.push({firstName:'',lastName:'',company:''});
    $(.ui grid coluiGrid”).prevObject[“0”].activeElement.style.display=“无”;
    $(“.ui网格单元格”)[$scope.gridOptions.data.length-2].style.display=“inline”;
    };
    $scope.gridOptions.onRegisterApi=registerGridApi;
    函数registerGridApi(gridApi){
    $scope.gridApi=gridApi
    };   
    $scope.gridOptions.columnDefs=[{
    姓名:'firstName',
    字段:“firstName”,
    }, {
    姓名:'lastNamer',
    字段:“名字”
    }, {
    名称:“ShowScope”,
    cellTemplate:“+”
    }];
    $scope.gridOptions.data=[{yourdata}];
    }
    ]);
    
    要使它正常工作,还需要做两件事

  • 使用cellContentEditable使行可编辑
  • 为了禁用显示在与已有数据行对应的单元格上的单元格模板按钮的显示样式,可以使用angular foreach或for循环来迭代这些行并禁用样式(我尝试使用renderContainers,但它总是将Add函数外渲染行的长度返回为0)

  • 我使用jQuery获取和更改单元格模板的特定单元格元素的样式

    这里有一个有用的例子

    以下是控制器脚本:-

    var app = angular.module('app', ['ngTouch', 'ui.grid']);
    app.controller('MainCtrl', ['$scope',
    function($scope) {
        $scope.gridOptions = {};
    
        $scope.Add = function() {
    
           $scope.gridOptions.data.push( { firstName: ' ',lastName:'',company:'' });
    $(".ui-grid-coluiGrid").prevObject["0"].activeElement.style.display="none";  
    $(".ui-grid-cell")[$scope.gridOptions.data.length-2].style.display="inline";
      };
    $scope.gridOptions.onRegisterApi = registerGridApi;
    function registerGridApi(gridApi) {
        $scope.gridApi= gridApi
    
    };   
    
     $scope.gridOptions.columnDefs = [{
            name: 'firstName',
            field: 'firstName', 
        }, {
            name: 'lastNamer',
            field: 'firstName'
        }, {
            name: 'ShowScope',
            cellTemplate: '<button id="btb" ng-click="grid.appScope.Add()">+</button>'
        }];
         $scope.gridOptions.data = [{ yourdata}];
    }
     ]);
    
    var-app=angular.module('app',['ngTouch','ui.grid']);
    app.controller('MainCtrl',['$scope',
    职能($范围){
    $scope.gridOptions={};
    $scope.Add=函数(){
    $scope.gridOptions.data.push({firstName:'',lastName:'',company:''});
    $(.ui grid coluiGrid”).prevObject[“0”].activeElement.style.display=“无”;
    $(“.ui网格单元格”)[$scope.gridOptions.data.length-2].style.display=“inline”;
    };
    $scope.gridOptions.onRegisterApi=registerGridApi;
    函数registerGridApi(gridApi){
    $scope.gridApi=gridApi
    };   
    $scope.gridOptions.columnDefs=[{
    名称