Search code examples
javascriptjqueryjquery-pluginshandsontable

Validate Data jQuery Handsontable input


Q1:What is the best way to validate the data entered by user in jQuery Handsontable before sending to Server?

I have read this article Upload jQuery Handsontable input

Is there any integrate solution? etc integrating to the jquery validation plugin,if not, how about using the onbeforechange() method?

Q2:What's more, I have initiated a 100 rows table , but it's probably the user would input only 50 rows, if I use the code below:

$('#btnGo').click(function() { 
  var rowList = $("#example9grid").handsontable("getData"); 
  $('#simple').val(JSON.stringify(rowList)); 
  console.log(rowList); 
});​ 

the rowList would return 50 data rows and 50 empty rows.

How to remove all the empty rows?


Solution

  • A1:Thanks for Marcin's reply,I have solved this problem by using these code below:

    onBeforeChange: function (data) {
          for (var i = 0, ilen = data.length; i < ilen; i++) {
                if (data[i][0] > 0) { //if it is not first row
                    if(data[i][1]==0){ //if it is the first column
                            //some validate logic here
                }else if(data[i][1]==1){//if it is the second column
                            //some validate logic here
                        }
                }
            }
          };
    

    A2:I have remove the empty row by using these code below:

    rowList = $("#dataTable").handsontable("getData");
    rowList = $.grep(rowList,function(array,index){
                ...write your logic here
    });