TableTools fnSelect() for a button fires twice on row selection

TableTools fnSelect() for a button fires twice on row selection

RSudwartsRSudwarts Posts: 2Questions: 1Answers: 0

[Sincere apologies if this is considered a double-post, I just posted this question on SO but realised that this is the correct place to ask...]

I am attempting to capture row selection via a (DataTable) TableTools button's fnSelect() (to do some pre-processing of the row's data) ie. the user selects a row, some manipulation of the row data is done; then user can click the button.

Problem is that the event (ie selecting the row) appears to fire twice -- the console.log() is clearly output twice...

And I've tried this using various versions of jQuery but always with the same result.

My table's definition is as follows:

$('#example').DataTable( {
    dom: 'T<"clear">lfrtip',
    tableTools: {
      "sRowSelect": "single",
      "aButtons": [
           {
              sExtends:"text",
              sNewLine: "<br/>", 
              sButtonText: "edit",
              fnSelect: function(nButton, oConfig, nRow){

                   // "do something" is output twice.
                  console.log("[edit button's fnSelect()] do something"); 
                  // so this would be a problem...
                  // row_data = this.fnGetSelectedData()[0]);
                 // do some function(row_data){}
           },
     },
   ],
 }

});

And I have a jsfiddle demonstrating this problem/behaviour.

I'd be grateful if anyone could shed some light on what I'm doing wrong (before I yell 'bug'!!)

Many thanks.

Answers

  • RSudwartsRSudwarts Posts: 2Questions: 1Answers: 0

    On closer inspection I see that the first time fnSelect() is hit, this.fnGetSelectedData() === 'undefined'

    So I have sidestepped the problem by using a conditional:

       fnSelect: function(nButton, oConfig, nRow){
          if ( this.fnGetSelectedData() ) {
             row_data = this.fnGetSelectedData()[0]
             ...
          }
       } 
    

    I'm still curious to know if this is expected behaviour from this function....

  • bitDoctorbitDoctor Posts: 9Questions: 1Answers: 0

    I notice that when I first load your jsfiddle page the "edit" button is active and then once I toggle a row selection it becomes inactive. Is there a way to start with it inactive? I really like the concept!

This discussion has been closed.