I am using Laravel with DataTables to display data, the first column of my table is checkboxes and the last one is buttons.
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="" />')
->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);
}
function postdata(Request $request)
{
$validation = Validator::make($request->all(), [
'ID_User' => 'required',
'Piece_name' => 'required',
'ID_Ligne' => 'required',
'order' => 'required',
]);
$error_array = array();
$success_output = '';
if ($validation->fails())
{
foreach($validation->messages()->getMessages() as $field_name => $messages)
{
$error_array[] = $messages;
}
}
else
{
if($request->get('button_action') == "insert")
{
$pdr = new Panier([
'ID_User' => $request->get('ID_User'),
'ID_Piece' => $request->get('Piece_name'),
'ID_Ligne' => $request->get('ID_Ligne'),
'order' => $request->get('order')
]);
$pdr->save();
$success_output = '<div class="alert alert-success">Commande ajouté</div>';
}
}
$output = array(
'error' => $error_array,
'success' => $success_output
);
echo json_encode($output);
}
View:
$(document).ready(function() {
$('#pdr_table').DataTable({
"processing": true,
"serverSide": true,
"ajax": "",
"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', '.pdr_checkbox', function(){//How to color the entire line and get all the values of that line when checked an do the opposite when unchecked?
});
$(document).on('click', '.Ajouter_au_panier', function(){//form_popup_when_click_on_button_from_the_last_column
$('#pdrModal').modal('show');
$('#pdr_form')[0].reset();
$('#form_output').html('');
$('#piece').text('PDR');
});
$('#pdf_form').on('submit', function(event){//How to get all the values of the line corresponding to the button clicked?
event.preventDefault();
var form_data = $(this).serialize();
$.ajax({
url:"",
method:"get",
data:form_data,
dataType:"json",
success:function(data)
{
if(data.error.length > 0)//Check the required fields if empty
{
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//no empty field
{
$('#form_output').html(data.success);
$('#pdr_form')[0].reset();
$('#pdr_table').DataTable().ajax.reload();
}
}
})
});
How to hover the line when check the box and do the opposite when uncheck it and get all the values of the corresponding line?
How to get all the values of the line corresponding to the button clicked?
Aucun commentaire:
Enregistrer un commentaire