Feature #3 : Districts
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import TomSelect from "tom-select";
|
||||
|
||||
let settings = {
|
||||
plugins: ['dropdown_input'],
|
||||
create: true,
|
||||
createOnBlur: true
|
||||
};
|
||||
|
||||
const provinceSelect = document.querySelector('#province_code');
|
||||
const citySelect = document.querySelector('#city_code');
|
||||
|
||||
if (provinceSelect && !citySelect) {
|
||||
new TomSelect('#province_code', settings);
|
||||
}
|
||||
|
||||
if (citySelect) {
|
||||
var tomcities = new TomSelect('#city_code', settings);
|
||||
var tomprovince = new TomSelect('#province_code', {
|
||||
...settings, // Spread existing settings
|
||||
onChange: function (value) {
|
||||
|
||||
// Destroy only if necessary (prevents unnecessary re-initialization)
|
||||
if (tomcities) {
|
||||
tomcities.destroy();
|
||||
}
|
||||
|
||||
var 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
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error fetching data:', error);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -29,7 +29,8 @@
|
||||
Province
|
||||
</label>
|
||||
<div class="flex flex-wrap items-baseline w-full">
|
||||
<select name="province_code" class="select tomselect w-full @error('province_id') border-danger @enderror">
|
||||
<select id="province_code" name="province_code" class="select w-full @error('province_code') border-danger @enderror">
|
||||
<option value="">Select Province</option>
|
||||
@foreach($provinces as $province)
|
||||
@if(isset($city))
|
||||
<option value="{{ $province->code }}" {{ isset($city->province_code) && $city->province_code == $province->code?'selected' : '' }}>
|
||||
|
||||
112
resources/views/districts/create.blade.php
Normal file
112
resources/views/districts/create.blade.php
Normal file
@@ -0,0 +1,112 @@
|
||||
@extends('layouts.main')
|
||||
|
||||
@section('breadcrumbs')
|
||||
{{ Breadcrumbs::render(request()->route()->getName()) }}
|
||||
@endsection
|
||||
|
||||
@section('content')
|
||||
<div class="w-full grid gap-5 lg:gap-7.5 mx-auto">
|
||||
@if(isset($district->id))
|
||||
<form action="{{ route('locations.districts.update', $district->id) }}" method="POST">
|
||||
<input type="hidden" name="id" value="{{ $district->id }}">
|
||||
@method('PUT')
|
||||
@else
|
||||
<form method="POST" action="{{ route('locations.districts.store') }}">
|
||||
@endif
|
||||
@csrf
|
||||
<div class="card pb-2.5">
|
||||
<div class="card-header" id="basic_settings">
|
||||
<h3 class="card-title">
|
||||
{{ isset($district->id) ? 'Edit' : 'Add' }} City
|
||||
</h3>
|
||||
<div class="flex items-center gap-2">
|
||||
<a href="{{ route('locations.districts.index') }}" class="btn btn-xs btn-info">Back</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-body grid gap-5">
|
||||
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||
<label class="form-label max-w-56">
|
||||
Province
|
||||
</label>
|
||||
<div class="flex flex-wrap items-baseline w-full">
|
||||
<select id="province_code" name="province_code" class="select w-full @error('province_code') border-danger @enderror">
|
||||
<option value="">Select Province</option>
|
||||
@foreach($provinces as $province)
|
||||
@if(isset($district))
|
||||
<option value="{{ $province->code }}" {{ isset($district->province_code) && $district->province_code == $province->code?'selected' : '' }}>
|
||||
{{ $province->name }}
|
||||
</option>
|
||||
@else
|
||||
<option value="{{ $province->code }}">
|
||||
{{ $province->name }}
|
||||
</option>
|
||||
@endif
|
||||
@endforeach
|
||||
</select>
|
||||
@error('province_code')
|
||||
<em class="alert text-danger text-sm">{{ $message }}</em>
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||
<label class="form-label max-w-56">
|
||||
City
|
||||
</label>
|
||||
<div class="flex flex-wrap items-baseline w-full">
|
||||
<select id="city_code" name="city_code" class="select w-full @error('city_code') border-danger @enderror">
|
||||
<option value="">Select City</option>
|
||||
@if(isset($cities))
|
||||
@foreach($cities as $city)
|
||||
@if(isset($district))
|
||||
<option value="{{ $city->code }}" {{ isset($district->city_code) && $district->city_code == $city->code?'selected' : '' }}>
|
||||
{{ $city->name }}
|
||||
</option>
|
||||
@else
|
||||
<option value="{{ $city->code }}">
|
||||
{{ $city->name }}
|
||||
</option>
|
||||
@endif
|
||||
@endforeach
|
||||
|
||||
@endif
|
||||
</select>
|
||||
@error('city_code')
|
||||
<em class="alert text-danger text-sm">{{ $message }}</em>
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||
<label class="form-label max-w-56">
|
||||
Code
|
||||
</label>
|
||||
<div class="flex flex-wrap items-baseline w-full">
|
||||
<input class="input @error('code') border-danger @enderror" type="text" name="code" value="{{ $district->code ?? '' }}">
|
||||
@error('code')
|
||||
<em class="alert text-danger text-sm">{{ $message }}</em>
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
|
||||
<label class="form-label max-w-56">
|
||||
Name
|
||||
</label>
|
||||
<div class="flex flex-wrap items-baseline w-full">
|
||||
<input class="input @error('name') border-danger @enderror" type="text" name="name" value="{{ $district->name ?? '' }}">
|
||||
@error('name')
|
||||
<em class="alert text-danger text-sm">{{ $message }}</em>
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex justify-end">
|
||||
<button type="submit" class="btn btn-primary">
|
||||
Save
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@push('scripts')
|
||||
@endpush
|
||||
192
resources/views/districts/index.blade.php
Normal file
192
resources/views/districts/index.blade.php
Normal file
@@ -0,0 +1,192 @@
|
||||
@extends('layouts.main')
|
||||
|
||||
@section('breadcrumbs')
|
||||
{{ Breadcrumbs::render('locations.districts') }}
|
||||
@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="true" id="districts-table" data-api-url="{{ route('locations.districts.datatables') }}">
|
||||
<div class="card-header py-5 flex-wrap">
|
||||
<h3 class="card-title">
|
||||
List of Districts
|
||||
</h3>
|
||||
<div class="flex flex-wrap gap-2 lg:gap-5">
|
||||
<div class="flex">
|
||||
<label class="input input-sm"> <i class="ki-filled ki-magnifier"> </i>
|
||||
<input placeholder="Search districts" id="search" type="text" value="">
|
||||
|
||||
</label>
|
||||
</div>
|
||||
<div class="flex flex-wrap gap-2.5">
|
||||
<select class="select select-sm w-28">
|
||||
<option value="1">
|
||||
Active
|
||||
</option>
|
||||
<option value="2">
|
||||
Disabled
|
||||
</option>
|
||||
<option value="2">
|
||||
Pending
|
||||
</option>
|
||||
</select>
|
||||
<select class="select select-sm w-28">
|
||||
<option value="desc">
|
||||
Latest
|
||||
</option>
|
||||
<option value="asc">
|
||||
Oldest
|
||||
</option>
|
||||
</select>
|
||||
<button class="btn btn-sm btn-outline btn-primary">
|
||||
<i class="ki-filled ki-setting-4"> </i> <Filters></Filters>
|
||||
</button>
|
||||
<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>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="scrollable-x-auto">
|
||||
<table class="table table-auto table-border align-middle text-gray-700 font-medium text-sm" data-datatable-table="true">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="w-14">
|
||||
<input class="checkbox checkbox-sm" data-datatable-check="true" type="checkbox"/>
|
||||
</th>
|
||||
<th class="min-w-[250px]" data-datatable-column="code">
|
||||
<span class="sort"> <span class="sort-label"> Code </span>
|
||||
<span class="sort-icon"> </span> </span>
|
||||
</th>
|
||||
<th class="min-w-[250px]" data-datatable-column="name">
|
||||
<span class="sort"> <span class="sort-label"> District </span>
|
||||
<span class="sort-icon"> </span> </span>
|
||||
</th>
|
||||
<th class="min-w-[250px]" data-datatable-column="city">
|
||||
<span class="sort"> <span class="sort-label"> City </span>
|
||||
<span class="sort-icon"> </span> </span>
|
||||
</th>
|
||||
<th class="min-w-[250px]" data-datatable-column="province">
|
||||
<span class="sort"> <span class="sort-label"> Province </span>
|
||||
<span class="sort-icon"> </span> </span>
|
||||
</th>
|
||||
<th class="min-w-[50px] text-center" data-datatable-column="actions">Action</th>
|
||||
</tr>
|
||||
</thead>
|
||||
</table>
|
||||
</div>
|
||||
<div class="card-footer justify-center md:justify-between flex-col md:flex-row gap-3 text-gray-600 text-2sm font-medium">
|
||||
<div class="flex items-center gap-2">
|
||||
Show
|
||||
<select class="select select-sm w-16" data-datatable-size="true" name="perpage"> </select> per page
|
||||
</div>
|
||||
<div class="flex items-center gap-4">
|
||||
<span data-datatable-info="true"> </span>
|
||||
<div class="pagination" data-datatable-pagination="true">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
@push('scripts')
|
||||
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
|
||||
<script type="text/javascript">
|
||||
function deleteData(data) {
|
||||
Swal.fire({
|
||||
title: 'Are you sure?',
|
||||
text: "You won't be able to revert this!" + data,
|
||||
icon: 'warning',
|
||||
showCancelButton: true,
|
||||
confirmButtonColor: '#3085d6',
|
||||
cancelButtonColor: '#d33',
|
||||
confirmButtonText: 'Yes, delete it!'
|
||||
}).then((result) => {
|
||||
if (result.isConfirmed) {
|
||||
$.ajaxSetup({
|
||||
headers: {
|
||||
'X-CSRF-TOKEN': '{{ csrf_token() }}'
|
||||
}
|
||||
});
|
||||
|
||||
$.ajax(`locations/districts/${data}`, {
|
||||
type: 'DELETE'
|
||||
}).then((response) => {
|
||||
swal.fire('Deleted!', 'User has been deleted.','success').then(() => {
|
||||
window.location.reload();
|
||||
});
|
||||
}).catch((error) => {
|
||||
console.error('Error:', error);
|
||||
Swal.fire('Error!', 'An error occurred while deleting the file.', 'error');
|
||||
});
|
||||
}
|
||||
})
|
||||
}
|
||||
</script>
|
||||
<script type="module">
|
||||
const element = document.querySelector('#districts-table');
|
||||
const searchInput = document.getElementById('search');
|
||||
|
||||
const apiUrl = element.getAttribute('data-api-url');
|
||||
const dataTableOptions = {
|
||||
apiEndpoint: apiUrl,
|
||||
pageSize: 5,
|
||||
columns: {
|
||||
select: {
|
||||
render: (item, data, context) => {
|
||||
const checkbox = document.createElement('input');
|
||||
checkbox.className = 'checkbox checkbox-sm';
|
||||
checkbox.type = 'checkbox';
|
||||
checkbox.value = data.id.toString();
|
||||
checkbox.setAttribute('data-datatable-row-check', 'true');
|
||||
return checkbox.outerHTML.trim();
|
||||
},
|
||||
},
|
||||
code: {
|
||||
title: 'Code',
|
||||
},
|
||||
name: {
|
||||
title: 'District',
|
||||
},
|
||||
city: {
|
||||
title: 'City',
|
||||
render: (item, data) => {
|
||||
return data.city.name;
|
||||
}
|
||||
},
|
||||
province: {
|
||||
title: 'Province',
|
||||
render: (item, data) => {
|
||||
return data.city.province.name;
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
title: 'Status',
|
||||
render: (item, data) => {
|
||||
return `<div class="flex flex-nowrap justify-center">
|
||||
<a class="btn btn-sm btn-icon btn-clear btn-info" href="locations/districts/${data.id}/edit">
|
||||
<i class="ki-outline ki-notepad-edit"></i>
|
||||
</a>
|
||||
<a onclick="deleteData(${data.id})" class="delete btn btn-sm btn-icon btn-clear btn-danger">
|
||||
<i class="ki-outline ki-trash"></i>
|
||||
</a>
|
||||
</div>`;
|
||||
},
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
let dataTable = new KTDataTable(element, dataTableOptions);
|
||||
// Custom search functionality
|
||||
searchInput.addEventListener('input', function () {
|
||||
const searchValue = this.value.trim();
|
||||
dataTable.search(searchValue, true);
|
||||
});
|
||||
</script>
|
||||
@endpush
|
||||
|
||||
Reference in New Issue
Block a user