|
7 | 7 | <x-app-layout>
|
8 | 8 | <x-category-list :category-list="$categoryList" class="-ml-5 -mt-5 -mr-5 px-4"/>
|
9 | 9 |
|
10 |
| - <div class="p-3 pb-0"> |
11 |
| - <form action="" method="GET"> |
12 |
| - <x-input type="text" name="search" placeholder="Search for the products" value="{{request()->get('search')}}" /> |
| 10 | + <div class="flex gap-2 items-center p-3 pb-0" x-data="{ |
| 11 | + selectedSort: '{{ request()->get('sort', '-updated_at') }}', |
| 12 | + searchKeyword: '{{ request()->get('search') }}', |
| 13 | + updateUrl() { |
| 14 | + const params = new URLSearchParams(window.location.search) |
| 15 | + if (this.selectedSort && this.selectedSort !== '-updated_at') { |
| 16 | + params.set('sort', this.selectedSort) |
| 17 | + } else { |
| 18 | + params.delete('sort') |
| 19 | + } |
| 20 | +
|
| 21 | + if (this.searchKeyword) { |
| 22 | + params.set('search', this.searchKeyword) |
| 23 | + } else { |
| 24 | + params.delete('search') |
| 25 | + } |
| 26 | + window.location.href = window.location.origin + window.location.pathname + '?' |
| 27 | + + params.toString(); |
| 28 | + } |
| 29 | + }"> |
| 30 | + <form action="" method="GET" class="flex-1" @submit.prevent="updateUrl"> |
| 31 | + <x-input type="text" name="search" placeholder="Search for the products" |
| 32 | + x-model="searchKeyword"/> |
13 | 33 | </form>
|
| 34 | + <x-input |
| 35 | + x-model="selectedSort" |
| 36 | + @change="updateUrl" |
| 37 | + type="select" |
| 38 | + name="sort" |
| 39 | + class="w-full focus:border-purple-600 focus:ring-purple-600 border-gray-300 rounded"> |
| 40 | + <option value="price">Price (ASC)</option> |
| 41 | + <option value="-price">Price (DESC)</option> |
| 42 | + <option value="title">Title (ASC)</option> |
| 43 | + <option value="-title">Title (DESC)</option> |
| 44 | + <option value="-updated_at">Last Modified at the top</option> |
| 45 | + <option value="updated_at">Last Modified at the bottom</option> |
| 46 | + </x-input> |
| 47 | + |
14 | 48 | </div>
|
15 | 49 |
|
16 |
| - <?php if ($products->count() === 0): ?> |
17 |
| - <div class="text-center text-gray-600 py-16 text-xl"> |
18 |
| - There are no products published |
19 |
| - </div> |
| 50 | + <?php if ( $products->count() === 0 ): ?> |
| 51 | + <div class="text-center text-gray-600 py-16 text-xl"> |
| 52 | + There are no products published |
| 53 | + </div> |
20 | 54 | <?php else: ?>
|
21 |
| - <div |
22 |
| - class="grid gap-4 grig-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 p-3" |
23 |
| - > |
24 |
| - @foreach($products as $product) |
25 |
| - <!-- Product Item --> |
26 |
| - <div |
27 |
| - x-data="productItem({{ json_encode([ |
| 55 | + <div |
| 56 | + class="grid gap-4 grig-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 p-3" |
| 57 | + > |
| 58 | + @foreach($products as $product) |
| 59 | + <!-- Product Item --> |
| 60 | + <div |
| 61 | + x-data="productItem({{ json_encode([ |
28 | 62 | 'id' => $product->id,
|
29 | 63 | 'slug' => $product->slug,
|
30 | 64 | 'image' => $product->image ?: '/img/noimage.png',
|
@@ -59,6 +93,6 @@ class="object-cover rounded-lg hover:scale-105 hover:rotate-1 transition-transfo
|
59 | 93 | <!--/ Product Item -->
|
60 | 94 | @endforeach
|
61 | 95 | </div>
|
62 |
| - {{$products->links()}} |
| 96 | + {{$products->appends(['sort' => request('sort'), 'search' => request('search')])->links()}} |
63 | 97 | <?php endif; ?>
|
64 | 98 | </x-app-layout>
|
0 commit comments