Alfred Ace Alfred Ace - 5 months ago 37
Javascript Question

how to get previously selected rows of a bootstrap dataTable while searching

I have used a bootstrap DataTable to show data. At the same time i have a HTML checkbox as a column of that dataTable. I have a submit button to get the selected checkbox's value to do some stuff. I am fetching the following problem while select checkboxes under searching.

enter image description here

When i select all checkboxes and Click Save button I got all the values. It works as my expectation.

But Unexpected occures when i search via built-in search box of bootstrap dataTable.Here is the pictorial view

enter image description here

In this case when i press Save i got just the two value though all other values are still selected. How can i get all the selected values while searching

Here is my jquery code of Save button of getting the checkboxes value

$("#btnSave").click(function () {
var checkboxes = document.getElementsByName('foo');
var vals = "";
for (var i = 0, n = checkboxes.length; i < n; i++) {
if (checkboxes[i].checked) {
vals += "," + checkboxes[i].value;
}
}
....
//Other stuff
....
}


Please help me.

Answer

I got my job done like this

  var table = $('#tblReportList').dataTable();

When i was searching through dataTable, i was getting just the selected values matching the search criteria. But the previously selected values was disappeared. To get the previously selected value i used the following code...

 table.$('input[type="checkbox"]').each(function () {
     if (!$.contains(document, this)) {
      if (this.checked) {
        previousVal = previousVal + ',' + this.value;
       }
     }
 });  

Now, for getting the selected values of search I used the previous code posted before. And that was

 var checkboxes = document.getElementsByName('foo');
 var vals = "";
 for (var i = 0, n = checkboxes.length; i < n; i++) {
     if (checkboxes[i].checked) {
        vals += "," + checkboxes[i].value;
     }
 } 

So all selected checkbox's values are

var allValues=previousVal+vals;
Comments