Domanda

I am generating a html table dynamically and after that using datatable 1.9.4 plugin to enhance my html table's features. My table looks like below

enter image description here

the "x" are checkboxes, I am trying to select checkboxes with respect to a particular row and column combination. I have values in pairs like (row1,col3) then I need to select that particular cell. I tried reading checkbox text from header row and selecting corresponding checkbox but no luck.

How should I proceed with this. I know there are some methods from dataTable but not very well aware of those.

È stato utile?

Soluzione

You lack to tell how exactly you generate those checkboxes. So the following is just how what I would do it. There isnt really difference if the checkboxes is inside a dataTable or anything else.

Example of creating a lot of checkboxes inside a dataTable, all with unique ID's :

for (var row=1;row<=10;row++) {
    dataTable.fnAddData([
        '<input type="checkbox" id="check_'+row+'_1">',
        '<input type="checkbox" id="check_'+row+'_2">',
        '<input type="checkbox" id="check_'+row+'_3">',
        '<input type="checkbox" id="check_'+row+'_4">',
    ]);
}

A shorthand function for retrieving the right checkbox, based on row, col pairs :

function dtCheckBox(row, col) {
   return document.getElementById('check_'+row+'_'+col);
}

Some examples to show the concept is working :

//setter examples, check 20 random checkboxes
for (var i=0;i<20;i++) {
    var row=Math.floor(Math.random()*10)+1,
        col=Math.floor(Math.random()*4)+1;
    dtCheckBox(row, col).checked=true;
}

//getter example, console out the checked status of all checkboxes
for (col=1;col<=4;col++) {
   for (row=1;row<=10;row++) {
       console.log(dtCheckBox(row, col).checked);
   }
}

See the above in action in this fiddle -> http://jsfiddle.net/EpLA7/

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top