Skip to content

Commit

Permalink
Select2 category paginate search: complete && scroll: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Slymee committed Jan 26, 2024
1 parent fd7b93e commit 454c313
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 46 deletions.
32 changes: 22 additions & 10 deletions app/Http/Controllers/ProductAdController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
use App\Models\Tag;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Input;

class ProductAdController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
public function index(): \Illuminate\Contracts\View\View|\Illuminate\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\Foundation\Application
{
$user = auth()->user();
$products = ProductAd::where('user_id', $user->id)->paginate(10);
Expand All @@ -25,24 +26,30 @@ public function index()
/**
* Show the form for creating a new resource.
*/
public function create(Category $category)
public function create(Category $category): \Illuminate\Contracts\View\View|\Illuminate\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\Foundation\Application
{
$mainParent = $category->whereNull('parent_id')->paginate(10);
return view('userend.create-product', compact('mainParent'));
}

/**
* Getting paginated parent category
* @param Category $category
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function getPaginatedCategory(Category $category){
$mainParent = $category->whereNull('parent_id')->get();
public function getPaginatedCategory(Category $category, Request $request): \Illuminate\Http\JsonResponse
{
// dd($request->all());
$term = $request->term;
$mainParent = $category->where('category_name','like','%'.$term.'%')->whereNull('parent_id')->paginate(2);
return response()->json(['items' => $mainParent->items()]);
}

/**
* Store a newly created resource in storage.
*/
public function store(CreateProductAdRequest $request)
public function store(CreateProductAdRequest $request): \Illuminate\Http\RedirectResponse
{
try{
$request['slug'] = Str::slug($request->input('product_title'));
Expand All @@ -60,7 +67,7 @@ public function store(CreateProductAdRequest $request)
$productAd->categories()->attach($request->input('parent_category'));
$productAd->categories()->attach($request->input('sub_category'));
$productAd->categories()->attach($request->input('sub_sub_category'));
$tagNames = $request->input('product_tags', []); // Assuming 'product_tags' is an array in the request
$tagNames = $request->input('product_tags', []);
$productAd->tags()->saveMany(
array_map(function ($tagName) {
return new Tag(['tag_name' => $tagName]);
Expand All @@ -77,11 +84,16 @@ public function store(CreateProductAdRequest $request)
}

/**
* Select child category for selecting multilevel category
* Getting paginated child category
*
* @param string $parentId
* @return \Illuminate\Http\JsonResponse
*/
public function displayChildCategory(string $parentId){
$data = Category::where('parent_id', $parentId)->paginate(10);
return response()->json($data);
public function displayChildCategory(string $parentId, Request $request): \Illuminate\Http\JsonResponse
{
$term = $request->term;
$data = Category::where('category_name', 'like', '%'.$term.'%')->where('parent_id', $parentId)->paginate(2);
return response()->json(['items' => $data->items()]);
// return response()->json(['items' => $data->items()]);
}

Expand Down
10 changes: 9 additions & 1 deletion resources/css/create-product.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ input::-webkit-inner-spin-button {

.error-message{
color: red;
}
}

#scrollingSelectContainer {
max-height: 300px; /* Adjust the maximum height as needed */
overflow-y: auto; /* Enable vertical scrolling if the content exceeds the container height */
border: 1px solid #ccc; /* Add a border for visual separation */
border-radius: 4px; /* Add border-radius for rounded corners */
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); /* Add a subtle box shadow for a lifted effect */
}
158 changes: 127 additions & 31 deletions resources/js/product-ad.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,127 @@
$(document).ready(function(){

$('#subCategoryDiv').hide();
$('#subSubCategoryDiv').hide();

/**
* Level 2 category fetch
*/
async function fetchSubCategory() {
$('#subCategoryDiv').show();
$('#subCategory').html('<option selected>Select Sub Category</option>');

var parentId = $('#parentCategory').val();
const url = 'get-child-option/' + parentId;

try {
let response = await axios.get(url);

if (response.status === 200) {
let categories = response.data.items;
categories.forEach(category => {
$('#subCategory').append(`<option value="${category.id}">${category.category_name}</option>`);
});
} else {
console.error('Error fetching data:', response.statusText);
}
} catch (error) {
console.error('Error fetching data:', error.message);
}
}
});
$(document).ready(function() {
// Initialize Select2
$("#parentCategory").select2({
placeholder: 'Search...',
width: '100%',
allowClear: true,
ajax: {
url: '/get-parent-category/',
dataType: 'json',
data: function(params) {
return {
term: params.term || '',
page: params.page || 1
}
},
processResults: function(data, params) {

params.page = params.page || 1;
return {
results: $.map(data.items, function(val, i) {
return {
id: val.id,
text: val.category_name
}
}),
pagination: {
more: (params.page * 5) < data.total_count
}
};
},
cache: true
},
// dropdownParent: $('#scrollingSelectContainer'), // Specify the container for the dropdown
});
// Infinite Scroll functionality
// $('#parentCategory').on('select2:scroll', function() {
// var lastPage = Math.ceil($('#parentCategory').select2('data').length / 5);
// $('#parentCategory').select2('trigger', 'query', {
// page: lastPage + 1
// });
// });



/**
* Getting first child category in select
*/
$('#parentCategory').on('change', function (){
var mainParentId = document.querySelector('#parentCategory').value;
$("#subCategory").select2({
placeholder: 'Search...',
width: '100%',
allowClear: true,
ajax: {
url: '/get-child-option/' + mainParentId,
dataType: 'json',
data: function(params) {
return {
term: params.term || '',
page: params.page || 1
}
},
processResults: function(data, params) {

params.page = params.page || 1;
return {
results: $.map(data.items, function(val, i) {
return {
id: val.id,
text: val.category_name
}
}),
pagination: {
more: (params.page * 5) < data.total_count
}
};
},
cache: true
},
// dropdownParent: $('#scrollingSelectContainer'), // Specify the container for the dropdown
});

});


/**
* Getting first child category
*/
$('#subCategory').on('change', function (){
var firstChildId = document.querySelector('#subCategory').value;
$("#subSubCategory").select2({
placeholder: 'Search...',
width: '100%',
allowClear: true,
ajax: {
url: '/get-child-option/' + firstChildId,
dataType: 'json',
data: function(params) {
return {
term: params.term || '',
page: params.page || 1
}
},
processResults: function(data, params) {

params.page = params.page || 1;
return {
results: $.map(data.items, function(val, i) {
return {
id: val.id,
text: val.category_name
}
}),
pagination: {
more: (params.page * 5) < data.total_count
}
};
},
cache: true
},
// dropdownParent: $('#scrollingSelectContainer'), // Specify the container for the dropdown
});

});


});
8 changes: 4 additions & 4 deletions resources/views/userend/create-product.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
</div>
</section>

<script>
{{-- <script>
/**
* Level 2 Category
*/
Expand Down Expand Up @@ -164,7 +164,7 @@
} catch (error) {
console.error('Error fetching data:', error);
}
});v
});
});
//---------------------------------------------------------------------------------------------------
Expand All @@ -181,8 +181,8 @@
// }
// }
//----------------------------------------------------------------------------------------------------
</script>
{{-- @vite(['resources/js/app.js']) --}}
</script> --}}
@vite(['resources/js/app.js'])
<script>
$(document).ready(function() {
$('#tags').select2({
Expand Down

0 comments on commit 454c313

Please sign in to comment.