当前位置: 首页 > news >正文

模型树实操

很多时候,数据都是有层级和分类的,使用laravel的Dcat框架,可以快速搭建一个结构清晰、且可以鼠标拖拽排序的后台;先上例子更直观:
在这里插入图片描述
这里是Dcat的模型树使用文档,戳一下;

重点注意事项有:

  • 1、数据库表的parent_id字段一定要默认为0,
  • 2、数据库表parent_id、order、title一定要有,不过这三个字段支持修改,可在Model中进行修改;其它字段没有要求
<?phpnamespace App\Models\Demo;use Dcat\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\Model;class Category extends Model
{use ModelTree;protected $table = 'demo_categories';// 父级ID字段名称,默认值为 parent_idprotected $parentColumn = 'pid';// 排序字段名称,默认值为 orderprotected $orderColumn = 'sort';// 标题字段名称,默认值为 titleprotected $titleColumn = 'name';// Since v2.1.6-beta,定义depthColumn属性后,将会在数据表保存当前行的层级protected $depthColumn = 'depth';
}
tree的使用

去 Dcat 后台用代码生成工具生成 Controller 文件,生成以后直接用我下面的代码覆盖:

<?phpnamespace App\Admin\Controllers\SecondPhase;use App\Models\CancerDetectionMethods;
use App\Models\CancerDetectionSamples;
use App\Models\CancerRecommendSearch;
use App\Models\CancerSiteInfo;
use App\Models\CancerSiteMethods;
use App\Models\CancerTargets;
use App\Models\LsDicLabel;
use App\Services\CancerTargetTreesService;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Tree;
use Dcat\Admin\Widgets\Box;
use Dcat\Admin\Widgets\Form as WidgetForm;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;class CancerTargetsController extends AdminController
{public function index(Content $content){return $content->title('模型树')->body(function (Row $row) {$row->column(6, $this->treeView()->render());$row->column(6, function (Column $column) {$form = new WidgetForm();$form->action(admin_url('cancer-target-tree'));$form->select('parent_id', '父级')->options(CancerTargets::selectOptions());$form->text('title', '名称')->required();$column->append(Box::make('新增', $form));});});}protected function treeView(){return new Tree(new CancerTargets(), function (Tree $tree) {$tree->disableCreateButton();$tree->disableQuickCreateButton();$tree->disableEditButton();$tree->maxDepth(4);$tree->branch(function ($branch) {$payload = $branch['id'].' - '.$branch['title'];if ($branch['level'] === 4) {$url     = admin_url('/cancer-site/'.$branch['id'].'/edit');$payload .= <<<HTML
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="$url" class="dd-nodrag">位点管理</a>
HTML;}return $payload;});});}protected function form(){return Form::make(new CancerTargets(), function (Form $form) {$form->model()->with(['methods:id, name',]);$form->display('id');$form->select('parent_id', '父级')->options(CancerTargets::selectOptions())->saving(function ($v) {return (int)$v;});$form->text('title', '名称')->required();$form->hidden('level', '等级');$form->disableViewButton();$form->footer(function ($footer) {// 去掉`重置`按钮$footer->disableReset();// 去掉`查看`checkbox$footer->disableViewCheck();// 去掉`继续编辑`checkbox$footer->disableEditingCheck();// 去掉`继续创建`checkbox$footer->disableCreatingCheck();// 设置`查看`默认选中$footer->defaultViewChecked();// 设置`继续编辑`默认选中$footer->defaultEditingChecked();// 设置`继续创建`默认选中$footer->defaultCreatingChecked();});});}}

大功告成

http://www.lryc.cn/news/268234.html

相关文章:

  • html table+css实现可编辑表格
  • c语言:计算1+2+3……+n的和|练习题
  • Yolov5水果分类识别+pyqt交互式界面
  • 正式环境和sandbox中的RecordTypeId不一样应该怎么办
  • 7种常见的网络安全设备及其功能
  • vue3实现pinia仓库状态持久化
  • Node.js(三)-模块的加载机制
  • 函数
  • 第四周:机器学习知识点回顾
  • 韩版传奇 2 源码分析与 Unity 重制(一)服务端 TCP 状态管理
  • python编程(2)之灯光秀的按钮
  • pandas将dataframe列中的list转换为多列
  • 小巧的Windows Memory Cleaner内存清理工具-释放内存,提升电脑的性能-供大家学习研究参考
  • STM32F072 CAN and USB
  • 卷积神经网络基础与补充
  • File Inclusion(Pikachu)
  • 【Redis刨析】知识图谱的构建与实现
  • html学习笔记 标题、段落、换行、列表、超链接、图片、表格
  • 汽车项目管理
  • Tg-5511cb: tcxo高稳定性+105℃高温
  • Linux 命令 ifconfig 全面解析!
  • 00-Git 应用
  • 安全运维是做什么的,主要工作内容是什么
  • nodejs+vue+ElementUi大学新生入学系统的设计与实现1hme0
  • Unity-序列化和反序列化
  • 如何将语音版大模型AI接入自己的项目里(语音ChatGPT)
  • 鸿蒙列表,item组件封装传参问题?@ObjectLink 和@Observerd
  • 信号与线性系统翻转课堂笔记12——时域取样定理
  • 爬虫工作量由小到大的思维转变---<Scrapy异常的存放小探讨>
  • 7.串口通信uart编写思路及自定义协议