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 340 forks source link

how to create the serial number colum #72

Closed hemant17 closed 4 years ago

hemant17 commented 4 years ago

able data is for multiple users and multiple users are interesting the data in the database but when you show it to the user to look like 5,56,64 more like that which is not good so I want to show them like the 1,2,3

ConnorHowell commented 4 years ago

Looking at the source it wouldn't currently be possible to show the index of the current item as a column in the table.

A PR could be made for this however I'm not sure how useful it would be as the whole point of this package is to filter, search & paginate data and the index would change when these functions are performed. Pagination could be overcome by using the pagination 'from' field and adding the current item index. But as soon as you sort/search the data the items would not retain their serial number.

@rappasoft Not sure what the best way to implement this would be; maybe this could be added as a property on the table component?

rappasoft commented 4 years ago

These requests literally make no sense to me. There is no use for this feature.

hariadi commented 4 years ago

I think you can do like this. Feel free to modify:

public function mount()
{
    $this->index = $this->page > 1 ? ($this->page - 1) * $this->perPage : 0;
}

public function columns(): array
{
    return [
        Column::make(__('No.'))->format(fn () => ++$this->index),
        //...
    ];
}
hemant17 commented 4 years ago

I think you can do like this. Feel free to modify:

public function mount()
{
    $this->index = $this->page > 1 ? ($this->page - 1) * $this->perPage : 0;
}

public function columns(): array
{
    return [
        Column::make(__('No.'))->format(fn () => ++$this->index),
        //...
    ];
}

image it's showing but when I delete the row with the action button it shows me this error

hariadi commented 3 years ago

I think you can do like this. Feel free to modify:

public function mount()
{
    $this->index = $this->page > 1 ? ($this->page - 1) * $this->perPage : 0;
}

public function columns(): array
{
    return [
        Column::make(__('No.'))->format(fn () => ++$this->index),
        //...
    ];
}

image it's showing but when I delete the row with the action button it shows me this error

Since pagination changes, just declare your index/serial and move implementation to columns directly:


protected $index = 0;

public function columns(): array
{
    $this->index = $this->page > 1 ? ($this->page - 1) * $this->perPage : 0;

    return [
        Column::make(__('No.'))->format(fn () => ++$this->index),
        //...
    ];
}
hariadi commented 2 years ago

The easy way, just add to your view (e.g. row.blade.php):

<x-livewire-tables::bs5.table.cell>
    {{ ($this->page > 1 ? ($this->page - 1) * $this->perPage : 0) + $loop->iteration }}
</x-livewire-tables::bs5.table.cell>
PamungkasRizall commented 11 months ago

I think you can do like this. Feel free to modify:

public function mount()
{
    $this->index = $this->page > 1 ? ($this->page - 1) * $this->perPage : 0;
}

public function columns(): array
{
    return [
        Column::make(__('No.'))->format(fn () => ++$this->index),
        //...
    ];
}

image it's showing but when I delete the row with the action button it shows me this error

Since pagination changes, just declare your index/serial and move implementation to columns directly:

protected $index = 0;

public function columns(): array
{
    $this->index = $this->page > 1 ? ($this->page - 1) * $this->perPage : 0;

    return [
        Column::make(__('No.'))->format(fn () => ++$this->index),
        //...
    ];
}

why in version 3 $this->page not found?

Hans-Star18 commented 1 month ago

in version 3 user $this->getPage()

Hans-Star18 commented 1 month ago

i make traits for numbering

namespace App\Traits\Livewire;

use Rappasoft\LaravelLivewireTables\Views\Column;

trait WithTableNumbering
{
    protected int $index = 0;

    public function numberingColumn(): Column
    {
        return Column::make(__('No.'))
            ->label(function () {
                $this->initializeIndex();
                return ++$this->index;
            });
    }

    protected function initializeIndex(): void
    {
        if ($this->index === 0) {
            $this->index = $this->getPage() > 1 ? ($this->getPage() - 1) * $this->perPage : 0;
        }
    }
}

and in livewire datatables

use App\Traits\Livewire\WithTableNumbering;

class UsersTable extends DataTableComponent
{
    use WithTableNumbering;

    protected $model = User::class;

    public function columns(): array
    {
        return [
            $this->numberingColumn()
                ->sortable(),
            .....
        ];
    }
}