Tablebuilder customization

renaldas - 1 week ago

Hello. 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?

piterden - 1 week ago

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 wrapper and the value props in a column definition. Also, you could use a Presenter class to display any data not storing in DB.

renaldas - 1 week ago

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