【发布时间】:2019-10-24 15:15:44
【问题描述】:
我花了很长时间解决这个问题,需要一些帮助。我在 Angular Ui-Grid 的帮助下在我的页面上渲染网格,但在我添加新行后无法强制它刷新数据。 在我的主控制器中,我有函数“创建事件”,它正在调用用于上传数据的模式的服务和模板:
$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)
})
};
所以插入效果很好,模态效果很好,但即使我试图返回承诺或回调,网格也不会更新,它没有帮助,
我尝试在关闭模式、插入事件或单击提交按钮后设置刷新,但没有任何帮助
$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);
});
}());
如果有人可以帮助我找出我的错误,我将不胜感激。
【问题讨论】:
-
第一次打开网格时,在哪里绑定原始数据到网格?我没有看到那个代码。
-
@RaniRadcliff 请检查更新后的帖子
-
插入后,result.data 是只包含插入的行还是整个数据集?
-
@RaniRadcliff 是的。 $log.info 显示插入的行
标签: javascript angularjs angular-ui-grid