DataTable.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. //decode by http://www.yunlu99.com/
  3. namespace App\Lib\DataTable;
  4. class DataTable extends \App\Lib\Facade
  5. {
  6. private $dataId;
  7. //表格ID
  8. private $dataSource = '';
  9. private $cols = array();
  10. private $lang = '';
  11. private $toolbar = -1;
  12. //array('view','edit','delete');
  13. private $toolbarCols = array();
  14. private $priKey = 'id';
  15. private $enableCheckBoxed = false;
  16. static function init()
  17. {
  18. if (S('dataTable') == -1) {
  19. S('dataTable', new self());
  20. }
  21. return S('dataTable');
  22. }
  23. function __construct()
  24. {
  25. $this->cols = array(array('type' => 'checkbox'));
  26. $this->toolbarCols = array('width' => 100, 'align' => 'center', 'toolbar' => '#toolbar', 'title' => trans('common.operation'));
  27. }
  28. /**
  29. *添加表头字段
  30. * [addColsFields description]
  31. * @param [type] $field [description]
  32. * @param array $params [description]
  33. */
  34. public function addColsFields($field, $params = array())
  35. {
  36. if (!empty($this->lang)) {
  37. $title = trans($this->lang . '.' . $field);
  38. } else {
  39. $title = $field;
  40. }
  41. $cols = array('field' => $field, 'title' => $title, 'sort' => true, 'minWidth' => 120);
  42. $cols = array_merge($cols, $params);
  43. $this->addCols($cols);
  44. }
  45. /**
  46. * 开启checkbox
  47. *
  48. * @return void
  49. */
  50. function enableCheckBox()
  51. {
  52. $this->enableCheckBoxed = true;
  53. }
  54. /**
  55. * 关闭checkbox
  56. *
  57. * @return void
  58. */
  59. function disableCheckBox()
  60. {
  61. $this->enableCheckBoxed = false;
  62. }
  63. /**
  64. * 设置主键
  65. * [setPriKey description]
  66. * @param [type] $id [description]
  67. */
  68. public function setPriKey($id)
  69. {
  70. $this->priKey = $id;
  71. }
  72. /**
  73. *添加表头
  74. * [addCols description]
  75. * @param [type] $item [description]
  76. */
  77. public function addCols($item)
  78. {
  79. $this->cols[] = $item;
  80. }
  81. /**
  82. *设置左边栏操作
  83. * [setToolBar description]
  84. * @param string $toolbar [description]
  85. */
  86. public function setToolBar($toolbar = '', $args = array())
  87. {
  88. if (empty($toolbar)) {
  89. $this->toolbar = -1;
  90. return;
  91. }
  92. if (is_array($toolbar) && count($toolbar) < 1) {
  93. $this->toolbar = -1;
  94. return;
  95. }
  96. $this->toolbar = $toolbar;
  97. $this->toolbarCols = array_merge($this->toolbarCols, $args);
  98. }
  99. /**
  100. *设置语言包
  101. * [setLang description]
  102. * @param [type] $lang [description]
  103. */
  104. public function setLang($lang)
  105. {
  106. $this->lang = $lang;
  107. }
  108. /**
  109. *设置表头
  110. * [setCols description]
  111. * @param [type] $data [description]
  112. */
  113. public function setCols($data)
  114. {
  115. $this->cols[] = $data;
  116. return $this;
  117. }
  118. /**
  119. *设置表格id
  120. * [setDataId description]
  121. * @param [type] $id [description]
  122. */
  123. public function setDataId($id)
  124. {
  125. $this->dataId = $id;
  126. return $this;
  127. }
  128. /**
  129. *调用数据接口地址
  130. * [setDataSource description]
  131. * @param [type] $url [description]
  132. */
  133. public function setDataSource($url)
  134. {
  135. $this->dataSource = $url;
  136. return $this;
  137. }
  138. /**
  139. *返回数据表格的渲染信息
  140. * [render description]
  141. * @return [type] [description]
  142. */
  143. public function render($arr = array())
  144. {
  145. if (empty($this->dataId)) {
  146. $this->dataId = "demo";
  147. }
  148. if (empty($this->dataSource)) {
  149. $this->dataSource = '/' . S('MODULE') . '/' . S("CONTROLLER") . '/dataSource';
  150. }
  151. if (!$this->enableCheckBoxed) {
  152. unset($this->cols[0]);
  153. }
  154. if ($this->toolbar != -1) {
  155. // $tmp = $this->cols;
  156. // $this->cols = array();
  157. $this->cols[] = $this->toolbarCols;
  158. // $this->cols = array_merge($this->cols, $tmp);
  159. }
  160. $data = array('dataId' => $this->dataId, 'dataUri' => $this->dataSource, 'dataCols' => array($this->cols), 'dataToolBar' => $this->toolbar, 'primaryKey' => $this->priKey, 'dataTableLang' => $this->lang);
  161. $data = array_merge($arr, $data);
  162. return $data;
  163. }
  164. /**
  165. *返回json数据
  166. * [toJson description]
  167. * @param [type] $data [description]
  168. * @param integer $count [description]
  169. * @param integer $code [description]
  170. * @param string $msg [description]
  171. * @return [type] [description]
  172. */
  173. public function toJson($data, $count = 1, $code = 0, $msg = '', $extras = array())
  174. {
  175. if (is_array($msg)) {
  176. $extras = $msg;
  177. $msg = '';
  178. }
  179. return $this->responseToJson(array('code' => $code, 'msg' => $msg, 'count' => $count, 'data' => $data, 'extras' => $extras));
  180. }
  181. public function responseToJson($params)
  182. {
  183. return json_encode($params);
  184. }
  185. }