使用 ui-grid 3.0 RC19,editableCellTemplate 方法是正确的。我建议您直接从 ui-grid.js 复制,其中定义了 'ui-grid/cellEditor' $templateCache:
$templateCache.put('ui-grid/cellEditor',
"<div><form name=\"inputForm\"><input type=\"INPUT_TYPE\" ng- class=\"'colt' + col.uid\" ui-grid-editor ng-model=\"MODEL_COL_FIELD\"></form> </div>");
然后,修改它以将其包含在您的代码中:
{
field: 'label',
enableCellEdit: true,
editableCellTemplate:
"<div><form name=\"inputForm\"><input type=\"INPUT_TYPE\" ng-class=\"'colt' + col.uid\" ui-grid-editor ng-model=\"MODEL_COL_FIELD\" name=\"label\" ng-minlength=\"3\" ng-maxlength=\"10\" required validate-required-cell></form></div><span class=\"error\" ng-show=\"inputForm.$valid\">Too short!</span>"
},
在我的情况下,我需要一个名为 validateRequiredField 的附加指令来在元素失去焦点时停止单元格关闭:
angularStartDirectives.directive('validateRequiredCell', function(uiGridEditConstants) {
return {
restrict: 'A',
scope: false,
require: 'ngModel',
link: function (scope, element, attrs, ngModel) {
element.bind('blur', function(evt) {
if (scope.inputForm && !scope.inputForm.$valid) {
// Stops the rest of the event handlers from being executed
evt.stopImmediatePropagation();
}
});
}
};
});
您还可以包括这样的样式:
form input.ng-invalid {
border-color:red;
}
form input.ng-valid {
border-color: transparent;
}
进一步改进的参考:
UI 网格验证