diff --git a/app/Http/Controllers/ProductAdController.php b/app/Http/Controllers/ProductAdController.php index aa1fc0e..bf5dc15 100644 --- a/app/Http/Controllers/ProductAdController.php +++ b/app/Http/Controllers/ProductAdController.php @@ -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); @@ -25,7 +26,7 @@ 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')); @@ -33,16 +34,22 @@ public function create(Category $category) /** * 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')); @@ -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]); @@ -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()]); } diff --git a/resources/css/create-product.css b/resources/css/create-product.css index 3f53063..b8af10e 100644 --- a/resources/css/create-product.css +++ b/resources/css/create-product.css @@ -33,4 +33,12 @@ input::-webkit-inner-spin-button { .error-message{ color: red; -} \ No newline at end of file +} + +#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 */ +} diff --git a/resources/js/product-ad.js b/resources/js/product-ad.js index c28663c..56be993 100644 --- a/resources/js/product-ad.js +++ b/resources/js/product-ad.js @@ -1,31 +1,127 @@ -$(document).ready(function(){ - -$('#subCategoryDiv').hide(); -$('#subSubCategoryDiv').hide(); - -/** -* Level 2 category fetch -*/ -async function fetchSubCategory() { - $('#subCategoryDiv').show(); - $('#subCategory').html(''); - - 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(``); - }); - } else { - console.error('Error fetching data:', response.statusText); - } - } catch (error) { - console.error('Error fetching data:', error.message); - } -} -}); \ No newline at end of file +$(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 + }); + + }); + + +}); diff --git a/resources/views/userend/create-product.blade.php b/resources/views/userend/create-product.blade.php index fb742bf..d733ec7 100644 --- a/resources/views/userend/create-product.blade.php +++ b/resources/views/userend/create-product.blade.php @@ -81,7 +81,7 @@ - - {{-- @vite(['resources/js/app.js']) --}} + --}} + @vite(['resources/js/app.js'])