HisiPHP内置的表格构建器同表单构建器一样的简单,无需引入任何类库,与TP的模板赋值方法一样赋值即可,页面渲染方法与TP完全一致。
    下面提供一段示例代码,在后面的章节会针对不同的配置项做具体说明:

    1. public function index()
    2. {
    3. if ($this->request->isAjax()) {
    4. $page = $this->request->param('page/d', 1);
    5. $limit = $this->request->param('limit/d', 20);
    6. $title = $this->request->param('title');
    7. $cid = $this->request->param('cid');
    8. $createTime = $this->request->param('create_time');
    9. $where = [];
    10. $title && $where[] = ['title', 'like', "%{$title}%"];
    11. $cid && $where[] = ['cid', 'in', $cid];
    12. if ($createTime) {
    13. [$startTime, $endTime] = explode(' - ', $createTime);
    14. $where[] = ['create_time', 'between', [strtotime($startTime), strtotime($endTime)]];
    15. }
    16. $data = $this->modelArticle->where($where)->page($page)->limit($limit)->select();
    17. $count = $this->modelArticle->count('id');
    18. return $this->layuiJson($data, $count);
    19. }
    20. // 表格构建器
    21. $assign['buildTable']['config'] = [// 配置文档 https://www.layui.com/doc/modules/table.html#options
    22. 'page' => true,
    23. 'cols' => [
    24. [
    25. 'type' => 'checkbox',
    26. ],
    27. [
    28. 'field' => 'cid',
    29. 'title' => '所属分类',
    30. 'templet' => '<div><a href="'.url('index').'?cid={{ d.cid }}" class="hisi-table-a-filter">{{ d.hasCategory.name || "---" }}</a></div>',
    31. 'width' => 100,
    32. 'align' => 'center',
    33. ],
    34. [
    35. 'field' => 'title',
    36. 'title' => '文章标题',
    37. 'templet' => '<div><a href="/example/article/detail?id={{ d.id }}" target="_blank">{{ d.title }}</a></div>'
    38. ],
    39. [
    40. 'field' => 'image',
    41. 'title' => '图片预览',
    42. 'align' => 'center',
    43. 'type' => 'image',// 图片预览模式
    44. 'width' => 100,
    45. ],
    46. [
    47. 'field' => 'status',
    48. 'title' => '状态',
    49. 'type' => 'switch',// 状态切换
    50. 'align' => 'center',
    51. 'width' => 70,
    52. ],
    53. [
    54. 'field' => 'create_time',
    55. 'title' => '创建时间',
    56. 'align' => 'center',
    57. 'width' => 170,
    58. ],
    59. [
    60. 'field' => 'title',
    61. 'title' => '预览',
    62. 'width' => 70,
    63. 'templet' => '<div><a class="layui-btn layui-btn-xs layui-btn-normal" href="/example/article/detail?id={{ d.id }}" target="_blank">预览</a></div>'
    64. ],
    65. [
    66. 'title' => '操作',
    67. 'width' => 120,
    68. 'align' => 'center',
    69. 'button' => [
    70. [
    71. 'title' => '编辑',
    72. 'class' => 'layui-btn layui-btn-xs hisi-iframe',// 如需弹窗编辑 加 hisi-iframe
    73. 'url' => url('edit'),
    74. 'attrs' => [//定义标签的扩展属性
    75. 'data-options' => [
    76. 'width' => '800px'
    77. ],
    78. ],
    79. ],
    80. [
    81. 'title' => '删除',
    82. 'class' => 'hisi-tr-del layui-btn layui-btn-xs layui-btn-danger',// 无刷新删除 必须加 hisi-tr-del
    83. 'url' => url('delete'),
    84. ],
    85. ]
    86. ]
    87. ],
    88. ];
    89. //表格工具栏
    90. $assign['buildTable']['toolbar'] = [
    91. [
    92. 'title' => '添加',
    93. 'url' => url('add'),// 推荐使用url函数定义,构建器渲染的时候会自动过滤权限
    94. 'class' => 'hisi-iframe',
    95. 'attrs' => [//定义标签的扩展属性
    96. 'data-options' => [
    97. 'width' => '800px'
    98. ],
    99. ],
    100. ],
    101. [
    102. 'title' => '删除',
    103. 'url' => url('delete'),
    104. 'class' => 'hisi-table-ajax layui-btn-danger',
    105. ],
    106. ];
    107. // 表格筛选器
    108. $assign['buildTable']['filter'] = [
    109. 'items' => [
    110. [
    111. 'type' => 'text',
    112. 'title' => '标题',
    113. 'name' => 'title',
    114. ],
    115. [
    116. 'type' => 'date',
    117. 'title' => '时间筛选',
    118. 'name' => 'create_time',
    119. 'attrs' => [
    120. 'data-options' => [// 配置文档 https://www.layui.com/doc/modules/laydate.html#use
    121. 'range' => true,
    122. ]
    123. ]
    124. ],
    125. [
    126. 'type' => 'select+',
    127. 'title' => '分类',
    128. 'name' => 'cid',
    129. 'attrs' => [
    130. 'data-options' => [
    131. // 配置文档 https://maplemei.gitee.io/xm-select/#/component/options
    132. 'autoRow' => true,
    133. 'filterable' => false,
    134. 'clickClose' => true,
    135. 'radio' => true,
    136. 'tree' => [
    137. 'show' => true,
    138. 'strict' => true,
    139. ],
    140. 'prop' => [
    141. 'value' => 'id',
    142. ],
    143. 'model' => [
    144. 'label' => [
    145. 'type' => 'text',
    146. ],
    147. ],
    148. 'data' => $this->getCategorys()
    149. ],
    150. ],
    151. ]
    152. ],
    153. ];
    154. return $this->assign($assign)->fetch();
    155. }

    image.png