Field.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <template>
  2. <div class="">
  3. <el-drawer class="custom-drawer-form" title="自定义栏位管理" size="1000px" :visible.sync="drawer" direction="rtl" :before-close="close" destroy-on-close append-to-body>
  4. <el-container class="project-field">
  5. <el-header>
  6. <el-form :inline="true" class="project-field-header-form">
  7. <el-form-item label="字段名称">
  8. <el-input v-model="searchOption.name" size="small" placeholder="请输入字段名称"></el-input>
  9. </el-form-item>
  10. <el-form-item>
  11. <el-button type="" :disabled="!$permission('/workspace/common/customField/check')" size="small" @click="getList">查询</el-button>
  12. <el-button type="primary" size="small" @click="handleAdd" :disabled="!$permission('/workspace/common/customField/add')">新增</el-button>
  13. </el-form-item>
  14. </el-form>
  15. <el-button class="margin-right_20" type="success" size="small" @click="handleImport" :disabled="!$permission('/workspace/common/customField/import')">导入</el-button>
  16. </el-header>
  17. <el-main class="project-field-main">
  18. <el-table v-loading="loading" :data="tableData" border header-row-class-name="custom-table-header">
  19. <el-table-column type="index" label="#" width="55" align="center"></el-table-column>
  20. <el-table-column prop="name" label="字段名称" align="center" show-overflow-tooltip></el-table-column>
  21. <el-table-column label="类型" align="center" show-overflow-tooltip>
  22. <template slot-scope="scope">
  23. <span>{{ typeObj[scope.row.type] }}</span>
  24. </template>
  25. </el-table-column>
  26. <el-table-column label="状态" align="center" show-overflow-tooltip>
  27. <template slot-scope="scope">
  28. <span>{{ statusObj.find(item=>item.value == scope.row.status).label }}</span>
  29. </template>
  30. </el-table-column>
  31. <el-table-column prop="createName" label="创建人" align="center" show-overflow-tooltip></el-table-column>
  32. <el-table-column prop="createTime" label="创建时间" align="center" show-overflow-tooltip>
  33. <template slot-scope="scope">
  34. <span>{{ (scope.row.createTime) }}</span>
  35. </template>
  36. </el-table-column>
  37. <el-table-column prop="description" label="备注" align="center" show-overflow-tooltip></el-table-column>
  38. <el-table-column label="操作" align="center" width="150">
  39. <template slot-scope="scope">
  40. <el-dropdown split-button type="primary" size="small">
  41. <span @click="handleEdit(scope.row)" v-if="$permission('/workspace/common/customField/modify')">编辑</span>
  42. <span v-else :disabled="true">编辑</span>
  43. <el-dropdown-menu slot="dropdown" class="text-align_center">
  44. <el-dropdown-item @click.native="handleEditOption(scope.row)" v-if="[3, 4, 5, 6].indexOf(scope.row.type) !== -1 && $permission('/workspace/common/customField/managementOptions')">选项管理</el-dropdown-item>
  45. <el-dropdown-item v-else-if="[3, 4, 5, 6].indexOf(scope.row.type) !== -1" type="primary" size="small" @click.native="handleEditOption(scope.row)">选项查看</el-dropdown-item>
  46. <el-dropdown-item :disabled="!$permission('/workspace/common/customField/copy')" @click.native="handleCopy(scope.row)">复制</el-dropdown-item>
  47. <el-dropdown-item class="color-red" @click.native="handleDelete(scope.row)" :disabled="!$permission('/workspace/common/customField/delete')" divided>删除</el-dropdown-item>
  48. </el-dropdown-menu>
  49. </el-dropdown>
  50. <!-- <el-button v-else-if="[3, 4, 5, 6].indexOf(scope.row.type) !== -1" type="primary" size="small" @click.native="handleEditOption(scope.row)">选项查看</el-button> -->
  51. </template>
  52. </el-table-column>
  53. </el-table>
  54. </el-main>
  55. <el-footer class="pagination">
  56. <!-- <div class="pagination"> -->
  57. <el-pagination :current-page.sync="queryParams.current" :page-size="queryParams.size" :total="total" @current-change="handleCurrentChange" layout="total, prev, pager, next, jumper" background></el-pagination>
  58. <!-- </div> -->
  59. </el-footer>
  60. </el-container>
  61. </el-drawer>
  62. <el-dialog :title="title" :visible.sync="dialogVisible" width="500px" append-to-body destroy-on-close :before-close="cancel" top="10vh">
  63. <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" label-position="left">
  64. <el-form-item label="字段名称" prop="name">
  65. <el-input v-model="ruleForm.name" placeholder="请输入字段名称"></el-input>
  66. </el-form-item>
  67. <el-form-item label="字段类型" prop="type">
  68. <el-select v-model="ruleForm.type" placeholder="请选择字段类型" class="width_100">
  69. <el-option v-for="(item, key) in typeObj" :key="key" :label="item" :value="parseInt(key)"></el-option>
  70. </el-select>
  71. </el-form-item>
  72. <el-form-item label="字段状态" prop="status">
  73. <el-select v-model="ruleForm.status" placeholder="请选择字段状态" style="width: 100%;">
  74. <el-option v-for="(item, key) in statusObj" :key="key" :label="item.label" :value="item.value"></el-option>
  75. </el-select>
  76. </el-form-item>
  77. <el-form-item label="备注" prop="description">
  78. <el-input v-model="ruleForm.description" placeholder="请输入备注" type="textarea"></el-input>
  79. </el-form-item>
  80. </el-form>
  81. <div slot="footer" class="dialog-footer">
  82. <el-button @click="cancel">取 消</el-button>
  83. <el-button type="primary" @click="submit" :loading="btnLoading">确 定</el-button>
  84. </div>
  85. </el-dialog>
  86. <el-dialog :title="title" :visible.sync="dialogVisible2" :width="optionDialogWidth" append-to-body destroy-on-close :close-on-click-modal="false" :before-close="close2" top="10vh">
  87. <el-container class="project-field-option-box" v-loading="optionLoading">
  88. <el-main>
  89. <template v-if="ruleForm.type === 6">
  90. <el-tree
  91. class="tree-option"
  92. :data="ruleForm.option"
  93. node-key="id"
  94. default-expand-all
  95. :expand-on-click-node="false"
  96. draggable
  97. @node-drop="handleDrop"
  98. >
  99. <div class="custom-tree-node" slot-scope="{ node, data }">
  100. <el-input v-model="data.name" placeholder="请输入内容" size="small" class="r" @focus="lockOption = true" @input="valueChange = true" @blur="submitOption(data)"></el-input>
  101. <div class="o" v-if="$permission('/workspace/common/customField/managementOptions')">
  102. <span class="el-icon-plus" @click="handleAddTreeNode(data)" title="添加"></span>
  103. <span class="el-icon-delete" @click="handleDeleteTreeNode(node, data)" title="删除"></span>
  104. </div>
  105. </div>
  106. </el-tree>
  107. </template>
  108. <template v-else>
  109. <div v-for="(item, index) in ruleForm.option" class="option">
  110. <el-input v-model="item.name" placeholder="请输入内容" size="small" class="t" @focus="lockOption = true" @input="valueChange = true" @blur="submitOption(item)"></el-input>
  111. <div class="o" v-if="$permission('/workspace/common/customField/managementOptions')">
  112. <span class="el-icon-delete" @click="handleDeleteOption(item, index)" title="删除"></span>
  113. </div>
  114. </div>
  115. </template>
  116. </el-main>
  117. <el-footer v-if="$permission('/workspace/common/customField/managementOptions')">
  118. <el-button type="primary" size="small" @click="handleAddOption()" :disabled="lockOption">添加</el-button>
  119. </el-footer>
  120. </el-container>
  121. <div slot="footer" class="dialog-footer">
  122. <el-button @click="close2">关 闭</el-button>
  123. </div>
  124. </el-dialog>
  125. <!-- <import-field-dialog ref="importFieldDialog" @close="getList" />
  126. <copy-field-dialog ref="copyFieldDialog" @close="getList" /> -->
  127. </div>
  128. </template>
  129. <script>
  130. // import ImportFieldDialog from "../dialog/ImportField";
  131. // import CopyFieldDialog from "../dialog/CopyField";
  132. import { findTreeEq } from "@/utils";
  133. export default {
  134. components: {
  135. // ImportFieldDialog,
  136. // CopyFieldDialog
  137. },
  138. data() {
  139. return {
  140. optionDialogWidth: '500px',
  141. lockOption: false,
  142. valueChange: false,
  143. queryParams: {
  144. size: 10,
  145. current: 1,
  146. },
  147. //检索条件
  148. searchOption: {
  149. projectId: 0,
  150. name:'',
  151. },
  152. sort:[
  153. {
  154. "orderBy": "createTime",
  155. "orderType": 1
  156. }
  157. ],
  158. projectId: 0,
  159. typeObj: {
  160. 0: '数字',
  161. 1: '日期',
  162. 2: '文本',
  163. // 3: '下拉框',
  164. 4: '单选',
  165. 5: '多选',
  166. 6: '树',
  167. },
  168. pTypeObj: {
  169. 1: '标引',
  170. 2: '分类'
  171. },
  172. statusObj: [
  173. {
  174. label:'关闭',
  175. value:false
  176. },
  177. {
  178. label:"正常",
  179. value:true
  180. }
  181. ],
  182. drawer: false,
  183. dialogVisible: false,
  184. dialogVisible2: false,
  185. title: '',
  186. total: 0,
  187. ruleForm: {},
  188. rules: {
  189. name: [{ required: true, message: '请输入字段名称', trigger: 'blur' },],
  190. type: [{ required: true, message: '请选择类型', trigger: 'change' },],
  191. status: [{ required: true, message: '请选择状态', trigger: 'change' },],
  192. },
  193. btnLoading: false,
  194. loading: false,
  195. optionLoading: false,
  196. tableData: []
  197. }
  198. },
  199. mounted() {
  200. },
  201. methods: {
  202. handleCurrentChange(val) {
  203. this.queryParams.current = val;
  204. this.getList();
  205. },
  206. cancel() {
  207. this.dialogVisible = false
  208. },
  209. open(id) {
  210. this.projectId = id
  211. this.drawer = true
  212. this.searchOption.projectId = this.projectId
  213. this.getList()
  214. },
  215. close() {
  216. this.drawer = false
  217. this.$emit('close',this.projectId)
  218. },
  219. close2() {
  220. this.lockOption = false
  221. this.dialogVisible2 = false
  222. },
  223. handleAdd() {
  224. this.title = '新增字段'
  225. this.ruleForm = {
  226. projectId: this.projectId,
  227. name: '',
  228. type: 6,
  229. status: true,
  230. description: '',
  231. option: []
  232. }
  233. this.dialogVisible = true
  234. },
  235. handleImport() {
  236. this.$refs.importFieldDialog.open(this.projectId)
  237. },
  238. handleEdit(row) {
  239. this.title = '编辑字段'
  240. this.dialogVisible = true
  241. this.ruleForm = { ...row }
  242. },
  243. handleEditOption(row) {
  244. this.ruleForm = { ...row }
  245. this.title = '选项管理'
  246. this.dialogVisible2 = true
  247. let params = {
  248. customFieldId: this.ruleForm.id,
  249. // type: this.ruleForm.type
  250. }
  251. if (this.ruleForm.type === 6) {
  252. this.optionDialogWidth = '600px'
  253. } else {
  254. this.optionDialogWidth = '500px'
  255. }
  256. this.optionLoading = true
  257. this.$api.queryCustomOption(params).then(response => {
  258. this.$set(this.ruleForm, 'option', response.data.data)
  259. this.optionLoading = false
  260. }).catch(error => {
  261. this.optionLoading = false
  262. })
  263. },
  264. handleDeleteOption(item, index) {
  265. if (!item.id) {
  266. this.ruleForm.option.splice(index, 1)
  267. this.lockOption = false
  268. this.valueChange = false
  269. return false
  270. }
  271. this.optionLoading = true
  272. this.$api.deleteCustomOption({ id: item.id, type: this.ruleForm.type }).then(response => {
  273. this.ruleForm.option.splice(index, 1)
  274. this.$message.success('操作成功')
  275. this.optionLoading = false
  276. this.lockOption = false
  277. this.valueChange = false
  278. }).catch(error => {
  279. this.optionLoading = false
  280. })
  281. },
  282. handleDrop(draggingNode, dropNode, dropType, ev) {
  283. let order = dropNode.data.order
  284. switch (dropType) {
  285. case "before":
  286. draggingNode.data.parentId = dropNode.data.parentId
  287. draggingNode.data.order = order - 1
  288. break
  289. case "after":
  290. draggingNode.data.parentId = dropNode.data.parentId
  291. draggingNode.data.order = order + 1
  292. break
  293. case "inner":
  294. draggingNode.data.parentId = dropNode.data.id
  295. draggingNode.data.order = order + 1
  296. break
  297. }
  298. this.valueChange = true
  299. this.submitOption(draggingNode.data)
  300. },
  301. handleDeleteTreeNode(node, data) {
  302. const parent = node.parent
  303. const children = parent.data.children || parent.data
  304. if (!data.id) {
  305. children.splice(children.findIndex(d => d.$treeNodeId === data.$treeNodeId), 1)
  306. this.lockOption = false
  307. return false
  308. }
  309. this.optionLoading = true
  310. this.$api.deleteCustomOption({ id: data.id, type: this.ruleForm.type }).then(response => {
  311. children.splice(children.findIndex(d => d.id === data.id), 1)
  312. this.$message.success('操作成功')
  313. this.optionLoading = false
  314. this.lockOption = false
  315. }).catch(error => {
  316. this.optionLoading = false
  317. })
  318. },
  319. handleAddTreeNode(data) {
  320. if (this.lockOption) {
  321. return false
  322. }
  323. if (!data.children) {
  324. this.$set(data, 'children', [])
  325. }
  326. const lastIndex = data.children.length - 1
  327. data.children.push({
  328. name: '',
  329. parentId: data.id,
  330. order: lastIndex < 0 ? 0 : data.children[lastIndex].order + 1,
  331. children: [],
  332. })
  333. this.lockOption = true
  334. this.$forceUpdate()
  335. },
  336. submitOption(item) {
  337. if (!this.$permission('/workspace/common/customField/managementOptions')) {
  338. return false
  339. }
  340. if (!item.name) {
  341. this.$message.error('请输入内容')
  342. return false
  343. }
  344. if (!this.valueChange) {
  345. this.lockOption = false
  346. return false
  347. }
  348. let data = {
  349. id: item.id,
  350. name: item.name,
  351. customFieldId: this.ruleForm.id,
  352. type: this.ruleForm.type,
  353. parentId: item.parentId,
  354. order: item.order,
  355. }
  356. this.optionLoading = true
  357. if(!item.id){
  358. this.$api.addCustomOption(data).then(response => {
  359. this.$set(item, 'id', response.data)
  360. this.$message.success('操作成功')
  361. this.optionLoading = false
  362. this.lockOption = false
  363. this.valueChange = false
  364. }).catch(error => {
  365. this.optionLoading = false
  366. })
  367. }else{
  368. this.$api.updateCustomOption(data).then(response => {
  369. // this.$set(item, 'id', response.data)
  370. this.$message.success('操作成功')
  371. this.optionLoading = false
  372. this.lockOption = false
  373. this.valueChange = false
  374. }).catch(error => {
  375. this.optionLoading = false
  376. })
  377. }
  378. },
  379. handleAddOption() {
  380. if (this.ruleForm.type === 6) {
  381. const lastIndex = this.ruleForm.option.length - 1
  382. this.ruleForm.option.push({
  383. name: '',
  384. parentId: 0,
  385. order: lastIndex < 0 ? 0 : this.ruleForm.option[lastIndex].order + 1,
  386. children: [],
  387. })
  388. } else {
  389. this.ruleForm.option.push({
  390. name: ''
  391. })
  392. }
  393. this.lockOption = true
  394. },
  395. getList() {
  396. this.loading = true
  397. var params = {
  398. ...this.queryParams,
  399. searchQuery:this.$commonJS.objectToString(this.searchOption),
  400. orderDTOList: this.sort,
  401. }
  402. this.$api.queryCustomField(params).then(response => {
  403. this.tableData = response.data.data
  404. this.total = response.data.total
  405. this.loading = false
  406. }).catch(error => {
  407. this.loading = false
  408. })
  409. },
  410. validationForm() {
  411. return new Promise((resolve, reject) => {
  412. switch (this.ruleForm.type) {
  413. case 0:
  414. case 1:
  415. case 2:
  416. resolve()
  417. break
  418. case 5:
  419. const optionName = this.ruleForm.option.map(item => item.name)
  420. if (optionName.length === 0) {
  421. reject('请添加选项')
  422. }
  423. if (!optionName.every(item => !!item)) {
  424. reject('选项不能为空')
  425. }
  426. if (this.$_.uniq(optionName).length !== optionName.length) {
  427. reject('选项名称重复')
  428. }
  429. resolve()
  430. break
  431. case 6:
  432. const flag = findTreeEq(this.ruleForm.option, 'name')
  433. if (flag) {
  434. reject(`${flag}`)
  435. }
  436. resolve()
  437. break
  438. }
  439. })
  440. },
  441. submit() {
  442. this.$refs.ruleForm.validate((valid) => {
  443. if (valid) {
  444. this.btnLoading = true
  445. if (this.ruleForm.id) {
  446. this.$api.updateCustomField(this.ruleForm).then(response => {
  447. this.$message.success('编辑成功')
  448. this.btnLoading = false
  449. this.getList()
  450. this.cancel()
  451. }).catch(error => {
  452. this.btnLoading = false
  453. })
  454. } else {
  455. this.$api.addCustomField(this.ruleForm).then(response => {
  456. this.$message.success('新增成功')
  457. this.btnLoading = false
  458. this.getList()
  459. this.cancel()
  460. }).catch(error => {
  461. this.btnLoading = false
  462. })
  463. }
  464. }
  465. })
  466. },
  467. handleCopy(row) {
  468. this.$refs.copyFieldDialog.open(row)
  469. },
  470. handleDelete(row) {
  471. this.handleDeletes([row.id])
  472. },
  473. handleDeletes(ids) {
  474. this.$confirm('确认删除该数据吗?', '提示', {
  475. confirmButtonText: '确定',
  476. cancelButtonText: '取消',
  477. type: 'warning'
  478. }).then(() => {
  479. this.loading = true
  480. this.$api.deleteCustomField(ids).then(response => {
  481. this.$message.success('删除成功')
  482. this.loading = false
  483. this.getList()
  484. }).catch(error => {
  485. this.loading = false
  486. })
  487. })
  488. },
  489. }
  490. }
  491. </script>
  492. <style lang="scss">
  493. .project-field {
  494. // .pagination {
  495. // text-align: center;
  496. // margin: 20px 0;
  497. // }
  498. .project-field-header-form {
  499. margin-left: 20px;
  500. }
  501. .project-field-main {
  502. background: #fff;
  503. padding: 5px 20px;
  504. margin-top: 20px;
  505. }
  506. }
  507. .project-field-option-box {
  508. height: 500px !important;
  509. background: #f5f5f5;
  510. border-radius: 5px;
  511. border: 1px solid #cecece;
  512. overflow: hidden;
  513. .option {
  514. margin-bottom: 10px;
  515. .t {
  516. cursor: pointer;
  517. width: 390px;
  518. }
  519. .o {
  520. cursor: pointer;
  521. width: 20px;
  522. float: right;
  523. text-align: right;
  524. margin-top: 5px;
  525. }
  526. }
  527. .el-footer {
  528. line-height: 50px;
  529. height: 50px !important;
  530. padding: 0 10px !important;
  531. }
  532. .tree-option {
  533. background: #f5f5f5 !important;
  534. .is-expanded, .is-focusable, {
  535. .el-tree-node__content:hover, .el-tree-node__content {
  536. background: #f5f5f5 !important;
  537. }
  538. }
  539. .el-tree-node__content {
  540. height: 40px !important;
  541. }
  542. .custom-tree-node {
  543. width: 100%;
  544. .r {
  545. width: 300px;
  546. }
  547. .o {
  548. cursor: pointer;
  549. width: 40px;
  550. float: right;
  551. text-align: right;
  552. margin-top: 5px;
  553. span:nth-child(1) {
  554. margin-right: 10px;
  555. }
  556. }
  557. }
  558. }
  559. }
  560. </style>