合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
## 表格构建器渲染输出 > 说明: 该方法用于表格构建器的渲染输出。 注意:需在控制器内,使用构建器实例对象调用`assign`方法实现。 > 示例: ```php <?php namespace app\demo\controller; use yunj\Controller; class TableDemo extends Controller { // 方式一 public function demo1(){ $builder=YT('demo1') ->state([11=>'正常',22=>"回收站"]) ->filter(function($state){ $filter=[ 'name'=>['title'=>'姓名'] ]; return $filter; }) ->toolbar(function($state){ $toolbar=['add'=>['type'=>'open_popup','title'=>'添加','icon'=>'layui-icon layui-icon-add-circle','url'=>url('add')]]; switch ($state){ case 11: $toolbar+=[ 22=>['type'=>'async_event','title'=>'移入回收站','dropdown'=>true], ]; break; case 22: $toolbar+=[ 11=>['type'=>'async_event','title'=>'还原','dropdown'=>true], 33=>['type'=>'async_event','title'=>'永久删除','dropdown'=>true], ]; break; } return $toolbar; }) ->defaultToolbar(function($state){ return ['filter','export','print']; }) ->import(function($state){ return 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'=>'open_popup','title'=>'详情','icon'=>'layui-icon layui-icon-survey','url'=>url('edit')] ] ] ]; switch ($state){ case 11: $cols['action']['options']+=[ 22=>['type'=>'async_event','title'=>'移入回收站','dropdown'=>true], ]; break; case 22: $cols['action']['options']+=[ 11=>['type'=>'async_event','title'=>'还原','dropdown'=>true], 33=>['type'=>'async_event','title'=>'永久删除','dropdown'=>true] ]; break; } return $cols; }) ->count(function($filter){ // 固定参数 $state=$filter['state']; $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']; $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(); }); $builder->assign($this); return $this->fetch(); } // 方式二 public function demo2(){ $args = [ "state"=>[11=>'正常',22=>"回收站"], "filter"=>function($state){ $filter=[ 'name'=>['title'=>'姓名'] ]; return $filter; }, "toolbar"=>function($state){ $toolbar=['add'=>['type'=>'open_popup','title'=>'添加','icon'=>'layui-icon layui-icon-add-circle','url'=>url('add')]]; switch ($state){ case 11: $toolbar+=[ 22=>['type'=>'async_event','title'=>'移入回收站','dropdown'=>true], ]; break; case 22: $toolbar+=[ 11=>['type'=>'async_event','title'=>'还原','dropdown'=>true], 33=>['type'=>'async_event','title'=>'永久删除','dropdown'=>true], ]; break; } return $toolbar; }, "defaultToolbar"=>function($state){ return ['filter','export','print']; }, "import"=>function($state){ return 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'=>'open_popup','title'=>'详情','icon'=>'layui-icon layui-icon-survey','url'=>url('edit')] ] ] ]; switch ($state){ case 11: $cols['action']['options']+=[ 22=>['type'=>'async_event','title'=>'移入回收站','dropdown'=>true], ]; break; case 22: $cols['action']['options']+=[ 11=>['type'=>'async_event','title'=>'还原','dropdown'=>true], 33=>['type'=>'async_event','title'=>'永久删除','dropdown'=>true] ]; break; } return $cols; }, "count"=>function($filter){ // 固定参数 $state=$filter['state']; $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']; $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(); } ]; $builder=YT('demo2',$args); $builder->assign($this); return $this->fetch(); } } ```