| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- //decode by http://www.yunlu99.com/
- namespace App\Lib\DataTable;
- class DataTable extends \App\Lib\Facade
- {
- private $dataId;
- //表格ID
- private $dataSource = '';
- private $cols = array();
- private $lang = '';
- private $toolbar = -1;
- //array('view','edit','delete');
- private $toolbarCols = array();
- private $priKey = 'id';
- private $enableCheckBoxed = false;
- static function init()
- {
- if (S('dataTable') == -1) {
- S('dataTable', new self());
- }
- return S('dataTable');
- }
- function __construct()
- {
- $this->cols = array(array('type' => 'checkbox'));
- $this->toolbarCols = array('width' => 100, 'align' => 'center', 'toolbar' => '#toolbar', 'title' => trans('common.operation'));
- }
- /**
- *添加表头字段
- * [addColsFields description]
- * @param [type] $field [description]
- * @param array $params [description]
- */
- public function addColsFields($field, $params = array())
- {
- if (!empty($this->lang)) {
- $title = trans($this->lang . '.' . $field);
- } else {
- $title = $field;
- }
- $cols = array('field' => $field, 'title' => $title, 'sort' => true, 'minWidth' => 120);
- $cols = array_merge($cols, $params);
- $this->addCols($cols);
- }
- /**
- * 开启checkbox
- *
- * @return void
- */
- function enableCheckBox()
- {
- $this->enableCheckBoxed = true;
- }
- /**
- * 关闭checkbox
- *
- * @return void
- */
- function disableCheckBox()
- {
- $this->enableCheckBoxed = false;
- }
- /**
- * 设置主键
- * [setPriKey description]
- * @param [type] $id [description]
- */
- public function setPriKey($id)
- {
- $this->priKey = $id;
- }
- /**
- *添加表头
- * [addCols description]
- * @param [type] $item [description]
- */
- public function addCols($item)
- {
- $this->cols[] = $item;
- }
- /**
- *设置左边栏操作
- * [setToolBar description]
- * @param string $toolbar [description]
- */
- public function setToolBar($toolbar = '', $args = array())
- {
- if (empty($toolbar)) {
- $this->toolbar = -1;
- return;
- }
- if (is_array($toolbar) && count($toolbar) < 1) {
- $this->toolbar = -1;
- return;
- }
- $this->toolbar = $toolbar;
- $this->toolbarCols = array_merge($this->toolbarCols, $args);
- }
- /**
- *设置语言包
- * [setLang description]
- * @param [type] $lang [description]
- */
- public function setLang($lang)
- {
- $this->lang = $lang;
- }
- /**
- *设置表头
- * [setCols description]
- * @param [type] $data [description]
- */
- public function setCols($data)
- {
- $this->cols[] = $data;
- return $this;
- }
- /**
- *设置表格id
- * [setDataId description]
- * @param [type] $id [description]
- */
- public function setDataId($id)
- {
- $this->dataId = $id;
- return $this;
- }
- /**
- *调用数据接口地址
- * [setDataSource description]
- * @param [type] $url [description]
- */
- public function setDataSource($url)
- {
- $this->dataSource = $url;
- return $this;
- }
- /**
- *返回数据表格的渲染信息
- * [render description]
- * @return [type] [description]
- */
- public function render($arr = array())
- {
- if (empty($this->dataId)) {
- $this->dataId = "demo";
- }
- if (empty($this->dataSource)) {
- $this->dataSource = '/' . S('MODULE') . '/' . S("CONTROLLER") . '/dataSource';
- }
- if (!$this->enableCheckBoxed) {
- unset($this->cols[0]);
- }
- if ($this->toolbar != -1) {
- // $tmp = $this->cols;
- // $this->cols = array();
- $this->cols[] = $this->toolbarCols;
- // $this->cols = array_merge($this->cols, $tmp);
- }
- $data = array('dataId' => $this->dataId, 'dataUri' => $this->dataSource, 'dataCols' => array($this->cols), 'dataToolBar' => $this->toolbar, 'primaryKey' => $this->priKey, 'dataTableLang' => $this->lang);
- $data = array_merge($arr, $data);
- return $data;
- }
- /**
- *返回json数据
- * [toJson description]
- * @param [type] $data [description]
- * @param integer $count [description]
- * @param integer $code [description]
- * @param string $msg [description]
- * @return [type] [description]
- */
- public function toJson($data, $count = 1, $code = 0, $msg = '', $extras = array())
- {
- if (is_array($msg)) {
- $extras = $msg;
- $msg = '';
- }
- return $this->responseToJson(array('code' => $code, 'msg' => $msg, 'count' => $count, 'data' => $data, 'extras' => $extras));
- }
- public function responseToJson($params)
- {
- return json_encode($params);
- }
- }
|