Javascript 在ui网格中添加新的空行
我试图在ui网格中添加一个新的空行。我试着用不同的芭蕾舞曲和例子,但我发现所有的都不符合我的要求,我也无法使它适应我所寻找的。 事实上,我正在研究如何在现有ui网格中添加一个新的空行,既不使用网格外的按钮,也不使用行尾中的按钮。 我希望在网格中添加一个abutton,如下面的屏幕截图所示的+按钮 或者在渲染ui网格时自动渲染一个新的空行,在填充所有行时自动渲染一个新的空行。 我试着用单元格模板中的函数来做,但它不起作用。Javascript 在ui网格中添加新的空行,javascript,angularjs,angular-ui-grid,Javascript,Angularjs,Angular Ui Grid,我试图在ui网格中添加一个新的空行。我试着用不同的芭蕾舞曲和例子,但我发现所有的都不符合我的要求,我也无法使它适应我所寻找的。 事实上,我正在研究如何在现有ui网格中添加一个新的空行,既不使用网格外的按钮,也不使用行尾中的按钮。 我希望在网格中添加一个abutton,如下面的屏幕截图所示的+按钮 或者在渲染ui网格时自动渲染一个新的空行,在填充所有行时自动渲染一个新的空行。 我试着用单元格模板中的函数来做,但它不起作用。 非常感谢您的帮助对我来说,第一个选项听起来更像是一个CSS问题。本质上,
非常感谢您的帮助对我来说,第一个选项听起来更像是一个CSS问题。本质上,“添加”按钮将使用某种包含+的字体库,您需要将其放置在网格的一角。也许可以从查看网格页脚开始。听起来您已经在这里看到了创建“添加行”按钮的基础知识: 第二个选项(在呈现ui网格时自动呈现新的空行,在填充所有行时自动呈现新的空行)需要JavaScript方法 我遵循的基本逻辑是:
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方法 我遵循的基本逻辑是:
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}];
}
]);
要使它正常工作,还需要做两件事
我使用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=[{
名称