I have an index table with a user that have an information status of verification based on passed on ID every time user register. Every status has 3 value which is 'Draft','Need Verify','Verified' under DB name status_records based on this picture. I want to make colouring text based on ID. For example, If user under 'Draft', the text status in index datatable will colouring text 'Draft' with blue colour while if user under 'Verified' the colour for text will be green.
I'm using Datatable for showing the index. For value under status column, it only passed the ID and just showing the standard black colour. Refer the picture below,
I'm still new to Datatable and i do not know where to put the logic whether in controller or JS index blade.
But here is my mode user.php for relationship with statusrecord :
public function statusrecord(){
return $this->hasOne(StatusRecord::class, 'id', 'status_record');
}
UserController
public function index(){
$record = User::with('ownership','assetdpa','statusrecord')->where('asset_type_id','=',1)->orderby('id', 'asc')->get();
if(request()->ajax()){
return DataTables()::of($record)
->addIndexColumn()
->addColumn('status', function($record){
if($record->statusrecord){
return '<span class="badge badge-primary" >Active</span>';
}else{
return '<span class="badge badge-danger">Deactive</span>';
}
})
->rawColumns(['action', 'fullOrganisation','fullLocation','fullCategory'])
->make(true);
}
return view('asset.index',compact('record'));
}
index.blade.php
$(document).ready(function(){
$('#item_table').DataTable({
columnDefs: [{data:('statusrecord'),
render: function ( statusrecord, type, row ) {
var color = 'black';
if (id = 1) {
color = 'green';
}
if (id = 2) {
color = 'blue';
}
if (id = 3) {
color = 'red';
}
return '<span style="color:' + color + '">' + data + '</span>';
}
}]
})
})

