I have a problem when I click the active or nonactive filter button, the table results do not change at all. I want when I click the active button the results of the table will show only records that have active status. there are no errors but when I click the active button nothing happens and neither do the other buttons.
could you check my code maybe there is a shortage or error so that the results of the table do not appear according to the filter button. Thank you so much for your help.
This is my file App/http/Livewire/Show.php
<?php
namespace App\Http\Livewire\Retails;
use App\Models\Retail;
use Livewire\Component;
use Livewire\WithPagination;
class Show extends Component
{
// Datatable
use WithPagination;
public $sortColumnName = 'created_at', $sortDirection = 'desc', $searchTerm = null, $showData = 5, $selectedRows = [], $selectedPageRows = false, $s = null;
protected $paginationTheme = 'bootstrap';
protected $queryString = ['searchTerm' => ['except' => '']];
public function render()
{
$allStatus = Retail::all()->count();
$aktif = Retail::where('status', 1)->count();
$nonAktif = Retail::where('status', 2)->count();
$retails = $this->dataList();
return view('livewire.retails.show', compact('retails','allStatus','aktif','nonAktif'));
}
public function filterStatus($s = null){
$this->s = $s;
}
public function dataList(){
return Retail::when($this->s, function($query, $s){
return $query->where('status', $s);
})
->orWhere('id_retail', 'LIKE', '%'.$this->searchTerm.'%')
->orWhere('name', 'LIKE', '%'.$this->searchTerm.'%')
->orderBy($this->sortColumnName, $this->sortDirection)
->paginate($this->showData);
}
This is my file resource/views/livewire/show.blade.php
{{-- DATA TABLE --}}
<div class="row mb-2">
<div class="col-md-4">
<div class="btn-group">
<x-select-input wire:model='showData'/>
</div>
@if($selectedRows)
<div class="btn-group ml-2">
<button type="button" class="btn btn-default">Aksi Bulk</button>
<button type="button" class="btn btn-default dropdown-toggle dropdown-icon" data-toggle="dropdown" aria-expanded="false">
<span class="sr-only">Toggle Dropdown</span>
</button>
<div class="dropdown-menu" role="menu" style="">
<a wire:click.prevent='deleteSelectedRows' class="dropdown-item" href="#">Hapus yang ditandai</a>
<a class="dropdown-item" href="#">Export Excel</a>
</div>
</div>
<span class="ml-1 badge badge-pill badge-secondary">Selected {{ count($selectedRows) }} {{ Str::plural('Retail', count($selectedRows)) }}</span>
@endif
</div>
<div class="col-md-8">
<div class="float-right">
<div class="btn-group mr-2">
<button wire:click='filterStatus' type="button" class="btn btn-default">
<span class="mr-1">Semua</span>
<span class="badge badge-pill badge-info">{{ $allStatus }}</span>
</button>
<button wire:click='filterStatus(1)' type="button" class="btn btn-default">
<span class="mr-1">Aktif</span>
<span class="badge badge-pill badge-success">{{ $aktif }}</span>
</button>
<button wire:click='filterStatus(2)' type="button" class="btn btn-default">
<span class="mr-1">Tidak Aktif</span>
<span class="badge badge-pill badge-secondary">{{ $nonAktif }}</span>
</button>
</div>
<div class="btn-group">
<x-search-input wire:model='searchTerm'/>
</div>
</div>
</div>
</div>
</div>
{{-- @include('livewire.supplier-modal') --}}
<div class="table-responsive">
<x-loading wire:loading.delay.longest/>
<table class="table table-striped table-sm table-hover rounded-6">
<thead style="background-color: #5f815e; color:#fff;">
<tr>
<th class=" align-middle text-center">
<input class="form-check" type="checkbox" value="" wire:model='selectedPageRows'>
</th>
<th>#</th>
<th>
Kode
<span wire:click="sortBy('id_retail')" class="float-right text-sm" style="cursor: pointer;">
<i class="fa fa-arrow-up {{ $sortColumnName === 'id_retail' && $sortDirection === 'asc' ? '' : 'text-dark' }}"></i>
<i class="fa fa-arrow-down {{ $sortColumnName === 'id_retail' && $sortDirection === 'desc' ? '' : 'text-dark' }}"></i>
</span>
</th>
<th>
Nama
<span wire:click="sortBy('name')" class="float-right text-sm" style="cursor: pointer;">
<i class="fa fa-arrow-up {{ $sortColumnName === 'name' && $sortDirection === 'asc' ? '' : 'text-dark' }}"></i>
<i class="fa fa-arrow-down {{ $sortColumnName === 'name' && $sortDirection === 'desc' ? '' : 'text-dark' }}"></i>
</span>
</th>
<th>Alamat</th>
<th>Tlp</th>
<th>Email</th>
<th>Status</th>
<th>Aksi</th>
</tr>
</thead>
<tbody wire:loading.class='text-muted'>
@forelse($retails as $index => $item)
<tr>
<td class="text-center align-middle">
<input wire:model='selectedRows' class="form-check" type="checkbox" value="{{ $item->id }}" id="{{ $item->id }}">
</td>
<td class="align-middle">{{ $retails->firstItem() + $index}}.</td>
<td class=" align-middle">{{ $item->id_retail }}</td>
<td class=" align-middle">{{ $item->name }}</td>
<td class=" align-middle">{{ $item->address }}</td>
<td class=" align-middle">{{ $item->tlp }}</td>
<td class=" align-middle">{{ $item->email }}</td>
<td class=" align-middle">
<div class="custom-control custom-switch">
<input wire:change='switchStatus({{$item->id}}, {{ $item->status }})' type="checkbox" class="custom-control-input" @checked($item->status == 1) id="{{ $item->id_retail }}">
<label class="custom-control-label text-sm @if($item->status == 1) text-success @else text-secondary @endif" for="{{ $item->id_retail }}">{!! $item->status == 1 ? "Aktif <i class='fa fa-circle' ></i>" : "Tidak Aktif <i class='fa fa-circle ' ></i>" !!}</label>
</div>
</td>
<td class=" align-middle">
<button data-toggle="modal" data-target="#updateretailmodal" wire:click='editRetail({{ $item->id }})' type="submit" class="btn btn-warning"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></button>
@if(!$selectedRows)
|
<button type="submit" class="btn btn-danger" wire:click="deleteConfirmation({{ $item->id }})"><i class="fa fa-trash-o" aria-hidden="true"></i></button>
@endif
</td>
</tr>
@empty
<tr class="text-center">
<td colspan="9">
<img src="https://www.hyperyno.com/front/img/no-result-found.png" alt="No results found" width="170">
{{-- <p class="mt-2">Tidak ada data ditemukan</p> --}}
</td>
</tr>
@endforelse
</tbody>
</table>
</div>
<div>
{{ $retails->links() }}
</div>
</div>