您好,根据官方示例简单修改一下即可实现,我实现了一版,参考以下代码:
- import 'bootstrap.css';
- import '@grapecity/wijmo.styles/wijmo.css';
- import './styles.css';
- //
- import * as wijmo from '@grapecity/wijmo';
- import * as grid from '@grapecity/wijmo.grid';
- import { countries, getData } from './data';
- //
- document.readyState === 'complete' ? init() : window.onload = init;
- //
- function init() {
- // create a CollectionView
- let view = new wijmo.CollectionView(getData(), {
- sortDescriptions: ['country'],
- getError,
- });
- //
- // CollectionView validation function
- function getError(item, propName) {
- switch (propName) {
- case 'country':
- return countries.indexOf(item.country) < 0 ? 'Invalid Country' : '';
- case 'downloads':
- case 'sales':
- case 'expenses':
- return item[propName] < 0 ? 'Negative values not allowed!' : '';
- case 'active':
- return item.active && item.country.match(/^(US|Germany)$/)
- ? 'Active items not allowed in the US or Germany!'
- : '';
- case null:
- let errors = [];
- for (let key in item) {
- let err = getError(item, key);
- if (err)
- errors.push(err);
- }
- return errors.length > 1
- ? 'this item has ' + errors.length + ' errors'
- : (errors.length == 1 ? errors[0] : null);
- case undefined:
- let errorsAll = [];
- for (let key in item) {
- let err = getError(item, key);
- if (err)
- errorsAll.push(err);
- }
- return errorsAll.length? errorsAll:null;
- }
- return null;
- }
- //
- // create grid for editing with validation
- let theGrid = new grid.FlexGrid('#theGrid', {
- itemsSource: view
- });
- //
- // use getError to provide form validation
- let theItem = {};
- //
- document.getElementById('theForm').addEventListener('input', e => {
- });
- //
- document.getElementById('theForm').addEventListener('submit', e => {
- e.preventDefault()
- // debugger
- view.sourceCollection.forEach((item)=>{
- console.log(view.getError(item));
- });
- });
- }
复制代码
测试地址:
https://www.grapecity.com/wijmo/ ... s/Validation/purejs |