Hello Guys!
I would like to add a column with a given name that is passed by variable and use the same variable to access the object property to render the content of the column. Is it possible?
I think you can do something like below:
$dt = Datatables::of($model);
if ($request->has('field') {
$dt->addColumn($request->field, 'content here');
}
return $dt->make(true);
Thanks Arjay for the response.
I have a collection of objects. Every object is built from JSON that is retrieved from the 'objects' table and has given type. Another table holds information about fields specific to given type of objects. I want to display objects of given type in a table. It's quite easy to generate columns based on the information stored in a table as to what fields belongs to what type of object but I have no idea how to refer to object property itself based on the same information.
public function dsDatatable($objectTypeId){
$jsonObjects = new Collection;
$objects = Object::where('objectTypeId',$objectTypeId)->get();
foreach($objects as $object){
$jsonObjects -> push( json_decode($object->body));
}
$datatable = Datatables::of($jsonObjects);
$propertyTypes = PropertyType::where('objectTypeId',$objectTypeId)->get();
foreach($propertyTypes as $propertyType){
$datatable->addColumn($propertyType->name, function($jsonObject){
return $jsonObject-> //reference to the field name which is the same as column name.
});
}
return $datatable->make(true);
}
@salehliam for adding columns dynamically, I use transformers.
Closing old questions. Please open a new one if still having issues. Thanks!
@yajra Is it possible to please share the code for this dynamic column thing?
@xyrintech you could do something like @yajra said on ajax:
$dt = Datatables::of($model);
if ($request->has('field') {
$dt->addColumn($request->field, 'content here');
}
return $dt->make(true);
and on columns:
$columns = [
[ 'data' => 'name', 'name' => 'name', 'title' => 'Name' ]
];
if($request->has('field'){
$columns[] = [ 'data' => $request->field, 'name' => $request->field, 'title' => 'Field Title'];
}
return $columns;
How would this be applied to the RawColumns api?
Most helpful comment
I think you can do something like below: