-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Select2 category paginate search: complete && scroll: wip
- Loading branch information
Showing
4 changed files
with
162 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
|
||
}); | ||
|
||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters