Check all cells in all SlickGrid rows

Given SlickGrid, how can I trigger a check on all cells in all rows? Or perhaps use JavaScript to activate cell validation (which I can use for all cells in all rows)?

A use case is one where the user needs to edit each cell and provide something other than the default, and we want to make sure they do this, and we want to show the behavior of the default validation error if they did not.

Currently, it turns out that validation is performed only in editable fields.

+5
source share
1 answer

As noted, Slickgrid validation is limited by default to the validate editor function, which checks for any validator available validator passing only the value as a parameter. To provide additional contextual information, a special editor or, more specifically, a special validation function is required.

 this.validate = function() { if (args.column.validator) { args.newValue = $input.val() var validationResults = args.column.validator(args); if (!validationResults.valid) { return validationResults; } } return { valid: true, msg: null }; }; 

Each column should then have a validator within which the default value will be checked either with a new value coming from the editor or with an existing value, as well as with any other necessary aspects of the check.

 var Validator = function(args) { //validate the existing value or the incoming editor value var value = args.newValue ? args.newValue : args.item[args.column.field] var result = value > 0 return {valid: result} } 

To validate the entire grid, enter a validation method that iterates through each row, looking at each column for the validator. Based on the results of the check, a relational mapping rowIndex -> collection of failures , which should be passed to the native onValidationError event. This allows the subscription to process user notifications of errors. In addition, the results of the check can be used to erase errors in the provision of certain metadata to the grid.

 var validateColumns = function(args){ var failures=[]; for (c in columns) { var column = columns[c] if (column.validator) { if(!column.validator({row: args.row, item: args.item, column: column}).valid){ failures.push({columnIndex: c, column: column, rowIndex: args.row, item: args.item}) } } } return failures; } grid.validate = function() { var rowFailures = {} for (r in data) { //ignore our metadata provider (if applicable) if(r == 'getItemMetadata'){continue;} var failures = validateColumns({item: data[r], row: r}) if(failures.length > 0){ rowFailures[r] = failures; } } if(Object.keys(rowFailures).length > 0){ grid.onValidationError.notify({"rowFailures": rowFailures}, new Slick.EventData()) } } 

Fiddle

+2
source

Source: https://habr.com/ru/post/1203265/


All Articles