gpt4 book ai didi

javascript - 在 ng-grid 中使用 ngGridEventEndCellEdit 更改特定字段

转载 作者:行者123 更新时间:2023-12-03 11:53:11 24 4
gpt4 key购买 nike

我在 ng-grid 模块中遇到了 ngGridEventEndCellEdit 的问题。

我的目的是当我更改 ng-grid 中的单元格时,我从我的服务中调用一个方法,该方法通过 ajax 请求返回一些数据。我的代码工作正常。但唯一的问题是,当我更改任何单元格或字段时,它会调用服务方法。

如何检查特定单元格是否已编辑???

以下是我的代码片段...

$scope.$on('ngGridEventEndCellEdit', function (data) {
console.log(data.targetScope.row);
costsheetCrudService.getItemByItemCode(data.targetScope.row.entity.ItemCode).then(function (res) {
data.targetScope.row.entity.ItemDescription = res.data.ItemDescription;
data.targetScope.row.entity.ItemCategory = res.data.ItemCategoryName;
data.targetScope.row.entity.ItemID = res.data.ItemID;
data.targetScope.row.entity.ItemCategoryID = res.data.ItemCategoryID;
});
});

最佳答案

来自documentation on templating :

When editing a cell, the ng-cell-has-focus directive will broadcast a message named ngGridEventStartCellEdit to let all children know that you can now give yourself focus. When the editable cell template is done with editing (usually on a blur event) you need to emit ngGridEventEndCellEdit to let ng-cell-has-focus know that you are done editing and it will then show the non-editable cell template. The reasoning for this is (good quote): "Now I can wrap my input elements in divs/spans, whatever and control exactly what element's blur triggers the end edit" - @swalters.

If you search for the 'ngInput' directive in ng-rgid's source code, you will find that that is exactly what this directive implements for input elements. So if you need to create your own 'cell editor', you could create your own directive that would listen to and emit the right events, to make your component work as expected.

示例(用于 ng-input 指令):

scope.$on( 'ngGridEventStartCellEdit', function () {
elm.focus();
}); //focus the input element on 'start cell edit'

angular.element( elm ).bind( 'blur', function () {
scope.$emit( 'ngGridEventEndCellEdit' );
}); //when leaving the input element, emit the 'end cell edit' event

否则,您可以在事件发出时在 Controller 内添加逻辑:

if(data.targetScope.col.index === X) {}
if(data.targetScope.row.rowIndex === X) {}
if(data.targetScope.row.entity.something === something else) {}.

关于javascript - 在 ng-grid 中使用 ngGridEventEndCellEdit 更改特定字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25719800/

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