SpreadJS提供了丰富的数据验证功能,可以想Excel一样在未通过验证时高亮提示,也可以在提交数据前调用接口检查数据合法性。同时SpreadJS也可以自定义数据验证规则,实现业务所需的特定逻辑。
1. 自定义验证条件,在evaluate方法中我们加入所需逻辑,返回验证结果即可,示例中只要等于期待值即为通过验证。
function CustomerCondition(compareType, expected, formula){
var self = this;
self.ignoreBlank = false;
self.conditionType = "CustomerCondition";
self.compareType = compareType;
self.expected = expected;
self.formula = formula;
};
CustomerCondition.prototype = new GcSpread.Sheets.CellValueCondition();
CustomerCondition.prototype.evaluate = function(evaluator, baseRow, baseColumn, actualValue){
if(actualValue === this.expected){
return true;
}
else{
return false;
}
}
2. 初始化验证条件,并将添加加入验证器中,在单元格设置验证器。
var cCondition = new CustomerCondition(GcSpread.Sheets.GeneralCompareType.EqualsTo, 4);
var validator1 = new GcSpread.Sheets.DefaultDataValidator(cCondition);
validator1.inputTitle = 'Please choose ';
validator1.ignoreBlank =false;
validator1.type = GcSpread.Sheets.CriteriaType.Custom;
activeSheet.setDataValidator(3,3, validator1);
activeSheet.setDataValidator(3,4, validator1);
spread.highlightInvalidData(true);
activeSheet.setValue(3, 3, -1);
activeSheet.setValue(3, 4, 4);
SpreadJS丰富的拓展性使得我们的开发变得更加灵活方便。
RunJS演示地址:http://runjs.cn/detail/vxggevqk
更多资源
SpreadJS中文学习指南:http://demo.grapecity.com.cn/SpreadJS/TutorialSample/#/samples
SpreadJS在线英文产品文档:http://sphelp.grapecity.com/webhelp/SpreadJSWeb/webframe.html#welcome.html
如果您对SpreadJS产品感兴趣,可以到官方网站下载试用:/developer/spreadjs
如果你有疑问,可以到GCDN论坛获得技术支持:http://gcdn.grapecity.com.cn