editDialog.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <template>
  2. <div>
  3. <el-form
  4. :model="menuForm"
  5. :rules="addMenuRules"
  6. ref="addUserForm"
  7. label-width="100px"
  8. class="demo-ruleForm"
  9. >
  10. <el-form-item label="上级目录:" prop="parentId" v-if="activeName !== '1'">
  11. <treeselect
  12. :normalizer="normalizer"
  13. :noChildrenText="'没有子选项'"
  14. :noOptionsText="'没有可选项'"
  15. :noResultsText="'没有匹配的结果'"
  16. :placeholder="'请选择'"
  17. :searchable="true"
  18. v-model="menuForm.parentId"
  19. placeholder="请选择"
  20. :options="menuOptions"
  21. ></treeselect>
  22. </el-form-item>
  23. <el-form-item label="菜单名称:" prop="pname">
  24. <el-input
  25. v-model="menuForm.pname"
  26. placeholder="请输入菜单名称"
  27. ></el-input>
  28. </el-form-item>
  29. <el-form-item label="菜单标识:" prop="pcode">
  30. <el-input
  31. v-model="menuForm.pcode"
  32. placeholder="请输入菜单标识"
  33. ></el-input>
  34. </el-form-item>
  35. <el-form-item label="路由地址:" prop="purl" v-if="activeName !== '1'">
  36. <el-input
  37. v-model="menuForm.purl"
  38. placeholder="请输入路由地址"
  39. ></el-input>
  40. </el-form-item>
  41. <el-form-item label="是否显示:" prop="permissionShow">
  42. <el-select
  43. v-model="menuForm.permissionShow"
  44. placeholder="请选择状态"
  45. style="width: 100%"
  46. >
  47. <el-option label="显示" :value="1"></el-option>
  48. <el-option label="不显示" :value="2"></el-option>
  49. </el-select>
  50. </el-form-item>
  51. <el-form-item label="状态:" prop="pstat">
  52. <el-select
  53. v-model="menuForm.pstat"
  54. placeholder="请选择状态"
  55. style="width: 100%"
  56. >
  57. <el-option label="启用" :value="1"></el-option>
  58. <el-option label="停用" :value="0"></el-option>
  59. </el-select>
  60. </el-form-item>
  61. </el-form>
  62. </div>
  63. </template>
  64. <script>
  65. import Treeselect from "@riophae/vue-treeselect";
  66. import { addMenuFn, updateMenuFn } from "@/api/system";
  67. import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  68. export default {
  69. components: {
  70. Treeselect,
  71. },
  72. props: {
  73. activeName: {
  74. type: String,
  75. },
  76. tableData: {
  77. type: Array,
  78. },
  79. checkId: {
  80. type: Number,
  81. },
  82. title: {
  83. type: String,
  84. },
  85. row: {
  86. type: Object,
  87. },
  88. },
  89. data() {
  90. return {
  91. menuForm: {
  92. parentId: null,
  93. pname: null,
  94. purl: null,
  95. pstat: null,
  96. pcode: null,
  97. permissionShow: null,
  98. },
  99. addMenuRules: {
  100. permissionShow: {
  101. required: true,
  102. message: "请选择",
  103. trigger: "change",
  104. },
  105. pcode: [
  106. { required: true, message: "请填写菜单菜单标识", trigger: "blur" },
  107. ],
  108. pname: [{ required: true, message: "请填写菜单名称", trigger: "blur" }],
  109. purl: [{ required: true, message: "请填写路由地址", trigger: "blur" }],
  110. parentId: [
  111. { required: true, message: "请选择上级目录", trigger: "change" },
  112. ],
  113. pstat: [{ required: true, message: "请选择状态", trigger: "change" }],
  114. },
  115. stateOPtions: [],
  116. menuOptions: [
  117. { permissionId: 0, permissionName: "主类目", children: [] },
  118. ],
  119. };
  120. },
  121. created() {
  122. console.log(this.checkId, "checkid");
  123. this.menuOptions[0].children.push(...this.tableData);
  124. if (this.title === "编辑" && this.activeName === "2") {
  125. this.menuForm = {
  126. parentId: this.row.parentId,
  127. pname: this.row.permissionName,
  128. purl: this.row.permissionUrl,
  129. pstat: this.row.permissionState,
  130. pcode: this.row.permissionCode,
  131. permissionShow: this.row.permissionShow,
  132. };
  133. } else if (this.title === "新增" && this.activeName === "2") {
  134. this.menuForm.parentId = this.checkId;
  135. } else {
  136. this.menuForm.parentId = 0;
  137. }
  138. },
  139. watch: {
  140. row: {
  141. handler(newVal) {
  142. if (newVal) {
  143. console.log(newVal, "newRowValue");
  144. if (this.title === "编辑" && this.activeName === "2") {
  145. this.menuForm = {
  146. parentId: newVal.parentId,
  147. pname: newVal.permissionName,
  148. purl: newVal.permissionUrl,
  149. pstat: newVal.permissionState,
  150. pcode: newVal.permissionCode,
  151. permissionShow: newVal.permissionShow,
  152. };
  153. } else if (this.title === "新增" && this.activeName === "2") {
  154. this.menuForm.parentId = this.checkId;
  155. } else {
  156. this.menuForm.parentId = 0;
  157. }
  158. }
  159. },
  160. immediate: true,
  161. deep: true,
  162. },
  163. checkId(newVal) {
  164. if (newVal !== null && newVal !== undefined) {
  165. console.log(newVal, "checkId");
  166. this.menuForm.parentId = newVal;
  167. }
  168. },
  169. tableData: {
  170. handler(newData) {
  171. this.$nextTick(() => {
  172. console.log("gengxin");
  173. this.setExpandAll(this.defaultExpandAll);
  174. });
  175. },
  176. immediate: true,
  177. deep: true,
  178. },
  179. },
  180. methods: {
  181. setExpandAll(expand) {
  182. const table = this.$refs.table;
  183. if (table) {
  184. this.tableData.forEach((row) => {
  185. table.toggleRowExpansion(row, expand);
  186. if (row.children) {
  187. row.children.forEach((child) => {
  188. table.toggleRowExpansion(child, expand);
  189. });
  190. }
  191. });
  192. }
  193. },
  194. /** 转换菜单数据结构 */
  195. normalizer(node) {
  196. if (node.children && !node.children.length) {
  197. delete node.children;
  198. }
  199. return {
  200. id: node.permissionId,
  201. label: node.permissionName,
  202. children: node.children,
  203. };
  204. },
  205. submit() {
  206. const that = this;
  207. this.$refs["addUserForm"].validate((valid) => {
  208. if (valid) {
  209. if (this.title === "编辑") {
  210. updateMenuFn({
  211. ...this.menuForm,
  212. permissionId: this.row.permissionId,
  213. })
  214. .then((res) => {
  215. this.$message({
  216. type: "success",
  217. message: "编辑成功",
  218. });
  219. this.cancel();
  220. that.$emit("updateList");
  221. })
  222. .catch((error) => {
  223. this.$message({
  224. type: "error",
  225. message: "编辑失败",
  226. });
  227. });
  228. } else if (this.title === "新增") {
  229. addMenuFn({
  230. ...this.menuForm,
  231. purl:
  232. this.menuForm.purl !== null ? this.menuForm.purl : undefined,
  233. ptype: "1",
  234. parentId:
  235. this.activeName === "1" ? undefined : this.menuForm.parentId,
  236. })
  237. .then((res) => {
  238. this.$message({
  239. type: "success",
  240. message: "新增成功",
  241. });
  242. this.cancel();
  243. that.$emit("updateList");
  244. })
  245. .catch((error) => {
  246. this.$message({
  247. type: "error",
  248. message: "新增失败",
  249. });
  250. });
  251. }
  252. } else {
  253. return false;
  254. }
  255. });
  256. },
  257. cancel() {
  258. this.$refs["addUserForm"].resetFields();
  259. this.menuForm = {
  260. parentId: null,
  261. pname: null,
  262. purl: null,
  263. pstat: null,
  264. pcode: null,
  265. permissionShow: null,
  266. };
  267. },
  268. },
  269. };
  270. </script>
  271. <style scoped lang="scss"></style>