Sorry noob with Datatables but I need to ask this question. I created a laravel datatable using this api function:
$products = \DB::table('products')
->orderBy('id')
->select('product_id', 'product_title', 'product_image', 'meta_value', 'meta_id')
->get();
return DataTables::of($products)
->addColumn('checkbox', function($product) {
return '<div style="padding-left: 8px"><input id="checkbox-bulk" type="checkbox" name="ids[]" value="'. $product->product_id .'" /></div>';
})
->addColumn('action', function($product) {
$text = 'Hide';
$class='Polaris-Button';
if($product->meta_value && $product->meta_value == 1) {
$text = 'Show';
$class .= ' Polaris-Button--primary';
}
return '<button id="btn-edit-product" type="button" data-id="'. $product->product_id .'" class="'. $class .'"><span class="Polaris-Button__Content"><span class="Polaris-Button__Text">'. $text .'</span></span></button>';
})
->addColumn('image', function($product) {
return '<img src="' . $product->product_image . '" alt="noImage" width="55px" height="55px">';
})
->rawColumns([
'action',
'image',
'checkbox',
])
->make(true);
It has a checkbox which has the id that I need and other columns, here is my datatable:
let datatable = $('#products').DataTable({
serverSide: true,
processing: true,
responsive: true,
ajax: {
url: '{{ route('product.api') }}',
dataSrc: 'data'
},
columns: [
{ data: 'checkbox', name: 'checkbox', 'orderable': false, 'searchable': false },
{ data: 'product_id', name: 'product_id' },
{ data: 'product_title', name: 'product_title' },
{ data: 'image', name: 'image', orderable: false, searchable: false },
{ data: 'action', name: 'action', orderable: false, searchable: false },
]
});
I added a button outside the table to get the ids from the checkbox and process it by bulk, using these
$( datatable.$('input[type="checkbox"]:checked').each(function () {
ids.push($(this).val());
}));
However, after the bulk update I need to change the text and class of the action buttons which was selected and affected by the bulk update, and I tried this
$(datatable.$('input[type="checkbox"]:checked').each(function () {
let btn = $('#btn-edit-product');
btn.toggleClass("Polaris-Button--primary");
}));
However, it only gets the 1st row that was selected not the all the rows and I can't update the button class properly. How do I need to do this that if I select the button after bulk update I need to change all those buttons classes for each row. Thank you.