# 渲染输出
表格构建器渲染输出
方法一:页面内只有单一表格时
注意!此方法仅支持渲染一个表格构建器,不需要写视图文件
控制器代码示例如下:
namespace app\demo\controller; class FormDemo { public function demo(){ $builder=YT('demo') ->state([11=>'正常',22=>"回收站"]) ->filter([ 'name'=>['title'=>'姓名'] ]) ->toolbar(function($state){ $toolbar=['add'=>['type'=>'openPopup','title'=>'添加','icon'=>'layui-icon layui-icon-add-circle','url'=>url('add')]]; switch ($state){ case 11: $toolbar+=[ 22=>['type'=>'asyncEvent','title'=>'移入回收站','dropdown'=>true], ]; break; case 22: $toolbar+=[ 11=>['type'=>'asyncEvent','title'=>'还原','dropdown'=>true], 33=>['type'=>'asyncEvent','title'=>'永久删除','dropdown'=>true], ]; break; } return $toolbar; }) ->defaultToolbar('filter','export','print') ->import(url("demo/importDemo/index")) ->cols(function($state){ $cols=[ 'id'=>['type'=>'checkbox'], 'name'=>['title'=>'姓名'], 'create_time'=>['title'=>'添加时间', 'align'=>'center', 'hide'=>'mobile_hide_hide','templet'=>'datetime'], 'action'=>[ 'title'=>'操作', 'templet'=>'action', 'options'=>[ 'edit'=>['type'=>'openPopup','title'=>'详情','icon'=>'layui-icon layui-icon-survey','url'=>url('edit')] ] ] ]; switch ($state){ case 11: $cols['action']['options']+=[ 22=>['type'=>'asyncEvent','title'=>'移入回收站','dropdown'=>true], ]; break; case 22: $cols['action']['options']+=[ 11=>['type'=>'asyncEvent','title'=>'还原','dropdown'=>true], 33=>['type'=>'asyncEvent','title'=>'永久删除','dropdown'=>true] ]; break; } return $cols; }) ->count(function($filter){ // 固定参数 $state=$filter['state']; // 当前选中数据行主键集合数组。若设置有对应的表格构建器”pk“,则主键集合键名为设置的”pk“+"s",默认为”ids“ $ids=$filter['ids']; // 筛选表单参数 $name=$filter['name']; $whereArr=[]; $whereArr[]=$state?['state','eq',$state]:['state','neq',33]; if($ids) $whereArr[]=['id','in',$ids]; if($name) $whereArr[]=['name_cn','like','%'.$name.'%']; $count = $this->model->getOwnCount($whereArr); return $count; }) ->items(function ($limit_start,$limit_length,$filter,$sort){ // 固定参数 $state=$filter['state']; // 当前选中数据行主键集合数组。若设置有对应的表格构建器”pk“,则主键集合键名为设置的”pk“+"s",默认为”ids“ $ids=$filter['ids']; // 筛选表单参数 $name=$filter['name']; $whereArr=[]; $whereArr[]=$state?['state','eq',$state]:['state','neq',33]; if($ids) $whereArr[]=['id','in',$ids]; if($name) $whereArr[]=['name_cn','like','%'.$name.'%']; $orderArr=$sort+['id'=>'desc']; $items = $this->model->getOwnRows(["*"],$whereArr,$orderArr,$limit_start,$limit_length); return $items; }) ->event(function ($event,$ids){ foreach ($ids as $id){ $whereArr=[['id','eq',$id]]; switch ($event){ case 11: $updateData=['state'=>11]; $whereArr[]=['state','eq',22]; break; case 22: $updateData=['state'=>22]; $whereArr[]=['state','eq',11]; break; case 33: $updateData=['state'=>33]; $whereArr[]=['state','eq',22]; break; default: return error_json(); } if($updateData) $this->model->change($updateData,$whereArr); } return success_json(); }); return view_table($builder); } }
不需要视图文件!
方法二:自定义页面内容时
注意!此方法支持一个页面渲染多个表格构建器,需要写视图文件
控制器代码示例如下:
namespace app\demo\controller; class FormDemo { public function demo(){ // 表格构建器配置参考方法一 $builder=YT('demo'); // 多个构建器可反复调用此方法 $builder->assign(); $builder->assign(); return $this->fetch(); } }
视图代码示例如下:
{extend name="$adminPage"} {block name="content"} <table type="yunj" id="demo"></table> {/block}