доработка компоненты вывода клиентов: исправлена пагинация

This commit is contained in:
developer 2026-04-23 16:56:09 +08:00
parent 0b33aaaf9f
commit 42bc369227
2 changed files with 58 additions and 46 deletions

View File

@ -25,6 +25,8 @@ class ClientsTable extends Component
public $count;
public $mode;//short || full
private $filter = [];
public $clientsCount;
public $currentPage;
public function mount($status = null, $count = 10, $mode = 'full')
{
@ -34,8 +36,13 @@ public function mount($status = null, $count = 10, $mode = 'full')
$this->filter = [];
}
public function goToPage($page) {
$this->currentPage = $page;
}
#[On('clientsTableFilterUpdated')]
public function appendFilter($filter) {
public function appendFilter($filter)
{
if ($filter) {
$this->filter = $filter;
} else {
@ -52,23 +59,18 @@ public function getDeals()
if ($cityManager = CityManager::where('user_id', auth()->id())->first()) {
$companies = $cityManager->city->companies->pluck('id');
$deals = Deal::whereIn('agent_id', function ($query) use ($companies)
{
$deals = Deal::whereIn('agent_id', function ($query) use ($companies) {
$query->select('id');
$query->from('agents');
$query->whereIn('company_id', $companies);
});
}
else if ($company = AdminCompanyOfUser()) {
$deals = Deal::whereIn('agent_id', function ($query) use ($company)
{
} else if ($company = AdminCompanyOfUser()) {
$deals = Deal::whereIn('agent_id', function ($query) use ($company) {
$query->select('id');
$query->from('agents');
$query->where('company_id', $company->id);
});
}
elseif ($agent = Agent::where('user_id', $user->id)->first())
{
} elseif ($agent = Agent::where('user_id', $user->id)->first()) {
$deals = Deal::where('agent_id', $agent->id);
}
if (array_key_exists('status', $this->filter)) {
@ -84,15 +86,13 @@ function getClients()
{
$deals = $this->getDeals();
$clients = Client::join('deal_clients', 'users.id', '=', 'deal_clients.client_id')
->whereIn('users.id', function ($query) use ($deals)
{
->whereIn('users.id', function ($query) use ($deals) {
$query->select('client_id')
->from('deal_clients')
->whereIn('deal_id', $deals->get()->pluck('id'));
})
->orderBy('deal_clients.id', 'desc')
->with('deals');
;
$clients = $clients->select('users.*');
$clients = $clients->orderBy('name');
@ -100,33 +100,31 @@ function getClients()
$clients->whereFullText(['name', 'phone', 'email'], $this->filter['search']);
}
if (array_key_exists('status', $this->filter) && $this->status == DealStatus::UNIQUE)
{
$clients = $clients->whereHas('deals', function ($query)
{
if (array_key_exists('status', $this->filter) && $this->status == DealStatus::UNIQUE) {
$clients = $clients->whereHas('deals', function ($query) {
$query->where('status', DealStatus::UNIQUE);
})->paginate($this->count, ['*'], 'clients');
}
elseif (array_key_exists('status', $this->filter) && $this->status == DealStatus::NOT_UNIQUE)
{
$clients = $clients->whereHas('deals', function ($query)
{
});
} elseif (array_key_exists('status', $this->filter) && $this->status == DealStatus::NOT_UNIQUE) {
$clients = $clients->whereHas('deals', function ($query) {
$query->whereIn('status', [DealStatus::MODERATION, DealStatus::NEW , DealStatus::NOT_UNIQUE]);
})->paginate($this->count, ['*'], 'clients');
}
else
{
$clients = $clients->paginate($this->count, ['*'], 'clients');
});
}
//$clients = $clients->skip($this->currentPage)->take(10)->get();
return $clients;
}
public function render()
{
$clients = $this->getClients();
if ($this->clientsCount != $clients->count()) {
$this->clientsCount = $clients->count();
$this->currentPage = 1;
}
$clients = $clients->skip(($this->currentPage - 1) * $this->count)->take($this->count)->get();
return view(
'livewire.clients-table',
[
'clients' => $this->getClients(),
'clients' => $clients,
'statuses' => DealStatus::class
]
);

View File

@ -1,5 +1,7 @@
<div class="position-relative">
<div wire:loading.class.remove="d-none" class="d-none d-flex position-absolute w-100 h-100 top-0 start-0 align-items-center justify-content-center rounded-4" style="background-color:#ffffffb5">
<div wire:loading.class.remove="d-none"
class="d-none d-flex position-absolute w-100 h-100 top-0 start-0 align-items-center justify-content-center rounded-4"
style="background-color:#ffffffb5">
<div class="spinner-border text-secondary" style="width: 3rem; height: 3rem;" role="status">
<span class="visually-hidden">Loading...</span>
</div>
@ -145,9 +147,21 @@ class="bi bi-arrow-right ms-auto" viewBox="0 0 16 16">
</div>
</div>
@if($mode == 'full')
<div class="mt-3">
{{ $clients->links('vendor.pagination.bootstrap-5', ['scrollTo' => false]) }}
@if($clientsCount > $count)
<div class="d-flex justify-content-end mt-2">
<ul class="pagination">
<li class="d-none page-item {{ ($currentPage == 1) ? 'disabled' : ''}}" aria-label="« Назад">
<span class="page-link" aria-hidden="true"></span>
</li>
@for($pageNum = 1; $pageNum <= $clientsCount / $count; $pageNum++)
<li wire:click="goToPage({{ $pageNum }})" class="page-item {{ ($pageNum == $currentPage) ? 'active' : '' }}"
aria-current="page" style="cursor:pointer"><span class="page-link">{{ $pageNum }}</span></li>
@endfor
<li class="d-none page-item">
<a class="page-link" rel="next" aria-label="Вперед »"></a>
</li>
</ul>
</div>
@endif
@endif
</div>