Tablebuilder customization
Created 8 years ago by renaldasHello. I have two columns in my table builder: start_date and due_date. First of all, I want to add class to due_date if that date has passed. How can I do it? Can I also add third column days_left, that would take current date and due_date and show how many days left?
Lets say I have very simple table with only two datetime fields: start_date and due_date.
As you can see in snippet, I want to add class "red" to due_date. But only if due_date is smaller than todays date. Can I use Carbon in Tablebuilder or something like this to compare date?
Also as you said i can use wrapper. But I want to add wrapper only if date has passed.
diff is not stored in database, it should be calculated every time i open table. So here i should use Presenter?
//fields migration
protected $fields = [
'start_date' => [
'type' => 'anomaly.field_type.datetime',
'config' => [
"mode" => "date",
]
],
'due_date' => [
'type' => 'anomaly.field_type.datetime',
'config' => [
"mode" => "date",
]
],
];
//stream
protected $assignments = [
'start_date',
'due_date',
];
//Tablebuilder colums
protected $columns = [
'start_date' => [
'sort_column' => 'start_date',
'wrapper' => '{value.start_date}',
'value' => [
'start_date' => 'entry.start_date',
]
],
'due_date' => [
'class' => 'red'
],
'diff' => [
'sort_column' => 'diff',
'wrapper' => '{value.diff}',
'value' => [
'start_date' => 'entry.start_date',
'due_date' => 'entry.due_date',
'diff' => '???',
]
],
];
https://codeshare.io/aYWy8E
Hello renaldas.
To do what you need you should do it in the presenter. You can create two methods like these into your stream presenter:
public function diff()
{
$diff_date = $this->object->start_date->diffInDays($this->object->due_date); //dates in pyro are carbon objects like laravel
return $diff_date;
}
public function dueDate()
{
if($this->diff()>0)
{
return '<strong>'.$this->object->due_date.'</strong>';
}
return $this->object->due_date;
}
In your table builder should put something like this:
protected $columns = [
'start_date',
'entry.dueDate',
'entry.diff'
];
My fields and assigments are like yours:
protected $fields = [
'start_date' => [
'type' => 'anomaly.field_type.datetime',
'config' => [
"mode" => "date",
]
],
'due_date' => [
'type' => 'anomaly.field_type.datetime',
'config' => [
"mode" => "date",
]
],
];
protected $assignments = [
'start_date',
'due_date',
];
I have tried the code and it works perfectly.
I hope I've helped.
To answer your question correctly, I need to see the fields migration of your addon, and also the stream, which TB you want to display, migration. In general you could use the
wrapperand thevalueprops in a column definition. Also, you could use a Presenter class to display any data not storing in DB.