index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <el-dialog
  3. v-model="isShowTable"
  4. :title="tableTitle"
  5. width="1200"
  6. :before-close="handleClose"
  7. >
  8. <avue-crud
  9. ref="crudRef"
  10. v-model:search="search"
  11. v-model="form"
  12. :data="data"
  13. :option="option"
  14. v-model:page="page"
  15. @row-click="rowClick"
  16. @row-save="createRow"
  17. @row-update="updateRow"
  18. @row-del="deleteRow"
  19. @search-change="searchChange"
  20. @search-reset="resetChange"
  21. @size-change="dataList"
  22. @current-change="dataList"
  23. @selection-change="selectionChange"
  24. />
  25. <template #footer>
  26. <div class="dialog-footer">
  27. <el-button @click="handleClose">取消</el-button>
  28. <el-button type="primary" @click="onSelected"> 确定 </el-button>
  29. </div>
  30. </template>
  31. </el-dialog>
  32. </template>
  33. <script setup>
  34. import { ref, getCurrentInstance } from "vue";
  35. import { useCrud } from "@/hooks/userCrud";
  36. import { tableConfig } from "./configs/tableConfig";
  37. const props = defineProps({
  38. tableTitle: {
  39. default: "",
  40. type: String,
  41. },
  42. tableType: {
  43. default: "",
  44. type: String,
  45. },
  46. multiple: {
  47. default: false,
  48. type: Boolean,
  49. },
  50. // 用于多选的返回值
  51. multipleKey: {
  52. default: "id",
  53. type: String,
  54. },
  55. });
  56. const isShowTable = ref(false);
  57. const startSelect = async (param) => {
  58. if (param) {
  59. commonConfig.value.params = param;
  60. }
  61. dataList();
  62. isShowTable.value = true;
  63. };
  64. /**
  65. * propName 要跟字典配置的key一致
  66. * dictData 字典数据, 注意props的value和label
  67. * */
  68. const refreshDictData = (propName, dictData, keyName = "value") => {
  69. nextTick(() => {
  70. if (dictData && dictData.length > 0 && crudRef.value) {
  71. crudRef.value.updateDic(propName, dictData);
  72. search.value[propName] = dictData[0][keyName];
  73. }
  74. });
  75. };
  76. /**
  77. * op 对象的字段要跟option实际字段一直 才能进行合并操作
  78. */
  79. const mergeOption = (op) => {
  80. for (const key of Object.keys(op)) {
  81. option.value[key] = op[key];
  82. }
  83. };
  84. // 传入一个url,后面不带/
  85. const {
  86. url,
  87. form,
  88. data,
  89. option,
  90. search,
  91. page,
  92. toDeleteIds,
  93. Methords,
  94. Utils,
  95. commonConfig,
  96. } = useCrud({
  97. src: tableConfig[props.tableType].url,
  98. multipleSelectKey: props.multipleKey,
  99. });
  100. const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } =
  101. Methords; //增删改查
  102. const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
  103. const crudRef = ref(null); //crudRef.value 获取avue-crud对象
  104. const commonTableEmits = defineEmits(["selectedSure", "selectMultipleSure"]);
  105. const calculateColumnWidth = (column) => {
  106. if (!column.label) {
  107. return column.minWidth;
  108. }
  109. // 字宽度
  110. const fontSize = 17;
  111. let width = fontSize * (column.label.length + 2);
  112. // 如果开启过滤
  113. if (column.filters) {
  114. width += 12;
  115. }
  116. // 如果开启排序
  117. if (column.sortable) {
  118. width += 24;
  119. }
  120. return width;
  121. };
  122. onMounted(() => {
  123. url.value = tableConfig[props.tableType].url;
  124. option.value = Object.assign(option.value, {
  125. menu: false,
  126. highlightCurrentRow: true,
  127. selection: props.multiple,
  128. addBtn: false,
  129. height: "500",
  130. overHidden: true,
  131. indexWidth: "120px",
  132. column: tableConfig[props.tableType].column,
  133. });
  134. // 列处理
  135. if (option.value.column) {
  136. for (let col of option.value.column) {
  137. col.minWidth = calculateColumnWidth(col);
  138. }
  139. }
  140. // dataList();
  141. });
  142. watch(
  143. () => props.tableType,
  144. () => {
  145. url.value = tableConfig[props.tableType].url;
  146. option.value = Object.assign(option.value, {
  147. menu: false,
  148. highlightCurrentRow: true,
  149. selection: props.multiple,
  150. addBtn: false,
  151. height: "500",
  152. overHidden: true,
  153. column: tableConfig[props.tableType].column,
  154. });
  155. // 列处理
  156. if (option.value.column) {
  157. for (let col of option.value.column) {
  158. col.minWidth = calculateColumnWidth(col);
  159. }
  160. }
  161. //dataList();
  162. }
  163. );
  164. const selectRowValue = ref({});
  165. const rowClick = (row) => {
  166. selectRowValue.value = row;
  167. };
  168. const handleClose = () => {
  169. isShowTable.value = false;
  170. };
  171. const onSelected = () => {
  172. if (props.multiple) {
  173. commonTableEmits("selectMultipleSure", toDeleteIds.value);
  174. } else {
  175. commonTableEmits("selectedSure", selectRowValue.value);
  176. }
  177. isShowTable.value = false;
  178. };
  179. defineExpose({ startSelect, refreshDictData, mergeOption });
  180. </script>