I'm having a html table with checkbox in each rows. On checking each checkbox I'm pushing the row values to an array named checkboxArrayForOperations
.
HTML
<table>
<?php
$i = 0;
foreach ($databaseValue as $stValue) {
$i++;
?>
<tr>
<input type="text" id="stName<?php echo $i;?>" value="<?php echo $stValue['stName'];?>">
<input type="text" id="stId<?php echo $i;?>" value="<?php echo $stValue[0]['stId'];?>">
<td><input type="checkbox" class="checkboxForOperations" value="<?php echo $i;?>" onclick="checkBoxForOperations(this)"></td>
</tr>
<?php
}
?>
</table>
JS
var checkboxArrayForOperations = []; // global array
function checkCheckBoxForOperations(obj){
var checkboxValue = obj.value;
if(obj.checked) {
checkboxArray = [];
checkboxArray["stName"] = $('#stName'+checkboxValue).val();
checkboxArray["stId"] = $('#stId'+checkboxValue).val();
checkboxArrayForOperations.push(checkboxArray);
} else {
var itemArrayForRemoval = [];
itemArrayForRemoval["stName"] = $('#stName'+checkboxValue).val();
itemArrayForRemoval["stId"] = $('#stId'+checkboxValue).val();
var indexItemArrayForRemoval = index(itemArrayForRemoval);
checkboxArrayForOperations = checkboxArrayForOperations.filter( function( el ) {
return !(JSON.stringify(el) in itemArrayForRemoval);
} );
}
console.log(checkboxArrayForOperations);
}
function index(arr) {
return arr.reduce(function(acc, item){
return acc[JSON.stringify(item)] = item, acc
}, {})
}
On uncheck I want to remove the elements (added as array) corresponding to that unchecked row. I'm referring this answer. But the array is not getting removed from checkboxArrayForOperations
.
What am I doing wrong? Can anyone help me to fix this. Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire