EasyUI Forum
April 24, 2024, 03:15:05 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: extend validateRow to consider limitToList  (Read 3125 times)
gordis gmbh
Full Member
***
Posts: 103


View Profile Email
« on: September 16, 2020, 11:48:40 PM »

I have Combobox editors in Edatagrid with limitToList set to true. How can I extend validateRow to consider limitToList as well? Is it possible to have it included as standard feature?
Logged
jarry
Administrator
Hero Member
*****
Posts: 2262


View Profile Email
« Reply #1 on: September 18, 2020, 07:26:26 AM »

You can extend a validate type and apply it to the combobox editor. Please refer to the code below.
Code:
$.extend($.fn.validatebox.defaults.rules, {
  inList: {
    validator: function(value, param){
      const index = param[0].findIndex(row=>row[param[1]]==value);
      return index>=0;
    },
    message: 'Not found'
  }
})
$(function(){
  $('#tt').edatagrid({
    data: data,
    columns: [[
      {field:'itemid',title:'Item ID',width:100,editor:'textbox'},
      {field:'productid',title:'Product',width:300,
        formatter:function(value,row){
          return row.productname;
        },
        editor:{
          type:'combobox',
          options:{
            valueField:'productid',
            textField:'productname',
            data: products,
            limitToList: true,
            required:true,
            validType:{
              inList:[products, 'productname']
            }
          }
        }
      },
    ]]
  });
});
Logged
gordis gmbh
Full Member
***
Posts: 103


View Profile Email
« Reply #2 on: September 18, 2020, 12:43:04 PM »

Thanks Jarry for your suggestion, will try it out. I was looking for a generic solution without having to adjust dozens of editors. I have in between extended the datagrid methods and overwritten the standard 'validateRow':

Code:
$.extend($.fn.datagrid.methods, {
validateRow: function(jq, index){
        return gordisExtendedValidateRow(jq[0], index);
    }
});

$.fn.combobox.defaults = $.extend({}, $.fn.combobox.defaults, {
        validateSelection: true
});

/**
 * Overwritten validateRow method of easyui to perform additional validation.
 * Here, the combobox editors are validated to ensure a valid selection from its list is made.
 */
function gordisExtendedValidateRow(target, index){
    const tr = $.data(target, 'datagrid').options.finder.getTr(target, index);
    if (!tr.hasClass('datagrid-row-editing')){
        return true;
    }

    const vbox = tr.find('.validatebox-text');
    vbox.validatebox('validate');
    vbox.trigger('mouseleave');
    const invalidbox = tr.find('.validatebox-invalid');
    let result = invalidbox.length === 0;
    if (result) {
        // Now check if combobox editors contain valid selection
        const editors = $(target).edatagrid('getEditors', index);
        for(let i=0; i<editors.length;i++) {
            const editor = editors[i];
            if (editor.type === 'combobox') {
                const comboEditor = $(editor.target);
                if(comboEditor.combobox('options').validateSelection === true &&
                    textNotEmpty(comboEditor.combobox('getText')) && _isSelectionValid(editor.target) === false) {
                    comboEditor.combobox('textbox').addClass("validatebox-invalid");
                    result = false;
                    break;
                }
            }
        }
    }
    return result;

    function _isSelectionValid(comboboxComp) {
        const c = $(comboboxComp);
        const opts = c.combobox('options');
        const data = c.combobox('getData');
        let exists = false;
        for(let i=0; i<data.length; i++){
            if (c.combobox('getText') === data[i][opts.textField]){
                exists = true;
                break;
            }
        }
        return exists;
    }
}
The extended validation for combobox editors will be skipped if the option validateSelection is set to false. 
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!