## 表格構建器渲染輸出
> 說明:
該方法用于表格構建器的渲染輸出。
注意:需在控制器內,使用構建器實例對象調用`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();
}
}
```
- 序言
- 基礎
- 下載安裝
- 配置
- 版本查看
- 控制器
- 使用說明
- 輸出構建器參數
- 視圖模板
- 使用說明
- 區塊重寫
- seo
- head_style
- head_script
- content
- script
- 驗證器
- TP驗證器
- 使用說明
- 自動處理
- 數據處理
- 前端驗證器
- 概述
- 調用示例
- 通用驗證規則
- 表單構建器
- 基礎示例
- 初始化
- 鏈式操作
- tab
- url
- field
- fieldValidate
- button
- load
- submit
- 渲染輸出
- 字段配置
- 使用說明
- 隱藏域(hidden)
- 文本框(text)
- 文本域(textarea)
- 密碼框(password)
- 富文本(editor)
- 文檔編輯(markdown)
- 下拉選框(select)
- 單選框(radio)
- 復選框(checkbox)
- 開關(switch)
- 日期(date)
- 時間日期(datetime)
- 年份(year)
- 月份(month)
- 時間(time)
- 單圖(img)
- 多圖(imgs)
- 單文件(file)
- 多文件(files)
- 取色器(color)
- 地區聯動(area)
- 下拉搜索(dropdown_search)
- 樹(tree)
- 自定義字段
- 單一字段調用
- 概述
- 示例
- 表格構建器
- 基礎示例
- 初始化
- 鏈式操作
- state
- url
- page
- limit
- limits
- filter
- filterValidate
- toolbar
- defaultToolbar
- import
- cols
- count
- items
- event
- 渲染輸出
- 表頭配置
- 使用說明
- 操作欄(action)
- 時間日期(datetime)
- 單圖(img)
- 多圖(imgs)
- 單文件(file)
- 多文件(files)
- 拖拽排序(drag_sort)
- 顏色呈現(color)
- 地區呈現(area)
- 枚舉(enum)
- 自定義表頭
- JS事件
- 異步事件監聽
- 導入構建器
- 基礎示例
- 初始化
- 鏈式操作
- sheets
- cols
- colsValidate
- limit
- tips
- row
- rows
- 渲染輸出
- 主題開發
- 實現步驟
- 系統主題
- 注意
- PHP公共方法庫
- 配置
- yunj_config
- 構建器
- YF
- YT
- YI
- 重定向
- url_tips
- redirect_tips
- throw_redirect
- 響應輸出
- response_msg
- response_json
- success_json
- error_json
- throw_json
- throw_success_json
- throw_error_json
- 數組
- array_eq
- array_in
- array_supp
- array_depth
- array_insert
- array_key_prefix
- 驗證
- is_mobile
- is_positive_integer
- is_json
- is_datetime
- is_md5_result
- 時間日期
- msectime
- 字符串
- rand_char
- filter_sql
- start_with
- exception_to_str
- JS公共方法庫
- 調用說明
- 數據類型
- varType
- 判斷
- isMobile
- isObj
- isEmptyObj
- isArray
- isEmptyArray
- isString
- isEmptyString
- isBool
- isNumber
- isFloat
- isUndefined
- isJson
- isCsv
- isXls
- isXlsx
- 字符串
- fileExt
- fileNameExt
- currTimestamp
- currDatetime
- timestampFormat
- 對象
- objSupp
- 圖片
- previewImg
- url
- url
- urlParam
- urlPushParam
- 頁面
- openNewPage
- openTab
- openPopup
- rawPageWin
- redirectTab
- redirectLogin
- isPopupPage
- isTabPage
- currPageId
- close
- closeCurr
- closeAll
- 網絡
- request
- 附錄
- 升級指導
- 更新日志