MedicOneSystems / livewire-datatables

Advanced datatables using Laravel, Livewire, Tailwind CSS and Alpine JS
https://livewire-datatables.com/
MIT License
1.19k stars 258 forks source link

Remove columns from the view #554

Open alukardua1 opened 1 year ago

alukardua1 commented 1 year ago

Is it possible to add a function to remove a column from the output in the table. I have user groups and I would like to customize the output of certain columns for different user groups?

insulae commented 4 months ago

I just needed the same thing, it's late but I'll leave it in case it helps someone

"normal" column function:

 public function columns()
    {
        return [
            Column::name('id')
            ->label('id'),

            Column::name('name')
            ->label('name'),

            Column::name('age')
            ->label('age'),
        ];
    }

returning age if user is admin:

public function builder()
    {
        $this->admin = TRUE;
    }

    public function columns()
    {
        $cols = [
            Column::name('id')
            ->label('id'),

            Column::name('name')
            ->label('name'),
        ];

        if($this->admin == TRUE){
            $cols[] = 
            Column::name('age')
            ->label('age');
        }

        return $cols;
    }

The setting of $this->admin should be done according to the logic you want to use.