rappasoft / laravel-livewire-tables

A dynamic table component for Laravel Livewire
https://rappasoft.com/docs/laravel-livewire-tables/v2/introduction
MIT License
1.8k stars 342 forks source link

[Feature Request]: Nullable table row url #2110

Open leesherwood opened 16 hours ago

leesherwood commented 16 hours ago

Overview

Allow setting whether a table row has a clickable URL on a per-row basis

Detailed explanation

I have a table that lists a Document model which has a status of pending or accepted. Only accepted documents have a public URL you can click through too.

So I added this:

->setTableRowUrl(fn($row) => $row->status === 'accepted' ? route("account.documents.show", $row) : null)

This works, albeit with a some unneccessary markup in the HTML

<td .... onclick="window.open('', '_self')" .... >

but I also want to change the cursor, I don't want it to change to a pointer for null URLs. Ideally i'd have a not allowed cursor. I tried this:

->setTrAttributes(fn($row) => ['class' => $row->status !== 'accepted'? 'cursor-not-allowed' : ''])

but this outputs the following class list:

cursor-pointer .... rappasoft-striped-row cursor-not-allowed

Because:

https://github.com/rappasoft/laravel-livewire-tables/blob/a28d2d8d75d81d478bdf01e5d406c948250310c6/resources/views/components/table/tr.blade.php#L27

https://github.com/rappasoft/laravel-livewire-tables/blob/a28d2d8d75d81d478bdf01e5d406c948250310c6/src/Traits/Helpers/TableAttributeHelpers.php#L108-L116

Notes

I propose that:

public function hasTableRowUrl(): bool
    {
        return isset($this->trUrlCallback);
    }

Is changed too:

public function hasTableRowUrl(int|Model $row): bool
    {
        return $this->getTableRowUrl($row) !== null;
    }
leesherwood commented 15 hours ago

Should this change (or similar) not make it into the library, and someone stumble's across this, you can do the following to make it work.

->setTrAttributes(fn($row, $rowIndex) => [
    'default' => false,
    'class' => implode(' ', [
        'dark:text-white', 
        ($rowIndex % 2 === 0 ? 'bg-white dark:bg-gray-700' : 'bg-gray-50 dark:bg-gray-800'), 
        ($row->status !== 'approved' ? 'cursor-not-allowed italic text-gray-400': 'cursor-pointer')])
     ]
)

Just means pulling the default style classes into your own code and disabling them from auto-applying.

Not as elegant but it works.

lrljoe commented 12 hours ago

Leave this one with me, what you're saying makes sense, but I need to validate any other impact.

Given the propensity for people to publish the package views, it may be that a new method needs to be added to support this, while maintaining the current behaviour on the existing method.