where(array("pid"=>0)) ->select(array("id","name")) ->get() ->toArray(); if (!empty($oneLevelData)){ $oneLevelData = array_column($oneLevelData,"name","id"); } $grid->column('id', __('Id')); $subjectData = MentalityDrillCategory::query() ->where(array("is_subject"=>MentalityDrillCategory::isSubjectYes)) ->select(array("id","name")) ->get() ->toArray(); if (!empty($subjectData)){ $subjectData = array_column($subjectData,"name","id"); } $grid->filter(function(\Encore\Admin\Grid\Filter $filter) use ($oneLevelData){ // 在这里添加字段过滤器 $filter->like('title', '标题'); $filter->equal('category_id', '训练')->select( $oneLevelData ); $filter->expand(); }); $grid->column('category_id', __('一级分类'))->display(function ($categoryId) use ($oneLevelData,$subjectData){ if ($categoryId > 0 && isset($oneLevelData[$categoryId])){ return isset($subjectData[$categoryId]) ? $oneLevelData[$categoryId]." [课程]" : $oneLevelData[$categoryId]; } return ''; }); $grid->column('two_category_id', __('二级分类'))->display(function ($twoCategoryId) use ($oneLevelData){ if ($twoCategoryId>0){ $twoLevelData = MentalityDrillCategory::query() ->where(array("id"=>$twoCategoryId)) ->select(array("id","name")) ->first(); if ($twoLevelData){ return $twoLevelData->name; } } return ''; }); $grid->column('title', __('标题')); $grid->column('number_of_studies', __('学习次数')); $grid->column('rank', __('排序 越大越前')); $grid->column('rank', __('排序 越大越前')); $grid->column('created_at', __('创建时间')); $grid->column('updated_at', __('更新时间')); $grid->actions(function (Grid\Displayers\Actions $actions) use ($subjectData) { $categoryId = $actions->row->category_id; if (isset($subjectData[$categoryId])){ $actions->add(new CustomAction( "课程列表", admin_url("/mentality_drill_subject")."?". http_build_query(array("mentality_drills_id"=>$actions->row->id)) )); } // 去掉删除 $actions->disableDelete(); }); return $grid; } /** * Make a show builder. * * @param mixed $id * @return Show */ protected function detail($id) { $show = new Show(MentalityDrill::findOrFail($id)); $show->field('id', __('Id')); $show->field('category_id', __('Category id')); $show->field('title', __('Title')); $show->field('subtitle', __('Subtitle')); $show->field('pic', __('Pic')); $show->field('content', __('Content')); $show->field('number_of_studies', __('Number of studies')); $show->field('rank', __('Rank')); $show->field('created_at', __('Created at')); $show->field('updated_at', __('Updated at')); $show->field('deleted_at', __('Deleted at')); return $show; } /** * Make a form builder. * * @return Form */ protected function form() { $form = new Form(new MentalityDrill()); Admin::script(" $(document).ready(function() { // 页面加载完毕后执行的代码 console.log('页面已加载完毕'); $('.category_id').trigger('change'); }); "); $oneLevelData = MentalityDrillCategory::query() ->where(array("pid"=>0)) ->select(array("id","name")) ->get() ->toArray(); if (!empty($oneLevelData)){ $oneLevelData = array_column($oneLevelData,"name","id"); } $form->select('category_id', '一级分类') ->options($oneLevelData) ->load('two_category_id', '/admin/lowerLevel') ->required(); $form->select('two_category_id', '二级分类')->options([]); $form->text('title', __('标题'))->required(); $form->text('subtitle', __('副标题'))->required(); $form->image('pic', __('图片'))->removable()->uniqueName()->move('pics/'.date('Y').'/'.date('m')); $form->editor('content', __('内容'))->required(); $form->number('number_of_studies', __('学习次数'))->default(0); $form->number('rank', __('排序 越大越前'))->default(0); $form->list('comment_tag', __('推荐标签')); return $form; } }