Select row is not working as expected

Select row is not working as expected

ShradhaShelkeShradhaShelke Posts: 2Questions: 1Answers: 0

I have created DataTable using ->
jQDatatable = $('#productDemands').DataTable( {
data: dataSet,
select: true,
columns: [ a,b,c] });

then with other function I am updating same table like -->
updateDataTable: function(data){
jQDatatable.row.add( [
p,q,r
] ).draw();
},

Now I used select so that I can select a row and then update the details as and when required -->

$('#productDemands tbody').on( 'click', 'tr', function () {
$(this).toggleClass('selected');
selectRowData = jQDatatable.row('.selected').data();
console.log("Selected Row data is ==> ");
console.log(selectRowData);
});

after updating table with new rows using updateDataTable function my select operation is not working as expected for single select it is running twice - first time it is selecting data properly but second time row data is blank [WHY IT IS running TWO times ????]

console log -->

ERI_ProductDemand.js:167 Selected Row data is ==>
ERI_ProductDemand.js:168 (3) ["a", "b", "c"]
ERI_ProductDemand.js:167 Selected Row data is ==>
ERI_ProductDemand.js:168 undefined

You can see line numbers are repeated, is it due to adding rows in same table using -->
jQDatatable.row.add( [
p,q,r
] ).draw();
},

Please help I am stuck since long time :neutral:

Answers

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923

    Do you have this code inside a function that is executed more than once?

    $('#productDemands tbody').on( 'click', 'tr', function () {
    $(this).toggleClass('selected');
    selectRowData = jQDatatable.row('.selected').data();
    console.log("Selected Row data is ==> ");
    console.log(selectRowData);
    });
    

    Kevin

  • ShradhaShelkeShradhaShelke Posts: 2Questions: 1Answers: 0

    yes this code is inside a function but it will be called once

    first time - on button click -> my datatable will be created that is jQDatatable lets say with createDataTable(data) function
    second time - on same button click I have managed counter if(counter !== 1) then addition of row is getting executed lets say with updateDataTable(data) function

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

This discussion has been closed.