Strange bug when checking for duplicate data, occurs only after reordering columns.
Strange bug when checking for duplicate data, occurs only after reordering columns.
So I have been working on my own editor for datatables for some time now, and I am currently trying to implement a check for data that already exists in the datatable.
The code below works fine initially, but as soon as i reorder the datatable by clicking the column-header it doesn't skip the selected row which I need it to do.
I have tried to console.log(dt.cell('.selected', 0).data() === dt.row(index).data().DT_RowId)
which prints the correct boolean value for every row, both initially and after the reordering of columns, but it still doesnt skip the selected row.
I have left out some code that is irrelevant for this questions, which is why you see unused variables.
A working example can be found here: https://plnkr.co/bF69ueTf8xZmX9yEEx7e?p=preview
(Error happens when: Reorder column -> select row and click edit -> submit without changing the input)
//Input validation for text-fields
var initValidation = function(tableObj){
var dt = tableObj.s.dt;
var isValid = false;
var errorcount = 0;
var matchcount = 0;
var errorLabel, unique, ports, input, selected;
//Looping through all inputs
$('form[name="altEditor-form"] *').filter(':input').each(function( i ){
//Checking for dublicate data in columns with unique attribute
if($(this).attr("data-unique") === "true"){
input = $(this).val();
selected = dt.cell('.selected', 0).data();
//Looping through all data from the column
$.each(dt.column(i+1).data(), function(index, value) {
//Skipping data from the selected row
if(typeof dt.row({selected:true}, index).data() === "undefined" || selected !== dt.row(index).data().DT_RowId){
if(input !== "" && input.toLowerCase() === value.toLowerCase()) {
$(errorLabel).html("Error: Duplicate data is not allowed.");
$(errorLabel).show();
matchcount++
return false;
}
}
});
}
}
});
//When no errors in input and no matches are found
if(errorcount == 0 && matchcount == 0){
isValid = true;
}
return isValid;
}
It really confuses me that it enters this if-statement, even though the statement returns true and that it only occurs after reordering:
if(typeof dt.row({selected:true}, index).data() === "undefined" || selected !== dt.row(index).data().DT_RowId)