laravel-admin表单提交两级联动功能编写
Laravel  /  管理员 发布于 2年前   3745
laravel-admin表单提交两级联动功能编写;
控制器:
1.
namespace App\Admin\Controllers;
use App\Http\Controllers\Controller;
use App\Models\SelectOption;
use App\Models\ProductCreditCard;
use App\Models\Top;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
2.
public function form()
{
$form = new Form(new Top());
//这里就是我的两级联动
$form->select('bankid','银行')->options(
SelectOption::companycreditcards()
)->load('cid', '/api/top/cid');//选中传入接口一个q参数,加载到第二级
//加上需options不然编辑页不会选中
$form->select('cid','信用卡')->options(function ($id) {
return ProductCreditCard::where('id',$id)->pluck('name', 'id');
});
$form->select('ctype','类别')->options(SelectOption::categoryOps([1591,1599,1608,1621]));
$form->display('created_at');
$form->display('updated_at');
//$form->switch('status', '状态')->states($this->states);
return $form;
}
3.加载的接口(我也写在这个控制器里面--->需写路由过来)
public function cid(Request $request)
{
$id = $request->get('q');
return ProductCreditCard::where('CompanyID', $id)->get(['id', DB::raw('name as text')]);
}
路由:
//选中加载返回数据
$router->get('api/top/cid', 'TopController@cid');
看看效果:
针对评论留言中@lin的问题:第二级分类编辑如果要想选择该父类下的其他子类
稍作修改代码如下:
$form->select('bankid','银行')->options(
SelectOption::companycreditcards()
)->load('cid', '/api/top/cid');
$form->select('cid','信用卡')->options(function () {
//获取父类id
$fid = $this -> bankid;
//查询出父类下的所有子类
$return = ProductCreditCard::where('CompanyID',$fid)->pluck('name', 'id');
return $returm;
});
博主 在
2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..xiaoB 在
2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..BUG4 在
你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..博主 在
go语言+beego框架中获取get,post请求的所有参数中评论 @ t1 直接在router.go文件中配就ok..Jade 在
如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号