index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <template>
  2. <el-card class="box-card">
  3. <byTable
  4. :hideTable="true"
  5. :hidePagination="true"
  6. :source="sourceList.data"
  7. :pagination="sourceList.pagination"
  8. :config="config"
  9. :loading="loading"
  10. :searchConfig="searchConfig"
  11. highlight-current-row
  12. :action-list="[
  13. {
  14. text: '新增',
  15. action: () => clickModal(),
  16. },
  17. ]"
  18. @get-list="getList"
  19. @clickReset="clickReset">
  20. </byTable>
  21. <el-table :data="sourceList.data" row-key="id" default-expand-all>
  22. <el-table-column prop="name" label="分类名称" min-width="180" />
  23. <el-table-column prop="sort" label="排序" align="center" width="100" />
  24. <el-table-column label="操作" align="center" width="180">
  25. <template #default="{ row }">
  26. <div>
  27. <el-button type="primary" @click="addChildNode(row)" text>添加子节点</el-button>
  28. <el-button type="primary" @click="clickUpdate(row)" v-if="row.parentId" text>编辑</el-button>
  29. <el-button type="primary" @click="clickDelete(row)" v-if="row.parentId" text>删除</el-button>
  30. </div>
  31. </template>
  32. </el-table-column>
  33. </el-table>
  34. <el-dialog :title="modalType == 'add' ? '新增分类' : '编辑分类'" v-if="openDialog" v-model="openDialog" width="400">
  35. <byForm :formConfig="formConfig" :formOption="formOption" v-model="formData.data" :rules="rules" ref="submit">
  36. <template #parentId>
  37. <div style="width: 100%">
  38. <el-cascader
  39. v-model="formData.data.parentId"
  40. :options="sourceList.data"
  41. :props="{ checkStrictly: true, value: 'id', label: 'name', emitPath: false }"
  42. clearable
  43. style="width: 100%" />
  44. </div>
  45. </template>
  46. </byForm>
  47. <template #footer>
  48. <el-button @click="openDialog = false" size="large">取 消</el-button>
  49. <el-button type="primary" @click="submitForm()" :disabled="btnDisabled" size="large">确 定</el-button>
  50. </template>
  51. </el-dialog>
  52. </el-card>
  53. </template>
  54. <script setup>
  55. import byTable from "@/components/byTable/index";
  56. import byForm from "@/components/byForm/index";
  57. import { ElMessage, ElMessageBox } from "element-plus";
  58. const { proxy } = getCurrentInstance();
  59. const sourceList = ref({
  60. data: [],
  61. pagination: {
  62. total: 0,
  63. name: "",
  64. },
  65. });
  66. const loading = ref(false);
  67. const searchConfig = computed(() => {
  68. return [
  69. {
  70. type: "input",
  71. prop: "name",
  72. label: "分类名称",
  73. },
  74. ];
  75. });
  76. const config = computed(() => {
  77. return [];
  78. });
  79. const getList = async (req) => {
  80. sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  81. loading.value = true;
  82. proxy.post("/bomClassify/tree", sourceList.value.pagination).then((res) => {
  83. sourceList.value.data = res;
  84. setTimeout(() => {
  85. loading.value = false;
  86. }, 200);
  87. });
  88. };
  89. getList();
  90. const clickReset = () => {
  91. getList({ name: "" });
  92. };
  93. const modalType = ref("add");
  94. const submit = ref(null);
  95. const openDialog = ref(false);
  96. const formOption = reactive({
  97. inline: true,
  98. labelWidth: 100,
  99. itemWidth: 100,
  100. rules: [],
  101. });
  102. const formData = reactive({
  103. data: {},
  104. });
  105. const formConfig = computed(() => {
  106. return [
  107. {
  108. type: "slot",
  109. prop: "parentId",
  110. slotName: "parentId",
  111. label: "上级分类",
  112. },
  113. {
  114. type: "input",
  115. prop: "name",
  116. label: "分类名称",
  117. itemType: "text",
  118. },
  119. {
  120. type: "input",
  121. prop: "code",
  122. label: "分类编码",
  123. itemType: "text",
  124. },
  125. {
  126. type: "number",
  127. prop: "sort",
  128. label: "排序",
  129. precision: 0,
  130. },
  131. {
  132. type: "input",
  133. prop: "remark",
  134. label: "备注",
  135. itemType: "textarea",
  136. },
  137. ];
  138. });
  139. const rules = ref({
  140. parentId: [{ required: true, message: "请选择上级分类", trigger: "change" }],
  141. name: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
  142. code: [{ required: true, message: "请输入分类编码", trigger: "blur" }],
  143. });
  144. const btnDisabled = ref(false);
  145. const clickModal = () => {
  146. modalType.value = "add";
  147. formData.data = {};
  148. btnDisabled.value = false;
  149. openDialog.value = true;
  150. };
  151. const addChildNode = (row) => {
  152. modalType.value = "add";
  153. formData.data = {
  154. parentId: row.id,
  155. };
  156. btnDisabled.value = false;
  157. openDialog.value = true;
  158. };
  159. const submitForm = () => {
  160. submit.value.handleSubmit(() => {
  161. btnDisabled.value = true;
  162. proxy.post("/bomClassify/" + modalType.value, formData.data).then(
  163. () => {
  164. ElMessage({
  165. message: modalType.value == "add" ? "添加成功" : "编辑成功",
  166. type: "success",
  167. });
  168. openDialog.value = false;
  169. btnDisabled.value = false;
  170. getList();
  171. },
  172. (err) => {
  173. console.log(err);
  174. btnDisabled.value = false;
  175. }
  176. );
  177. });
  178. };
  179. const clickUpdate = (row) => {
  180. modalType.value = "edit";
  181. formData.data = {
  182. id: row.id,
  183. parentId: row.parentId,
  184. name: row.name,
  185. code: row.code,
  186. sort: row.sort,
  187. remark: row.remark,
  188. };
  189. btnDisabled.value = false;
  190. openDialog.value = true;
  191. };
  192. const clickDelete = (row) => {
  193. ElMessageBox.confirm("你是否确认此操作", "提示", {
  194. confirmButtonText: "确定",
  195. cancelButtonText: "取消",
  196. type: "warning",
  197. })
  198. .then(() => {
  199. proxy.post("/bomClassify/delete", { id: row.id }).then(() => {
  200. ElMessage({ message: "删除成功", type: "success" });
  201. getList();
  202. });
  203. })
  204. .catch(() => {});
  205. };
  206. </script>
  207. <style lang="scss" scoped></style>