Template2.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <template>
  2. <a-card :bordered="false">
  3. <a-row :gutter="8">
  4. <!-- 左侧父 -->
  5. <a-col :span="12">
  6. <JVxeTable
  7. toolbar
  8. rowNumber
  9. rowSelection
  10. clickSelectRow
  11. highlightCurrentRow
  12. :radioConfig="{ highlight: false }"
  13. :checkboxConfig="{ highlight: false }"
  14. :height="790"
  15. :loading="table1.loading"
  16. :columns="table1.columns"
  17. :dataSource="table1.dataSource"
  18. :pagination="table1.pagination"
  19. @pageChange="handleTable1PageChange"
  20. @selectRowChange="handleTable1SelectRowChange"
  21. />
  22. </a-col>
  23. <a-col :span="12">
  24. <!-- 左侧选择的数据展示在这里 -->
  25. <JVxeTable rowNumber :height="375" :columns="table1.columns" :dataSource="table1.selectedRows" style="margin: 52px 0 8px" />
  26. <!-- 右下子 -->
  27. <JVxeTable
  28. toolbar
  29. rowNumber
  30. rowSelection
  31. clickSelectRow
  32. :height="355"
  33. :loading="table2.loading"
  34. :columns="table2.columns"
  35. :dataSource="table2.dataSource"
  36. :pagination="table2.pagination"
  37. @pageChange="handleTable2PageChange"
  38. @selectRowChange="handleTable2SelectRowChange"
  39. />
  40. </a-col>
  41. </a-row>
  42. </a-card>
  43. </template>
  44. <script>
  45. import { defHttp } from '/@/utils/http/axios';
  46. import { JVxeTypes } from '/@/components/jeecg/JVxeTable/types';
  47. // 【多种布局模板】 左边选择后,记录选到右侧,右侧是父、子
  48. export default {
  49. name: 'Template2',
  50. data() {
  51. return {
  52. table1: {
  53. // 是否正在加载
  54. loading: false,
  55. // 分页器参数
  56. pagination: {
  57. // 当前页码
  58. current: 1,
  59. // 每页的条数
  60. pageSize: 200,
  61. // 可切换的条数
  62. pageSizeOptions: ['10', '20', '30', '100', '200'],
  63. // 数据总数(目前并不知道真实的总数,所以先填写0,在后台查出来后再赋值)
  64. total: 0,
  65. },
  66. // 最后选中的行
  67. lastRow: null,
  68. // 选择的行
  69. selectedRows: [],
  70. // 数据源,控制表格的数据
  71. dataSource: [],
  72. // 列配置,控制表格显示的列
  73. columns: [
  74. { key: 'num', title: '序号', width: '80px' },
  75. {
  76. // 字段key,跟后台数据的字段名匹配
  77. key: 'ship_name',
  78. // 列的标题
  79. title: '船名',
  80. // 列的宽度
  81. width: '180px',
  82. // 如果加上了该属性,就代表当前单元格是可编辑的,type就是表单的类型,input就是简单的输入框
  83. type: JVxeTypes.input,
  84. },
  85. { key: 'call', title: '呼叫', width: '80px', type: JVxeTypes.input },
  86. { key: 'len', title: '长', width: '80px', type: JVxeTypes.input },
  87. { key: 'ton', title: '吨', width: '120px', type: JVxeTypes.input },
  88. { key: 'payer', title: '付款方', width: '120px', type: JVxeTypes.input },
  89. { key: 'count', title: '数', width: '40px' },
  90. {
  91. key: 'company',
  92. title: '公司',
  93. // 最小宽度,与宽度不同的是,这个不是固定的宽度,如果表格有多余的空间,会平均分配给设置了 minWidth 的列
  94. // 如果要做占满表格的列可以这么写
  95. minWidth: '180px',
  96. type: JVxeTypes.input,
  97. },
  98. { key: 'trend', title: '动向', width: '120px', type: JVxeTypes.input },
  99. ],
  100. },
  101. // 子级表的配置信息 (配置和主表的完全一致,就不写冗余的注释了)
  102. table2: {
  103. loading: false,
  104. pagination: { current: 1, pageSize: 200, pageSizeOptions: ['100', '200'], total: 0 },
  105. selectedRows: [],
  106. dataSource: [],
  107. columns: [
  108. { key: 'dd_num', title: '调度序号', width: '120px' },
  109. { key: 'tug', title: '拖轮', width: '180px', type: JVxeTypes.input },
  110. { key: 'work_start_time', title: '作业开始时间', width: '180px', type: JVxeTypes.input },
  111. { key: 'work_stop_time', title: '作业结束时间', width: '180px', type: JVxeTypes.input },
  112. { key: 'type', title: '船舶分类', width: '120px', type: JVxeTypes.input },
  113. { key: 'port_area', title: '所属港区', width: '120px', type: JVxeTypes.input },
  114. ],
  115. },
  116. // 查询url地址
  117. url: {
  118. getData: '/mock/vxe/getData',
  119. },
  120. };
  121. },
  122. // 监听器
  123. watch: {
  124. // 监听table1 【主表】选择的数据发生了变化
  125. ['table1.lastRow']() {
  126. this.loadTable2Data();
  127. },
  128. },
  129. created() {
  130. this.loadTable1Data();
  131. },
  132. methods: {
  133. // 加载table1【主表】的数据
  134. loadTable1Data() {
  135. // 封装查询条件
  136. let formData = {
  137. pageNo: this.table1.pagination.current,
  138. pageSize: this.table1.pagination.pageSize,
  139. };
  140. // 调用查询数据接口
  141. this.table1.loading = true;
  142. defHttp
  143. .get({
  144. url: this.url.getData,
  145. params: formData,
  146. })
  147. .then((result) => {
  148. // 后台查询回来的 total,数据总数量
  149. this.table1.pagination.total = result.total;
  150. // 将查询的数据赋值给 dataSource
  151. this.table1.dataSource = result.records;
  152. // 重置选择
  153. this.table1.selectedRows = [];
  154. })
  155. .finally(() => {
  156. // 这里是无论成功或失败都会执行的方法,在这里关闭loading
  157. this.table1.loading = false;
  158. });
  159. },
  160. // 加载table2【子表】的数据,根据主表的id进行查询
  161. loadTable2Data() {
  162. // 如果主表没有选择,则不查询
  163. let selectedRows = this.table1.selectedRows;
  164. if (!selectedRows || selectedRows.length === 0) {
  165. this.table2.pagination.total = 0;
  166. this.table2.dataSource = [];
  167. this.table2.selectedRows = [];
  168. return;
  169. } else if (this.table1.lastRow == null) {
  170. this.table1.lastRow = selectedRows[selectedRows.length - 1];
  171. }
  172. let formData = {
  173. parentId: this.table1.lastRow.id,
  174. pageNo: this.table2.pagination.current,
  175. pageSize: this.table2.pagination.pageSize,
  176. };
  177. this.table2.loading = true;
  178. defHttp
  179. .get({
  180. url: this.url.getData,
  181. params: formData,
  182. })
  183. .then((result) => {
  184. this.table2.pagination.total = result.total;
  185. this.table2.dataSource = result.records;
  186. this.table2.selectedRows = [];
  187. })
  188. .finally(() => {
  189. this.table2.loading = false;
  190. });
  191. },
  192. // table1【主表】当选择的行变化时触发的事件
  193. handleTable1SelectRowChange(event) {
  194. this.handleTableSelectRowChange(this.table1, event);
  195. },
  196. // table2【子表】当选择的行变化时触发的事件
  197. handleTable2SelectRowChange(event) {
  198. this.table2.selectedRows = event.selectedRows;
  199. },
  200. // 当table1【主表】分页参数变化时触发的事件
  201. handleTable1PageChange(event) {
  202. // 重新赋值
  203. this.table1.pagination.current = event.current;
  204. this.table1.pagination.pageSize = event.pageSize;
  205. // 查询数据
  206. this.loadTable1Data();
  207. },
  208. // 当table2【子表】分页参数变化时触发的事件
  209. handleTable2PageChange(event) {
  210. // 重新赋值
  211. this.table2.pagination.current = event.current;
  212. this.table2.pagination.pageSize = event.pageSize;
  213. // 查询数据
  214. this.loadTable2Data();
  215. },
  216. /** 公共方法:处理表格选中变化事件 */
  217. handleTableSelectRowChange(table, event) {
  218. let { row, action, selectedRows, $table } = event;
  219. // 获取最后一个选中的
  220. let lastSelected = selectedRows[selectedRows.length - 1];
  221. if (action === 'selected') {
  222. table.lastRow = row;
  223. } else if (action === 'selected-all') {
  224. // 取消全选
  225. if (selectedRows.length === 0) {
  226. table.lastRow = null;
  227. } else if (!table.lastRow) {
  228. table.lastRow = lastSelected;
  229. }
  230. } else if (action === 'unselected' && row === table.lastRow) {
  231. table.lastRow = lastSelected;
  232. }
  233. $table.setCurrentRow(table.lastRow);
  234. table.selectedRows = selectedRows;
  235. },
  236. },
  237. };
  238. </script>
  239. <style scoped></style>