Title: Editable Datagrid with Validators
Post by: Andy1980 on March 04, 2014, 04:00:01 AM
Hi Experts, how would you manage it, to get a editable datagrid with fields "from" and "to", which both should a validator like "from" must be less equal to field "to" I did the following: $.extend($.fn.validatebox.defaults.rules, { $.extend($.fn.validatebox.defaults.rules, { from: { validator: function(value, param){ var ed1 = $('#Tolerances_grid').datagrid('getEditor', {index:my_single_update_index,field:'TO'}); var to_value = $(ed1.target).val() return to_value >= value; }, message: 'From must be lesser than or equals To.' }, to: { validator: function(value, param){ var ed2 = $('#Tolerances_grid').datagrid('getEditor', {index:my_single_update_index,field:'FROM'}); var from_value = $(ed2.target).val() return from_value <= value; }, message: 'To must be greater than or equals From.' } });
And here is the field defintion: {field:'FROM',title:'From',width:70, editor:{ type:"validatebox", options:{ validType:"from[]" } }}, {field:'TO',title:'To',width:70, editor:{ type:"validatebox", options:{ validType:"to[]" } }},
The code is working fine, but I think it can be much easier, with a row validator perhaps? If yes, can u give me simple example ? And i don't like this lines here: var ed2 = $('#Tolerances_grid').datagrid('getEditor', {index:my_single_update_index,field:'FROM'}); var from_value = $(ed2.target).val()
I wonder, why i can't use the action getValue() from the editor to get value? Any ideas? Thanks.
Title: Re: Editable Datagrid with Validators
Post by: stworthy on March 04, 2014, 07:05:58 AM
Another solution is to extend the validators as below: $.extend($.fn.validatebox.defaults.rules, { from: { validator: function(value, param){ return value < $(param[0]).val(); }, message: 'The value is too big.' }, to: { validator: function(value, param){ return value >= $(param[0]).val() }, message: 'The value is too small.' } })
When editing a row, apply these validator to corresponding editors. var dg = $('#dg'); dg.datagrid('beginEdit', index); var ed1 = dg.datagrid('getEditor', {index:index,field:'FROM'}); var ed2 = dg.datagrid('getEditor', {index:index,field:'TO'});
$(ed1.target).attr('id','_from').validatebox({ required:true, validType:'from["#_to"]' }); $(ed2.target).attr('id','_to').validatebox({ required:true, validType:'to["#_from"]' });
|