columns.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import type {
  2. LoadingConfig,
  3. AdaptiveConfig,
  4. PaginationProps
  5. } from "@pureadmin/table";
  6. import { ref, reactive } from "vue";
  7. import { delObjectProperty } from "@pureadmin/utils";
  8. import { clone, delay } from "@pureadmin/utils";
  9. import Anchor from "@/model/anchor.js";
  10. export function useColumns() {
  11. const loading = ref(true);
  12. const editMap = ref({});
  13. const dataList = ref([]);
  14. const options = ref([]);
  15. const columns: TableColumnList = [
  16. {
  17. label: "ID",
  18. prop: "id",
  19. cellRenderer: ({ row, index }) => (
  20. <>
  21. {editMap.value[index]?.editable ? (
  22. <el-input v-model={row.id} />
  23. ) : (
  24. <p>{row.id}</p>
  25. )}
  26. </>
  27. )
  28. },
  29. {
  30. label: "IMEI",
  31. prop: "imei",
  32. cellRenderer: ({ row, index }) => (
  33. <>
  34. {editMap.value[index]?.editable ? (
  35. <el-input v-model={row.imei} />
  36. ) : (
  37. <p>{row.imei}</p>
  38. )}
  39. </>
  40. )
  41. },
  42. {
  43. label: "ICCID",
  44. prop: "iccid",
  45. cellRenderer: ({ row, index }) => (
  46. <>
  47. {editMap.value[index]?.editable ? (
  48. <el-input v-model={row.iccid} />
  49. ) : (
  50. <p>{row.iccid}</p>
  51. )}
  52. </>
  53. )
  54. },
  55. {
  56. label: "项目ID",
  57. prop: "projId",
  58. cellRenderer: ({ row, index }) => (
  59. <>
  60. {editMap.value[index]?.editable ? (
  61. <el-select v-model={row.projId} clearable placeholder="请选择爱好">
  62. {options.value.map(item => {
  63. return (
  64. <el-option
  65. key={item.value}
  66. label={item.label}
  67. value={item.value}
  68. />
  69. );
  70. })}
  71. </el-select>
  72. ) : (
  73. <el-tag type="primary">
  74. {options.value.filter(opt => opt.value == row.projId)[0]?.label}
  75. </el-tag>
  76. )}
  77. </>
  78. )
  79. },
  80. {
  81. label: "电池",
  82. prop: "battery",
  83. cellRenderer: ({ row, index }) => (
  84. <p>{row.battery}</p>
  85. )
  86. },
  87. {
  88. label: "状态",
  89. prop: "status",
  90. cellRenderer: ({ row, index }) => (
  91. <p>{row.status}</p>
  92. )
  93. },
  94. {
  95. label: "操作",
  96. fixed: "right",
  97. slot: "operation"
  98. }
  99. ];
  100. /** 分页配置 */
  101. const pagination = reactive<PaginationProps>({
  102. pageSize: 20,
  103. currentPage: 1,
  104. pageSizes: [20, 40, 60],
  105. total: 0,
  106. align: "right",
  107. background: true,
  108. size: "default"
  109. });
  110. /** 加载动画配置 */
  111. const loadingConfig = reactive<LoadingConfig>({
  112. text: "正在加载第一页...",
  113. viewBox: "-10, -10, 50, 50",
  114. spinner: `
  115. <path class="path" d="
  116. M 30 15
  117. L 28 17
  118. M 25.61 25.61
  119. A 15 15, 0, 0, 1, 15 30
  120. A 15 15, 0, 1, 1, 27.99 7.5
  121. L 15 15
  122. " style="stroke-width: 4px; fill: rgba(0, 0, 0, 0)"/>
  123. `
  124. // svg: "",
  125. // background: rgba()
  126. });
  127. /** 撑满内容区自适应高度相关配置 */
  128. const adaptiveConfig: AdaptiveConfig = {
  129. /** 表格距离页面底部的偏移量,默认值为 `96` */
  130. offsetBottom: 110
  131. /** 是否固定表头,默认值为 `true`(如果不想固定表头,fixHeader设置为false并且表格要设置table-layout="auto") */
  132. // fixHeader: true
  133. /** 页面 `resize` 时的防抖时间,默认值为 `60` ms */
  134. // timeout: 60
  135. /** 表头的 `z-index`,默认值为 `100` */
  136. // zIndex: 100
  137. };
  138. function onSizeChange(val) {
  139. console.log("onSizeChange", val);
  140. }
  141. function onCurrentChange(val) {
  142. loadingConfig.text = `正在加载第${val}页...`;
  143. loading.value = true;
  144. delay(600).then(() => {
  145. loading.value = false;
  146. });
  147. }
  148. function onEdit(row, index) {
  149. editMap.value[index] = Object.assign({ ...row, editable: true });
  150. }
  151. function onSave(index) {
  152. editMap.value[index].editable = false;
  153. }
  154. function onCancel(index) {
  155. editMap.value[index].editable = false;
  156. dataList.value[index] = delObjectProperty(editMap.value[index], "editable");
  157. }
  158. function onAdd() {
  159. const anchor = new Anchor();
  160. anchor.id = dataList.value.length + 1;
  161. dataList.value.push(anchor);
  162. }
  163. function onDel(row) {
  164. const index = dataList.value.indexOf(row);
  165. if (index !== -1) dataList.value.splice(index, 1);
  166. }
  167. return {
  168. loading,
  169. editMap,
  170. columns,
  171. dataList,
  172. options,
  173. pagination,
  174. loadingConfig,
  175. adaptiveConfig,
  176. onAdd,
  177. onDel,
  178. onEdit,
  179. onSave,
  180. onCancel,
  181. onSizeChange,
  182. onCurrentChange
  183. };
  184. }