Power-Components / livewire-powergrid

⚡ PowerGrid generates modern, powerful and easy-to-customize data tables using Laravel Livewire.
https://livewire-powergrid.com
MIT License
1.41k stars 206 forks source link

Actions Button is Not redirect when clicked #1600

Closed hansajith1998 closed 1 week ago

hansajith1998 commented 1 week ago
public function actions(User $row): array
    {
        return [
            Button::add('edit')
                ->slot("<i data-tw-merge data-lucide='pencil' class='stroke-[2] w-3.5 h-3.5 mx-auto block text-white'></i>")
                ->id('edit')
                ->class('transition duration-200 border shadow-sm inline-flex items-center justify-center rounded-md font-medium cursor-pointer focus:ring-none focus-visible:outline-none [&:hover:not(:disabled)]:bg-opacity-90 [&:hover:not(:disabled)]:border-opacity-90 [&:not(button)]:text-center disabled:opacity-70 disabled:cursor-not-allowed bg-[#ffc107] border-opacity-5 border-[#ffca2c] text-slate-900 dark:border-warning w-8 h-[1.63rem]')
                ->method('get')
                ->route('super_admin.users.edit', ['user' => $row->id])
                ->can(allowed: auth()->user()->can('users.update'))
                ->target('_self')
                ->tooltip('Edit user')
//                ->dispatch('edit', ['rowId' => $row->id])
        ];
    }

image

I have a button like this, rendering the HTML as a button (like the screenshot). The problem is it will not redirect to the edit page.. I haven't found any solution in the documentation

hemant-kr-meena commented 1 week ago

You can generate a link by removing id() tag. from table class without calling id() it generate <a href="">link</a>.

hansajith1998 commented 1 week ago

image

image

Still the same, removing the ->id() does not generate <a> tag for me.

hemant-kr-meena commented 1 week ago

Well, I debugged this problem, and this is the solution.

to generate a link we need to call the route at starting before calling any other methods like id, dispatch because this package finds button type from dynamicProperties. which store values in the same manner that we call. and it uses the first one to define type.

so try this approach

Before

Button::add('edit')
                ->slot("<i data-tw-merge data-lucide='pencil' class='stroke-[2] w-3.5 h-3.5 mx-auto block text-white'></i>")
                ->id('edit')
                ->class('transition duration-200 border shadow-sm inline-flex items-center justify-center rounded-md font-medium cursor-pointer focus:ring-none focus-visible:outline-none [&:hover:not(:disabled)]:bg-opacity-90 [&:hover:not(:disabled)]:border-opacity-90 [&:not(button)]:text-center disabled:opacity-70 disabled:cursor-not-allowed bg-[#ffc107] border-opacity-5 border-[#ffca2c] text-slate-900 dark:border-warning w-8 h-[1.63rem]')
                ->method('get')
                ->route('super_admin.users.edit', ['user' => $row->id])
                ->can(allowed: auth()->user()->can('users.update'))
                ->target('_self')
                ->tooltip('Edit user')
//                ->dispatch('edit', ['rowId' => $row->id])

After

Button::add('edit')
                ->slot("<i data-tw-merge data-lucide='pencil' class='stroke-[2] w-3.5 h-3.5 mx-auto block text-white'></i>")
                ->route('welcome', ['user' => $row->id])
                ->id('edit')
                ->class('transition duration-200 border shadow-sm inline-flex items-center justify-center rounded-md font-medium cursor-pointer focus:ring-none focus-visible:outline-none [&:hover:not(:disabled)]:bg-opacity-90 [&:hover:not(:disabled)]:border-opacity-90 [&:not(button)]:text-center disabled:opacity-70 disabled:cursor-not-allowed bg-[#ffc107] border-opacity-5 border-[#ffca2c] text-slate-900 dark:border-warning w-8 h-[1.63rem]')
                ->target('_self')
                ->tooltip('Edit user')
//                ->dispatch('edit', ['rowId' => $row->id])

Make sure you remove the method function because it generates html form, not a link. So if you want to generate a form then define a method if not then remove it. You can also use any method after calling the route at starting.

Result

Screenshot (116)
hansajith1998 commented 1 week ago

Hi @hemant-kr-meena

It worked. Thank you.

Best Regards