Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion to add tdPresenter() #29

Open
snapey opened this issue Jun 30, 2020 · 0 comments
Open

Suggestion to add tdPresenter() #29

snapey opened this issue Jun 30, 2020 · 0 comments

Comments

@snapey
Copy link

snapey commented Jun 30, 2020

I was getting frustrated by wanting columns formatted in a particular way, adding an accessor to the model, adding the accessor to the $appends of the model, then putting the accessor name in the Column:make()

Then after doing all this, invariably, the search and sort functions would be broken because livewire tables tries to use the accessor in the model search and sort by, resulting in unknown column (name of accessor)

The below seems to be a simple way to deal with this, and in keeping with the package;

Having published the views; In the laravel-livewire-tables/table.blade.php;

change

    {{$value}}

to

     {{ $this->tdPresenter($column->attribute, $value) }}

each cell will now call the tdPresenter function in the component, passing the column name and the value

In the component, add a method, like;

    public function tdPresenter($attribute, $value)
    {
        if ($attribute == 'budget') return '£' .  round($value, 2);
        if ($attribute == 'created_at') return Carbon::parse($value)->format('H:i D d.m.y');
        if ($attribute == 'updated_at') return Carbon::parse($value)->format('H:i D d.m.y');

        return $value;
    }

Now I can format dates and currencies in the component, I don't have to pollute my models, and sorting and searching still works on these columns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant