Row selection data. Tabular rails. How to get a list of ids of the selected rows.

Hello I am new to ruby โ€‹โ€‹on rails and javascript and I want to use dataTable to select multiple rows in my table. Then I would like to have a list of all the selected row ids.

I was able to select multiple rows thanks to the code available on the dataTable website. This is the code:

<script>
$(document).ready(function() {

  var selected =[];

  $("#assureur").dataTable({
    
    "rowCallback": function(row,data){
      if($.inArray(data.DT_RowId, selected) !==-1){
        $(row).addClass('selected');
      } 
    }
  });

  $('#assureur tbody').on('click', 'tr', function(){
    var id = this.id;
    var index = $.inArray(id, selected);

    if( index === -1){
      selected.push(id);
    } else {
      selected.splice(index,1);
    }

    $(this).toggleClass('selected');
  });

});
</script>
      

Run codeHide result


However, I don't understand how to get the list of selected rows.

I have searched the internet but I don't understand how their code works. They talk about using TableTool and fnGetSelected , but I don't know how to use it.

I've read about alert , but I don't want to show the alert. I would prefer to have an array with all the selected row ids in order to use that array later. p>

Then how can I use this array no longer with javascript, but with the Ruby language? I read about using Ajax or using a hidden field tag and then fetching the parameters from the controller ... but how does this work? What do I need to overlay on the controller to get the $ var defined in my javascript?

thanks for the help

+3


source to share


1 answer


You can use Jquery call:



var $yourVar = $("#assureur").find('tbody tr.selected');

      

+1


source







All Articles