|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Devaweb\LivewireDataTable\Livewire; |
| 4 | + |
| 5 | +use Livewire\Component; |
| 6 | +use Devaweb\LivewireDataTable\Traits\ColumnsCache; |
| 7 | + |
| 8 | +class CustomizeColumns extends Component |
| 9 | +{ |
| 10 | + |
| 11 | + use ColumnsCache; |
| 12 | + |
| 13 | + public $table; |
| 14 | + //all columns names only |
| 15 | + public $columns = []; |
| 16 | + //selected columns from cache |
| 17 | + public $seCols = []; |
| 18 | + //unselected columns |
| 19 | + public $uCols = []; |
| 20 | + //full columns with details |
| 21 | + public $detailCols = []; |
| 22 | + |
| 23 | + //event listeners |
| 24 | + protected $listeners = [ |
| 25 | + 'refresh' => '$refresh', |
| 26 | + 'savedetailseCols' => 'getColumnsDetails' |
| 27 | + ]; |
| 28 | + |
| 29 | + public function mount( |
| 30 | + string $table, |
| 31 | + array $columns, |
| 32 | + array $selectedColumns, |
| 33 | + array $detailCols |
| 34 | + ): void |
| 35 | + { |
| 36 | + $this->table = $table; |
| 37 | + $this->columns = $columns; |
| 38 | + $this->seCols = $selectedColumns; |
| 39 | + $this->detailCols = $detailCols; |
| 40 | + |
| 41 | + //initcache |
| 42 | + $this->initCache($this->table); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Add column to columns array |
| 47 | + * |
| 48 | + * @param string $col |
| 49 | + * |
| 50 | + * @return void |
| 51 | + */ |
| 52 | + public function addCol($col) |
| 53 | + { |
| 54 | + if(!in_array($col, $this->seCols)) { |
| 55 | + array_push($this->seCols, $col); |
| 56 | + } |
| 57 | + |
| 58 | + $this->saveCols(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Remove column from columns array |
| 63 | + * |
| 64 | + * @param string $col |
| 65 | + * |
| 66 | + * @return void |
| 67 | + */ |
| 68 | + public function removeCol($col) |
| 69 | + { |
| 70 | + if(in_array($col, $this->seCols)) { |
| 71 | + unset($this->seCols[array_search($col, $this->seCols)]); |
| 72 | + } |
| 73 | + |
| 74 | + $this->saveCols(); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Move the column up or down |
| 79 | + * |
| 80 | + * @param string $dir |
| 81 | + * @param string $col |
| 82 | + * |
| 83 | + * @return void |
| 84 | + */ |
| 85 | + public function moveCol($dir, $col) |
| 86 | + { |
| 87 | + //dd($dir,$col); |
| 88 | + //$this->dispatchBrowserEvent('toast', ['msg' => 'Moving..']); |
| 89 | + $index = array_search($col, $this->seCols); |
| 90 | + |
| 91 | + $c = count($this->seCols); |
| 92 | + $newIndex = ($dir == 'up') ? $index - 1 : $index + 1; |
| 93 | + |
| 94 | + //dd($index, $newIndex, count($this->seCols), $this->seCols); |
| 95 | + |
| 96 | + $co = array_splice($this->seCols, $index, 1); |
| 97 | + array_splice($this->seCols, $newIndex, 0, $co); |
| 98 | + |
| 99 | + $this->saveCols(); |
| 100 | + //dd($this->seCols); |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Save Column to Cache |
| 106 | + * |
| 107 | + * @return void |
| 108 | + */ |
| 109 | + public function saveCols() |
| 110 | + { |
| 111 | + $this->upCols($this->seCols); |
| 112 | + //$this->emitTo('data-table', 'refresh'); |
| 113 | + } |
| 114 | + |
| 115 | + public function refresh() |
| 116 | + { |
| 117 | + $this->emitTo('livewire-data-table', 'refresh'); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * get Columns details from child component - ColumnsDetails |
| 122 | + * |
| 123 | + * @param array $detailCols |
| 124 | + * @return void |
| 125 | + */ |
| 126 | + public function getColumnsDetails(array $detailCols): void |
| 127 | + { |
| 128 | + $this->detailCols = $detailCols; |
| 129 | + } |
| 130 | + |
| 131 | + public function render() |
| 132 | + { |
| 133 | + return view('livewire-data-table::livewire.customize-columns'); |
| 134 | + } |
| 135 | +} |
0 commit comments