1

I am developing an application in Laravel with DataTables where i display data form Mysql database. How to do multiple selection with line color for each selection ? like this link but with multiple selection and how to get values from the selected line not just the line id and how to get the current line values when click on the corresponding button of that line ?

Each button of the last column open a form with a submit button, the submit function is for all the blue buttons.

This is a screenshot of the app:

enter image description here

Here is code:

$(document).ready(function() {
 $('#pdr_table').DataTable({
    "processing": true,
    "serverSide": true,
    "ajax": "{{ route('ajaxdata.getdata') }}",
    "columns":[
        { "data": "checkbox", orderable:false, searchable:false},
        { "data": "ID_Piece" },
        { "data": "Designation" },
        { "data": "Status" },
        { "data": "action"}
    ],
    //"order": [[ 0, "asc" ]],
    'rowCallback': function(row, data, index){ 
        if(data.Status == 'Disponible'){ 
            $(row).find('td:eq(3)').css('background-color', 'green').css('color', 'white'); 
        }
        if(data.Status == 'Indisponible'){ 
            $(row).find('td:eq(3)').css('background-color', 'red').css('color', 'white'); 
        } 
    }
 });    

$(document).on('click', '.Ajouter_au_panier', function(){
    $('#pdrModal').modal('show');
    $('#pdr_form')[0].reset();
    $('#form_output').html('');
    $('#piece').text('PDR'); 
});

$(document).on('click', '.pdr_checkbox', function(){//How to color the entire line and get all the values of that line

});

$('#pdr_form').on('submit', function(event){//How to get all the values for the line of that button
    event.preventDefault();
    var form_data = $(this).serialize();
    $.ajax({
        url:"{{ route('ajaxdata.postdata') }}",
        method:"get",
        data:form_data,
        dataType:"json",
        success:function(data)
        {
            if(data.error.length > 0)
            {
                var error_html = '';
                for(var count = 0; count < data.error.length; count++)
                {
                    error_html += '<div class="alert alert-danger">'+data.error[count]+'</div>';
                }
                $('#form_output').html(error_html);
            }
            else
            {                            
                $('#form_output').html(data.success);
                $('#pdr_form')[0].reset();
                $('#pdr_table').DataTable().ajax.reload();
            }
        }
    })
});

Controller:

function getdata()
    {
     $pdrs = Pdrs::select('ID_Piece', 'Designation', 'Status');
     return DataTables::of($pdrs)
            ->addColumn('checkbox', '<input type="checkbox" name="pdr_checkbox[]" class="pdr_checkbox" value="{{$ID_Piece}}" />')
            ->rawColumns(['checkbox','action'])
            ->addColumn('action', function($pdr){
                return '<a href="#" class="btn btn-xs btn-primary Ajouter_au_panier" id="'.$pdr->ID_Piece.'"><i class="glyphicon glyphicon-shopping-cart"></i> Ajouter au panier</a>';})
            ->make(true);            
    }
5
  • And how you are deciding those colors, as there are different colors showing the image ? Commented Jan 23, 2019 at 10:07
  • I mean hover the line when click on checkbox just a color that differentiates the selected lines from the non-selected lines. Commented Jan 23, 2019 at 10:10
  • You did not implement any of the code from the link you provided. You need to add a select property to your config. Commented Jan 23, 2019 at 10:13
  • The link i provided is for comprehension only. Commented Jan 23, 2019 at 10:17
  • 1
    I updated my post, take a look. Commented Jan 23, 2019 at 14:56

1 Answer 1

1

You can use

select: {
   style:'multi',
},

if you don't intend to use style:'multi' use CTRL+Select for multiple selection under style:'os', as given in the documentation in the link provided by you. Check Here

Then, you can do a $.each on the selected checkboxes within the ajax click function and form the data variable that you want to submit to the controller. Also, please follow the code of the documentation.

$('#someSubmitButton').on('submit', function(e){
  //Place submit button within the form
  var form = this;
  //Define your dataTable to a variable - here it is table
  var rows_selected = table.column(0).checkboxes.selected();

  // Iterate over all selected checkboxes
  $.each(rows_selected, function(index, rowId){.... Your other code goes 
  here based on how you want to handle.....}
Sign up to request clarification or add additional context in comments.

10 Comments

Can you provide the working code, $('#pdr_table').DataTable({...
I am adding the checkboxes manually from the controller, this code will not work for me.
It will still work, $.each doesn't mind, whether you load data from controller or, the datatable loads it for you. It is just a script you need to put in your submit function, and form your data variable and pass into ajax function. Incase you are generating the checkboxes manually, don't refer to the datatable link, it wont help you much. For marking the row , you have to invoke an onclick function..and change the css attribute for the background-colour for the row.
After $('#pdr_form').on('submit', function(event){ and before $.ajax({
And for the "how" i have already wrote a sample items variable
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.