Last active
March 22, 2023 18:45
-
-
Save arif98741/ecb4d59d53c0d88250b70b3607c20c20 to your computer and use it in GitHub Desktop.
Add Custom Column to Yajra Datatable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var table = $('.contacts_table').DataTable({ | |
processing: false, | |
serverSide: true, | |
pageLength: 15, | |
lengthChange: true, | |
lengthMenu: [[10, 25, 50, -1], [10, 25, 50, "All"]], | |
dom: 'Bfrtip', | |
buttons: [ | |
{ | |
extend: 'excel', | |
text: 'Excel', | |
className: 'btn btn-default', | |
exportOptions: { | |
columns: [ 0, 1, 2, 3, 4] | |
} | |
}, | |
{ | |
extend: 'pdf', | |
text: 'PDF', | |
orientation: 'landscape', | |
className: 'btn btn-default', | |
exportOptions: { | |
columns: [ 0, 1, 2, 3, 4] | |
}, | |
customize: function (doc) { | |
doc.content[1].table.widths = | |
Array(doc.content[1].table.body[0].length + 1).join('*').split(''); | |
} | |
}, | |
{ | |
extend: 'print', | |
text: 'Print', | |
className: 'btn btn-default', | |
exportOptions: { | |
columns: [ 0, 1, 2, 3, 4] | |
} | |
} | |
], | |
ajax: "{{ url()->current() }}", | |
columns: [ | |
{data: 'name', name: 'name'}, | |
{data: 'mobile', name: 'mobile'}, | |
{data: 'email', name: 'email'}, | |
{data: 'status', name: 'status'}, | |
{data: 'created_at', name: 'created_at'}, | |
{data: 'deleted_at', name: 'deleted_at'}, | |
{data: 'action', name: 'action', orderable: false, searchable: false}, | |
] | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @return JsonResponse | |
* @throws \Exception | |
*/ | |
public function index(Request $request) | |
{ | |
if ($request->ajax()) { | |
$data = Contact::with(['user'])->where('created_by', self::getUserId()) | |
->orderBy('created_at', 'desc'); | |
return Datatables::of($data) | |
->addColumn('status', function ($row) { | |
return '<span class="badge badge-' . $row["status_color"] . ' text-white">' . $row['status'] . '</span>'; | |
}) | |
->addColumn('created_at', function ($row) { | |
return $row->created_at->format('Y-m-d, h:i:sa'); | |
}) | |
->addColumn('action', function ($row) { | |
$viewBtn = ''; | |
$viewBtn .= '<a class="btn btn-sm btn-info" href="#"><i | |
class="fa fa-eye"></i> </a> '; | |
$viewBtn .= '<a href = "#" data-toggle = "modal" | |
data-target="#edit" data-name = "' . $row['name'] . '" | |
id = "' . $row->id . '" | |
class="btn-sm btn global-background text-white edit-btn" ><i | |
class="fa fa-pencil-alt" ></i ></a> | |
<form style = "display: inline-block" class="" | |
action = "' . route('backend.contact.contact.destroy', $row->id) . '" | |
method = "POST" >' . csrf_field() . method_field('DELETE') . '<button | |
class="btn btn-danger btn-sm delete-user" type="button" ><i | |
class="fa fa-trash-alt" ></i ></button > | |
</form >'; | |
if ($row->deleted_at != null) { | |
$viewBtn .= '<a class="btn btn-sm btn-info" href="#"><i | |
class="fa fa-undo"></i> Restore</a>'; | |
} | |
return $viewBtn; | |
}) | |
->rawColumns(['status', 'created_at', 'action']) | |
->make(true); | |
} | |
$data = [ | |
'title' => 'Contacts', | |
'statuses' => $this->getContactStatuses(), | |
]; | |
return view('backend.contact.contact.index')->with($data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment