gpt4 book ai didi

javascript - 添加行后如何强制自动刷新ui-grid

转载 作者:数据小太阳 更新时间:2023-10-29 05:32:32 24 4
gpt4 key购买 nike

我花了很长时间来解决这个问题,需要一些帮助。我在 Angular Ui-Grid 的帮助下在我的页面上呈现网格,但在我添加新行后无法强制它刷新数据。在我的主 Controller 中,我有函数“创建事件”,它调用服务和模态模板来上传数据:

  $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)

};

从事件 Controller 提交事件和插入事件看起来像

 $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)
})
};

所以插入效果很好,模态效果很好,但即使我试图返回 promise 或回调,网格也不会更新,这无济于事,

我尝试在关闭模式、插入事件或单击提交按钮后设置刷新,但没有任何帮助

   $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);
});
}());

如果有人能帮我找出我的错误,我将不胜感激。

最佳答案

在你的“then”函数中试试这个:

$scope.gridOptions.data.push(result.data);

关于javascript - 添加行后如何强制自动刷新ui-grid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37599044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com