添加行后如何强制自动刷新ui-grid
how to force automatically refresh ui-grid after adding row
我花了很长时间来解决这个问题,需要一些帮助。我正在使用 help Angular Ui-Grid 在我的页面上呈现网格,但在我添加新行后无法强制它刷新数据。
在我的主控制器中,我有函数 'create event',它调用服务和模态模板来上传数据:
$scope.createEvent = function (eventTypeId) {
var newEvent = null;
var eventFields = null;
var opts = {
backdrop: 'static',
backdropClick: true,
dialogFade: false,
keyboard: true,
templateUrl: 'views/event.html',
controller: 'EventEditCtrl',
size: 'md',
resolve: {
params: function () {
return {model: newEvent, fields: eventFields, caption: "Create event"};
}
}
};
eventService.getEventTemplate(0, eventTypeId)
.then(function (data) {
newEvent = data.model;
newEvent.id = null;
newEvent.occuredDate = new Date(newEvent.occuredDate);
eventFields = data.fields;
var uibModalInstance = $uibModal.open(opts);
uibModalInstance.result.then(function (data) {
$location.path("views/" + data.model.id);
}, function () {
}
);
}, errorDetails)
};
从事件控制器提交事件和插入事件看起来像
$scope.insertEvent = function (event) {
$log.info('insert');
$log.info(event);
eventService.insertEvent(event)
.then(function (data) {
$uibModalInstance.close(data);
});
};
$scope.onSubmit = function (event) {
console.log(event);
if (event.id == null || event.id == 0) {
$scope.insertEvent(event)
}
}
最后我的插入事件服务函数看起来像
var insertEvent = function (event) {
return $http({
method : 'POST',
url : apiUrl,
data : event
})
.then(function success (result ) {
console.log(result.data);
cachedEvents = result.data;
return result.data
}, function error (response){
$log.error(" post has been failed ", response)
})
};
所以插入效果很好,模态效果很好,但即使我尝试 return 承诺或回调,网格也不会更新,这无济于事,
我试过在关闭模式、插入事件或单击提交按钮后设置刷新,但没有任何帮助
$scope.gridApi.core.queueGridRefresh();
$scope.gridApi.core.refresh();
$scope.gridApi.grid.refreshCanvas();
$scope.gridApi.grid.refreshRows();
我也尝试过设置网格更改时的通知,但也无济于事:
onRegisterApi: function (gridApi) {
$scope.gridApi = gridApi;
gridApi.core.on.columnVisibilityChanged($scope, function (changedColumn) {
$scope.columnChanged = {name: changedColumn.colDef.name, visible: changedColumn.colDef.visible};
});
//**********
gridApi.selection.on.rowSelectionChanged($scope, function (row) {
$scope.rowData = row.entity;
$scope.id = $scope.rowData.id;
$scope.rowKeys = Object.keys($scope.rowData);
});
gridApi.core.notifyDataChange( uiGridConstants.dataChange.ALL)
}
原始网格数据初始化
var getData = (function () {
$http.get(urlData)
.success(function (data) {
// Considering Angular UI-Grid $scope.gridOptions.data should be declared as is
$scope.gridOptions.data = data;
// $interval whilst we wait for the grid to digest the data we just gave it
$interval(function () {
$scope.gridApi.selection.selectRow($scope.gridOptions.data[0]);
}, 0, 1);
});
}());
如果有人能帮我找出我的错误,我将不胜感激。
您能否尝试从您的 httpPost 返回一个 promise 或一个回调,而不是通常只返回缓存的事件。试试这个是否有效。
在你的 "then" 函数中试试这个:
$scope.gridOptions.data.push(result.data);
您可以使用grid.api.core.notifyDataChange( uiGridConstants.dataChange.ALL );
您可以先使用 $scope.grid.options.data.push(data);
,然后再使用 scope.grid.refresh();
,但问题是如果您将数据导出为 PDF 或 CSV,则不会反映新添加的数据。
我花了很长时间来解决这个问题,需要一些帮助。我正在使用 help Angular Ui-Grid 在我的页面上呈现网格,但在我添加新行后无法强制它刷新数据。 在我的主控制器中,我有函数 'create event',它调用服务和模态模板来上传数据:
$scope.createEvent = function (eventTypeId) {
var newEvent = null;
var eventFields = null;
var opts = {
backdrop: 'static',
backdropClick: true,
dialogFade: false,
keyboard: true,
templateUrl: 'views/event.html',
controller: 'EventEditCtrl',
size: 'md',
resolve: {
params: function () {
return {model: newEvent, fields: eventFields, caption: "Create event"};
}
}
};
eventService.getEventTemplate(0, eventTypeId)
.then(function (data) {
newEvent = data.model;
newEvent.id = null;
newEvent.occuredDate = new Date(newEvent.occuredDate);
eventFields = data.fields;
var uibModalInstance = $uibModal.open(opts);
uibModalInstance.result.then(function (data) {
$location.path("views/" + data.model.id);
}, function () {
}
);
}, errorDetails)
};
从事件控制器提交事件和插入事件看起来像
$scope.insertEvent = function (event) {
$log.info('insert');
$log.info(event);
eventService.insertEvent(event)
.then(function (data) {
$uibModalInstance.close(data);
});
};
$scope.onSubmit = function (event) {
console.log(event);
if (event.id == null || event.id == 0) {
$scope.insertEvent(event)
}
}
最后我的插入事件服务函数看起来像
var insertEvent = function (event) {
return $http({
method : 'POST',
url : apiUrl,
data : event
})
.then(function success (result ) {
console.log(result.data);
cachedEvents = result.data;
return result.data
}, function error (response){
$log.error(" post has been failed ", response)
})
};
所以插入效果很好,模态效果很好,但即使我尝试 return 承诺或回调,网格也不会更新,这无济于事,
我试过在关闭模式、插入事件或单击提交按钮后设置刷新,但没有任何帮助
$scope.gridApi.core.queueGridRefresh();
$scope.gridApi.core.refresh();
$scope.gridApi.grid.refreshCanvas();
$scope.gridApi.grid.refreshRows();
我也尝试过设置网格更改时的通知,但也无济于事:
onRegisterApi: function (gridApi) {
$scope.gridApi = gridApi;
gridApi.core.on.columnVisibilityChanged($scope, function (changedColumn) {
$scope.columnChanged = {name: changedColumn.colDef.name, visible: changedColumn.colDef.visible};
});
//**********
gridApi.selection.on.rowSelectionChanged($scope, function (row) {
$scope.rowData = row.entity;
$scope.id = $scope.rowData.id;
$scope.rowKeys = Object.keys($scope.rowData);
});
gridApi.core.notifyDataChange( uiGridConstants.dataChange.ALL)
}
原始网格数据初始化
var getData = (function () {
$http.get(urlData)
.success(function (data) {
// Considering Angular UI-Grid $scope.gridOptions.data should be declared as is
$scope.gridOptions.data = data;
// $interval whilst we wait for the grid to digest the data we just gave it
$interval(function () {
$scope.gridApi.selection.selectRow($scope.gridOptions.data[0]);
}, 0, 1);
});
}());
如果有人能帮我找出我的错误,我将不胜感激。
您能否尝试从您的 httpPost 返回一个 promise 或一个回调,而不是通常只返回缓存的事件。试试这个是否有效。
在你的 "then" 函数中试试这个:
$scope.gridOptions.data.push(result.data);
您可以使用grid.api.core.notifyDataChange( uiGridConstants.dataChange.ALL );
您可以先使用 $scope.grid.options.data.push(data);
,然后再使用 scope.grid.refresh();
,但问题是如果您将数据导出为 PDF 或 CSV,则不会反映新添加的数据。