Using datatables with examples

629 0 0 0

Last Updated : 2024-03-29 15:45:26

Here are different examples on how to use datatables library into your laravel project controllers.

//admin index page 
if(\Request::ajax()) {
return datatables()->of(\App\Bill::all())
->addcolumn('Parl_and_Session',function($theModelObject){
return "{$theModelObject->parlNumber} <br> {$theModelObject->session}";
})
->addcolumn('Bill_No',function($theModelObject){
return $theModelObject->billNumberPrefix . "-" . $theModelObject->billNumber;
})
->addcolumn('Actions', function($theModelObject){
return "
<i class='fa fa-gavel' aria-hidden='true' id='eventsIcon-{$theModelObject->id}' title='Events'></i>
<i class='fa fa-pencil-square-o' aria-hidden='true' id='editIcon-{$theModelObject->id}' title='Edit'></i>
<i class='fa fa-trash-o' aria-hidden='true' id='deleteIcon-{$theModelObject->id}' title='Delete'></i>
";
})
->rawColumns(['Parl_and_Session','Bill_No','Actions'])
->make(true);
}

$items = [];
return view('admin.federalBills.all',['items'=>$items]) ;

Another example would be :


if(\Request::ajax()) {
//---------------- SEARCH AND SORT START
$draw = $_GET['draw'];
$row = $_GET['start'];
$rowPerPage = $_GET['length'];
$columnIndex = $_GET['order']['0']['column'];
$columnName = $_GET['columns'][$columnIndex]['data'];
$columnSortOrder = $_GET['order']['0']['dir'];
$searchValue = $_GET['search']['value'];
$searchQuery = '';

//Total number of records without filtering
$totalRecords = \App\Bill::count();

$bills = new Bill();
$bills = $bills->newQuery();
$bills->where("title" , 'like', "%{$searchValue}%");
$bills->orwhere("title" , 'like', "%{$searchValue}%");

if( (stripos($searchValue, 'c-') !== false AND stripos($searchValue, 'c-') == 0) or (stripos($searchValue, 's-') !== false AND stripos($searchValue, 's-') == 0) ) {

$searchvalueParts = explode("-", $searchValue);
$bills->orWhere(function($query) use ($searchvalueParts) {
$query->where('billNumberPrefix', strtoupper($searchvalueParts['0']) )
->where('billNumber', 'like', "{$searchvalueParts['1']}%");
});

} elseif( is_numeric(substr($searchValue, 0, 2)) and substr($searchValue, 2, 1) == '-' ) {

$searchvalueParts = explode("-", $searchValue);
$bills->orWhere(function($query) use ($searchvalueParts) {
$query->where('parlNumber', $searchvalueParts['0'] )
->where('session', 'like', "{$searchvalueParts['1']}%");
});

} else {
$bills->orwhere("billNumberPrefix" , 'like', "%{$searchValue}%");
$bills->orwhere("billNumber" , 'like', "%{$searchValue}%");
$bills->orwhere("session" , 'like', "%{$searchValue}%");
$bills->orwhere("parlNumber" , 'like', "%{$searchValue}%");
}

//Fetch records
if($columnName == 'Bill_No') {
$bills->orderby("billNumberPrefix" , $columnSortOrder);
$bills->orderby("billNumber" , $columnSortOrder);
} else if($columnName == 'Parl_and_Session') {
$bills->orderby("parlNumber" , $columnSortOrder);
$bills->orderby("session" , $columnSortOrder);
} else {
$bills->orderby($columnName , $columnSortOrder);
}


//Total number of records with filtering
$totalRecordsWithFilter = $bills->count();

$bills->skip($row);
$bills->take($rowPerPage);
$dataAll = $bills->get(['id','title', 'parlNumber', 'session', 'billNumber', 'billNumberPrefix' ]);

$data = [];
foreach($dataAll as $bill) {
$data[] = array(
'title'=> $bill->title,
'Parl_and_Session'=> "{$bill->parlNumber}-{$bill->session}" ,
'Bill_No'=> "{$bill->billNumberPrefix}-{$bill->billNumber}",
'Actions'=> "
<i class='fa fa-gavel' aria-hidden='true' id='eventsIcon-{$bill->id}' title='Events'></i>
<i class='fa fa-clone' aria-hidden='true' data-id='{$bill->id}' id='cloneIcon-{$bill->id}' title='Clone'></i>
<i class='fa fa-pencil-square-o' aria-hidden='true' id='editIcon-{$bill->id}' title='Edit'></i>
<i class='fa fa-trash-o' aria-hidden='true' id='deleteIcon-{$bill->id}' title='Delete'></i>
"
);
}

$response = array(
"draw" => intval($draw),
"iTotalRecords" => $totalRecordsWithFilter,
"iTotalDisplayRecords" => $totalRecordsWithFilter,
//"iTotalDisplayRecords" => $totalRecords,
"aaData" => $data,
);

return json_encode($response);
//---------------- SEARCH AND SORT END
}


sss

Mohammed Anwar

Mohammed Anwar

Experienced technical lead PHP, MySQL and Laravel Developer for 15+ years, with proven ability to develop and create high-quality and optimized web applications. Great ability to build and optimize database design, schema and queries. Versed programing trainer and instructor delivering web courses and helping others to get into the field in a timely manner. Fast and eager learner for new technologies .