laravel-admin 从入门到弃用(三、多级联动选择、回填)

那年仲夏 提交于 2020-04-29 11:58:40

假装之前的安装、页面基本操作都没问题了,现在开始选择控件的多级联动、回填,以省市区三级联动为demo,

一、首先是 多级联动选择

在 控制器的 form() 方法中 

$form->select('province', __('省市'))->options($provInfo)->load('city', url('/admin/api/cityInfo'));

$form->select('city', __('市区'))->load('area', url('/admin/api/areaInfo'));

$form->select('area', __('区县'))

然后在 app/Admin/route.php 中添加添加 上面的url,放在当前控制器前面,或者顶端都可以

创建一个 Areas 的 Model 文件,在文件如下

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Areas extends Model{

    protected $table = 'area';

//    protected $primaryKey = 'id';

    public function table(){
        return $this->table;
    }

    /**
     * @return array
     */
    public function provinceInfo(){
        $provList =  DB::select("SELECT area_id, `area_name` FROM va_area WHERE parent_id = -1");
        if($provList){
            $selectOption = [];
            foreach ($provList as $option){
                $selectOption[$option->area_id] = $option->area_name;
            }
            return $selectOption;
        }
        return [];
    }

    /**
     * @return array
     */
    public function childInfo($parentId){
        if(!is_numeric($parentId)){return [];}
        $childList =  DB::select("SELECT area_id, `area_name` FROM va_area WHERE parent_id = ".$parentId);
        if($childList){
            $selectOption = [];
            foreach ($childList as $key => $option){
                $selectOption[$key]['id'] = $option->area_id;
                $selectOption[$key]['text'] = $option->area_name;
            }

            return $selectOption;
        }
        return [];
    }

    /**
     * @return array
     */
    public function localInfo($localId){
        if(!is_numeric($localId)){return [];}
        $childList =  DB::select("SELECT area_id, `area_name` FROM va_area WHERE area_id = ".$localId);
        if($childList){
            $selectOption = [];
            foreach ($childList as $option){
                $selectOption['id'] = $option->area_id;
                $selectOption['text'] = $option->area_name;
            }
            return $selectOption;
        }
        return [];
    }
}

在 上述url 的控制器中添加以下方法

/**
     * 城市信息
     */
    public function cityInfo(Request $request){
        $q = $request->get('q');
        $special = [1, 19, 792, 2236];

//        $provList =  DB::select("SELECT area_id AS id, `area_name` AS text FROM va_area WHERE parent_id = -1");
//        return Areas::city()->where('parent_id', '=', $q)->paginate(null, ['area_id AS id', 'area_name AS text']);
        $area = new Areas();
        $areaInfo = $area->childInfo($q);
//        if(in_array($q,$special)){ $areaInfo = $area->childInfo($areaInfo[0]['id']); }
        return $areaInfo;
    }

    /**
     * 区县信息
     */
    public function areaInfo(Request $request){
        $q = $request->get('q');
        $area = new Areas();
        return $area->childInfo($q);
    }

 

表单回填

$area = new Areas();
$provInfo = $area->provinceInfo();
$form->select('province', __('省市'))->options($provInfo)->load('city', url('/admin/api/cityInfo'));

$form->select('city', __('市区'))->options(function ($id){
     $area = new Areas();
     $cityInfo = $area->localInfo($id);
     $cityInfo = [$cityInfo['id']=>$cityInfo['text']];
     return $cityInfo;
})->load('area', url('/admin/api/areaInfo'));

$form->select('area', __('区县'))->options(function ($id){
      $area = new Areas();
      $cityInfo = $area->localInfo($id);
      $cityInfo = [$cityInfo['id']=>$cityInfo['text']];
      return $cityInfo;
});

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!