Add function to get json data for select2

This commit is contained in:
daeng.deni@dharma.or.id 2023-06-10 18:44:00 +07:00
parent 058baccedf
commit 015ea5ea1d
7 changed files with 79 additions and 39 deletions

View File

@ -35,17 +35,12 @@
*
* @return Renderable
*/
public function index(CityDataTable $dataTable, Request $request)
public function index(CityDataTable $dataTable)
{
if (is_null($this->user) || !$this->user->can($this->module->alias . '.read')) {
abort(403, 'Sorry !! You are Unauthorized to view any ' . $this->module->alias . ' !');
}
if (isset($request->province_id) && !empty($request->province_id)) {
$this->show($request);
return;
}
addVendor('chained-select');
$countries = Country::all();
@ -111,9 +106,19 @@
abort(403, 'Sorry !! You are Unauthorized to view any ' . $this->module->alias . ' !');
}
$cities = City::where('province_id', $request->province_id)->get();
abort(404);
}
public function select2(Request $request)
{
if (is_null($this->user) || !$this->user->can($this->module->alias . '.read')) {
abort(403, 'Sorry !! You are Unauthorized to view any ' . $this->module->alias . ' !');
}
$data = [];
if ($request->has('province_id') && $request->province_id != '') {
$cities = City::where('province_id', $request->province_id)->get();
foreach ($cities as $row) {
$result = [
$row->id => $row->name,
@ -121,10 +126,12 @@
$data[] = $result;
}
}
echo json_encode($data);
}
/**
* Show the form for editing the specified resource.
*

View File

@ -35,15 +35,12 @@
*
* @return Renderable
*/
public function index(DistrictDataTable $dataTable, Request $request)
public function index(DistrictDataTable $dataTable)
{
if (is_null($this->user) || !$this->user->can($this->module->alias . '.read')) {
abort(403, 'Sorry !! You are Unauthorized to view any ' . $this->module->alias . ' !');
}
if (isset($request->city_id) && !empty($request->city_id)) {
$this->show($request);
return;
}
addVendor('chained-select');
$countries = Country::all();
return $dataTable->render($this->module->alias . '::district.index', compact('countries'));
@ -121,6 +118,30 @@
echo json_encode($data);
}
public function select2(Request $request)
{
if (is_null($this->user) || !$this->user->can($this->module->alias . '.read')) {
abort(403, 'Sorry !! You are Unauthorized to view any ' . $this->module->alias . ' !');
}
$data = [];
if ($request->has('city_id') && $request->city_id != '') {
$districts = District::where('city_id', $request->city_id)->get();
foreach ($districts as $row) {
$result = [
'id' => $row->id,
'text' => $row->name,
];
$data[] = $result;
}
}
echo json_encode($data);
}
/**
* Show the form for editing the specified resource.
*

View File

@ -35,17 +35,12 @@
*
* @return Renderable
*/
public function index(ProvinceDataTable $dataTable, Request $request)
public function index(ProvinceDataTable $dataTable)
{
if (is_null($this->user) || !$this->user->can($this->module->alias . '.read')) {
abort(403, 'Sorry !! You are Unauthorized to view any ' . $this->module->alias . ' !');
}
if (isset($request->country_id) && !empty($request->country_id)) {
$this->show($request);
return;
}
$countries = Country::all();
return $dataTable->render($this->module->alias . '::province.index', compact('countries'));
@ -64,9 +59,19 @@
abort(403, 'Sorry !! You are Unauthorized to view any ' . $this->module->alias . ' !');
}
$provinces = Province::where('country_id', $request->country_id)->get();
abort(404);
}
public function select2(Request $request)
{
if (is_null($this->user) || !$this->user->can($this->module->alias . '.read')) {
abort(403, 'Sorry !! You are Unauthorized to view any ' . $this->module->alias . ' !');
}
$data = [];
if ($request->has('country_id') && $request->country_id != '') {
$provinces = Province::where('country_id', $request->country_id)->get();
foreach ($provinces as $row) {
$result = [
$row->id => $row->name,
@ -74,7 +79,7 @@
$data[] = $result;
}
}
echo json_encode($data);
}

View File

@ -134,7 +134,7 @@
$("#{{$route[0].'_'.$route[1]}}_province_id").remoteChained({
parents: "#{{$route[0].'_'.$route[1]}}_country_id",
url: "/master/province"
url: "/master/province-select2"
});
Inputmask({

View File

@ -135,12 +135,12 @@
$("#{{$route[0].'_'.$route[1]}}_province_id").remoteChained({
parents: "#{{$route[0].'_'.$route[1]}}_country_id",
url: "/master/province"
url: "/master/province-select2"
});
$("#{{$route[0].'_'.$route[1]}}_city_id").remoteChained({
parents: "#{{$route[0].'_'.$route[1]}}_province_id",
url: "/master/city"
url: "/master/city-select2"
});
Inputmask({

View File

@ -136,17 +136,17 @@
$("#{{$route[0].'_'.$route[1]}}_province_id").remoteChained({
parents: "#{{$route[0].'_'.$route[1]}}_country_id",
url: "/master/province"
url: "/master/province-select2"
});
$("#{{$route[0].'_'.$route[1]}}_city_id").remoteChained({
parents: "#{{$route[0].'_'.$route[1]}}_province_id",
url: "/master/city"
url: "/master/city-select2"
});
$("#{{$route[0].'_'.$route[1]}}_district_id").remoteChained({
parents: "#{{$route[0].'_'.$route[1]}}_city_id",
url: "/master/district"
url: "/master/district-select2"
});
Inputmask({

View File

@ -37,8 +37,15 @@
Route::resource('card', CardController::class);
Route::resource('country', CountryController::class);
Route::resource('province', ProvinceController::class);
Route::get('province-select2', [ProvinceController::class, 'select2'])->name('province.select2');
Route::resource('city', CityController::class);
Route::get('city-select2', [CityController::class, 'select2'])->name('city.select2');
Route::resource('district', DistrictController::class);
Route::get('district-select2', [DistrictController::class, 'select2'])->name('district.select2');
Route::resource('sub-district', SubDistrictController::class);
});
});