Feature #6 : Update

- Add Get Village by District Code
- Add Get Postal Code by Village Code
- Update Filter on Villages, District, and City, add dropdown to select province, city, district before filter for faster filter
This commit is contained in:
Daeng Deni Mardaeni 2024-08-13 10:42:03 +07:00
parent b75db02f27
commit 4987002a9d
8 changed files with 469 additions and 293 deletions

View File

@ -158,4 +158,12 @@
]);
}
public function getVillagesByDistrictId($id){
return response()->json(Village::where('district_code', $id)->get());
}
public function getPostalCodesByVillageId($id){
return response()->json(Village::where('code', $id)->first());
}
}

View File

@ -9,6 +9,8 @@ let settings = {
const provinceSelect = document.querySelector('#province_code');
const citySelect = document.querySelector('#city_code');
const districtSelect = document.querySelector('#district_code');
const villageSelect = document.querySelector('#village_code');
const postalCode = document.querySelector('#postal_code');
if (provinceSelect && !citySelect) {
new TomSelect('#province_code', settings);
@ -46,9 +48,9 @@ if (citySelect && !districtSelect) {
});
}
if (districtSelect) {
if (districtSelect && !villageSelect) {
let tomdistrict = new TomSelect('#district_code', settings);
let tomcities = new TomSelect('#city_code',{
let tomcities = new TomSelect('#city_code', {
...settings,
onChange: function (value) {
console.log('City selected:', value);
@ -131,3 +133,175 @@ if (districtSelect) {
}
});
}
if (villageSelect) {
let tomvillage = new TomSelect('#village_code', settings);
let tomdistrict = new TomSelect('#district_code', {
...settings,
onChange: function (value) {
// Destroy only if necessary (prevents unnecessary re-initialization)
if (tomvillage) {
tomvillage.destroy();
}
let url = 'locations/villages/district/' + value;
fetch(url)
.then(response => response.json())
.then(data => {
const options = data.map(item => ({
value: item.code,
text: item.name
}));
tomvillage = new TomSelect('#village_code', {
...settings, // Spread existing settings (including createOnBlur: false)
options: options
});
})
.catch(error => {
console.error('Error fetching data:', error);
});
}
});
let tomcities = new TomSelect('#city_code', {
...settings,
onChange: function (value) {
// Destroy only if necessary (prevents unnecessary re-initialization)
if (tomdistrict) {
tomdistrict.destroy();
}
let url = 'locations/districts/city/' + value;
fetch(url)
.then(response => response.json())
.then(data => {
const options = data.map(item => ({
value: item.code,
text: item.name
}));
tomdistrict = new TomSelect('#district_code', {
...settings, // Spread existing settings (including createOnBlur: false)
options: options,
onChange: function (value) {
// Destroy only if necessary (prevents unnecessary re-initialization)
if (tomvillage) {
tomvillage.destroy();
}
let url = 'locations/villages/district/' + value;
fetch(url)
.then(response => response.json())
.then(data => {
const options = data.map(item => ({
value: item.code,
text: item.name
}));
tomvillage = new TomSelect('#village_code', {
...settings, // Spread existing settings (including createOnBlur: false)
options: options
});
})
.catch(error => {
console.error('Error fetching data:', error);
});
}
});
})
.catch(error => {
console.error('Error fetching data:', error);
});
}
});
let tomprovince = new TomSelect('#province_code', {
...settings, // Spread existing settings
onChange: function (value) {
console.log('Province selected:', value);
// Destroy only if necessary (prevents unnecessary re-initialization)
if (tomcities) {
tomcities.destroy();
}
let url = 'locations/cities/province/' + value;
fetch(url)
.then(response => response.json())
.then(data => {
const options = data.map(item => ({
value: item.code,
text: item.name
}));
tomcities = new TomSelect('#city_code', {
...settings, // Spread existing settings (including createOnBlur: false)
options: options,
onChange: function (value) {
// Destroy only if necessary (prevents unnecessary re-initialization)
if (tomdistrict) {
tomdistrict.destroy();
}
let url = 'locations/districts/city/' + value;
fetch(url)
.then(response => response.json())
.then(data => {
const options = data.map(item => ({
value: item.code,
text: item.name
}));
tomdistrict = new TomSelect('#district_code', {
...settings, // Spread existing settings (including createOnBlur: false)
options: options,
onChange: function (value) {
// Destroy only if necessary (prevents unnecessary re-initialization)
if (tomvillage) {
tomvillage.destroy();
}
let url = 'locations/villages/district/' + value;
fetch(url)
.then(response => response.json())
.then(data => {
const options = data.map(item => ({
value: item.code,
text: item.name
}));
tomvillage = new TomSelect('#village_code', {
...settings, // Spread existing settings (including createOnBlur: false)
options: options
});
})
.catch(error => {
console.error('Error fetching data:', error);
});
}
});
})
.catch(error => {
console.error('Error fetching data:', error);
});
}
});
})
.catch(error => {
console.error('Error fetching data:', error);
});
}
});
if (postalCode) {
villageSelect.addEventListener('change', function (event) {
fetch('locations/villages/postal-code/' + event.target.value)
.then(response => response.json())
.then(data => {
postalCode.value = data.postal_code;
})
});
}
}

View File

@ -16,7 +16,6 @@
@endpush
@section('content')
<div class="container-fluid">
<div class="grid">
<div class="card card-grid min-w-full" data-datatable="false" data-datatable-page-size="5" data-datatable-state-save="false" id="cities-table" data-api-url="{{ route('locations.cities.datatables') }}">
<div class="card-header py-5 flex-wrap">
@ -38,9 +37,10 @@
<input placeholder="Search Cities" id="search" type="text" value="">
</label>
<button class="search btn btn-sm btn-outline btn-primary">
<i class="ki-filled ki-setting-4"> </i> <Filters></Filters>
<i class="ki-filled ki-setting-4"> </i>
<Filters></Filters>
</button>
<div class="h-[24px] border border-r-gray-200"> </div>
<div class="h-[24px] border border-r-gray-200"></div>
<a class="btn btn-sm btn-light" href="{{ route('locations.cities.export') }}"> Export to Excel </a>
<a class="btn btn-sm btn-primary" href="{{ route('locations.cities.create') }}"> Add City </a>
</div>
@ -85,7 +85,6 @@
</div>
</div>
</div>
</div>
@endsection
@push('scripts')
@ -94,7 +93,7 @@
function deleteData(data) {
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!" ,
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
@ -111,7 +110,7 @@
$.ajax(`locations/cities/${data}`, {
type: 'DELETE'
}).then((response) => {
swal.fire('Deleted!', 'User has been deleted.','success').then(() => {
swal.fire('Deleted!', 'User has been deleted.', 'success').then(() => {
window.location.reload();
});
}).catch((error) => {

View File

@ -16,7 +16,6 @@
@endpush
@section('content')
<div class="container-fluid">
<div class="grid">
<div class="card card-grid min-w-full" data-datatable="false" data-datatable-page-size="5" data-datatable-state-save="false" id="districts-table" data-api-url="{{ route('locations.districts.datatables') }}">
<div class="card-header py-5 flex-wrap">
@ -42,9 +41,10 @@
<input placeholder="Search Districts" id="search" type="text" value="">
</label>
<button class="search btn btn-sm btn-outline btn-primary">
<i class="ki-filled ki-setting-4"> </i> <Filters></Filters>
<i class="ki-filled ki-setting-4"> </i>
<Filters></Filters>
</button>
<div class="h-[24px] border border-r-gray-200"> </div>
<div class="h-[24px] border border-r-gray-200"></div>
<a class="btn btn-sm btn-light" href="{{ route('locations.districts.export') }}"> Export to Excel </a>
<a class="btn btn-sm btn-primary" href="{{ route('locations.districts.create') }}"> Add District </a>
</div>
@ -93,7 +93,6 @@
</div>
</div>
</div>
</div>
@endsection
@push('scripts')
@ -102,7 +101,7 @@
function deleteData(data) {
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!" ,
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
@ -119,7 +118,7 @@
$.ajax(`locations/districts/${data}`, {
type: 'DELETE'
}).then((response) => {
swal.fire('Deleted!', 'User has been deleted.','success').then(() => {
swal.fire('Deleted!', 'User has been deleted.', 'success').then(() => {
window.location.reload();
});
}).catch((error) => {

View File

@ -5,7 +5,6 @@
@endsection
@section('content')
<div class="container-fluid">
<div class="grid">
<div class="card card-grid min-w-full" data-datatable="false" data-datatable-page-size="5" data-datatable-state-save="false" id="provinces-table" data-api-url="{{ route('locations.provinces.datatables') }}">
<div class="card-header py-5 flex-wrap">
@ -40,9 +39,10 @@
</option>
</select>
<button class="btn btn-sm btn-outline btn-primary">
<i class="ki-filled ki-setting-4"> </i> <Filters></Filters>
<i class="ki-filled ki-setting-4"> </i>
<Filters></Filters>
</button>
<div class="h-[24px] border border-r-gray-200"> </div>
<div class="h-[24px] border border-r-gray-200"></div>
<a class="btn btn-sm btn-light" href="{{ route('locations.provinces.export') }}"> Export to Excel </a>
<a class="btn btn-sm btn-primary" href="{{ route('locations.provinces.create') }}"> Add Province </a>
</div>
@ -83,7 +83,6 @@
</div>
</div>
</div>
</div>
@endsection
@push('scripts')
@ -92,7 +91,7 @@
function deleteData(data) {
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!" ,
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
@ -109,7 +108,7 @@
$.ajax(`locations/provinces/${data}`, {
type: 'DELETE'
}).then((response) => {
swal.fire('Deleted!', 'User has been deleted.','success').then(() => {
swal.fire('Deleted!', 'User has been deleted.', 'success').then(() => {
window.location.reload();
});
}).catch((error) => {
@ -165,7 +164,7 @@
// Custom search functionality
searchInput.addEventListener('input', function () {
const searchValue = this.value.trim();
if(searchValue.length > 3) {
if (searchValue.length > 3) {
dataTable.search(searchValue, true);
}
});

View File

@ -16,7 +16,6 @@
@endpush
@section('content')
<div class="container-fluid">
<div class="grid">
<div class="card card-grid min-w-full" data-datatable="false" data-datatable-page-size="5" data-datatable-state-save="false" id="villages-table" data-api-url="{{ route('locations.villages.datatables') }}">
<div class="card-header py-5 flex-wrap">
@ -111,7 +110,6 @@
</div>
</div>
</div>
</div>
@endsection
@push('scripts')
@ -120,7 +118,7 @@
function deleteData(data) {
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!" ,
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
@ -214,6 +212,7 @@
};
let dataTable = new KTDataTable(element, dataTableOptions);
dataTable.showSpinner();
// Custom search functionality
searchInput.addEventListener('keyup', function () {
const _province = document.getElementById('province_code').value;

View File

@ -13,7 +13,3 @@ use Modules\Location\Http\Controllers\LocationController;
* is assigned the "api" middleware group. Enjoy building your API!
*
*/
Route::middleware(['auth:sanctum'])->prefix('v1')->group(function () {
Route::apiResource('location', LocationController::class)->names('location');
});

View File

@ -47,6 +47,8 @@ use Illuminate\Support\Facades\Route;
Route::get('restore/{id}', [VillagesController::class, 'restore'])->name('restore');
Route::get('datatables', [VillagesController::class, 'dataForDatatables'])->name('datatables');
Route::get('export', [VillagesController ::class, 'export'])->name('export');
Route::get('district/{district_code}', [VillagesController::class, 'getVillagesByDistrictId'])->name('district');
Route::get('postal-code/{village_code}', [VillagesController::class, 'getPostalCodesByVillageId'])->name('postalcode');
});
Route::resource('villages', VillagesController::class);
});