LabelConfigAddOrUpdate.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. <template>
  2. <el-dialog
  3. class="bs-dialog-wrap bs-el-dialog"
  4. :append-to-body="true"
  5. :before-close="handleClose"
  6. :title="dataForm.id!==''?'编辑标签':'新增标签'"
  7. :visible.sync="dialogFormVisible"
  8. :width="relVisible?'1100px':'450px'"
  9. >
  10. <el-row>
  11. <el-col :span="relVisible ? 8 : 24">
  12. <el-divider
  13. class="bs-el-divider"
  14. content-position="left"
  15. >
  16. 属性信息
  17. </el-divider>
  18. <el-form
  19. ref="ruleForm"
  20. :model="dataForm"
  21. :rules="rules"
  22. label-position="right"
  23. label-width="90px"
  24. class="form-container"
  25. >
  26. <el-form-item
  27. label="标签名称"
  28. prop="labelName"
  29. >
  30. <el-input
  31. v-model="dataForm.labelName"
  32. class="bs-el-input"
  33. clearable
  34. placeholder="请输入标签名称"
  35. maxlength="200"
  36. />
  37. </el-form-item>
  38. <el-form-item
  39. label="标签类型"
  40. prop="labelType"
  41. >
  42. <el-select
  43. ref="searchSelect"
  44. v-model="dataForm.labelType"
  45. class="bs-el-select"
  46. popper-class="bs-el-select"
  47. allow-create
  48. clearable
  49. filterable
  50. placeholder="请选择或输入标签类型"
  51. @blur="selectBlur"
  52. @input.native="filterData"
  53. >
  54. <el-option
  55. v-for="(item,K) in labelTypeList"
  56. :key="K"
  57. :label="item"
  58. :value="item"
  59. />
  60. </el-select>
  61. </el-form-item>
  62. <el-form-item
  63. label="标签说明"
  64. prop="labelDesc"
  65. >
  66. <el-input
  67. v-model="dataForm.labelDesc"
  68. clearable
  69. placeholder="请输入标签说明"
  70. class="bs-el-input"
  71. type="text"
  72. />
  73. </el-form-item>
  74. </el-form>
  75. <el-divider
  76. class="bs-el-divider"
  77. content-position="left"
  78. >
  79. 关联数据集信息
  80. </el-divider>
  81. <el-form class="form-container">
  82. <el-form-item align="center">
  83. <el-tag effect="plain">
  84. 标签
  85. </el-tag>
  86. <span>—————</span>
  87. <el-button
  88. round
  89. size="mini"
  90. type="primary"
  91. @click="buildRel"
  92. >
  93. 添加关联
  94. </el-button>
  95. </el-form-item>
  96. </el-form>
  97. </el-col>
  98. <el-col
  99. v-if="relVisible"
  100. :span="8"
  101. >
  102. <div>
  103. <el-divider
  104. class="bs-el-divider"
  105. content-position="left"
  106. >
  107. 添加关联
  108. </el-divider>
  109. <div
  110. class="tree-box full-box--position"
  111. style="padding: 0 8px 24px 0"
  112. >
  113. <Tree
  114. ref="tree"
  115. :tree-data="categoryData"
  116. style="height: 300px;overflow: auto"
  117. @handleNodeClick="handleNodeClick"
  118. />
  119. </div>
  120. </div>
  121. </el-col>
  122. <el-col
  123. v-if="relVisible"
  124. :span="8"
  125. >
  126. <el-divider
  127. class="bs-el-divider"
  128. content-position="left"
  129. >
  130. 数据集列表
  131. </el-divider>
  132. <div class="bs-table-box">
  133. <el-table
  134. ref="mytable"
  135. class="bs-el-table"
  136. :data="datasetList"
  137. height="300"
  138. @select="handleSelect"
  139. @select-all="handleSelectionAll"
  140. >
  141. <el-table-column
  142. type="selection"
  143. width="55"
  144. />
  145. <el-table-column
  146. label="数据集名称"
  147. prop="name"
  148. show-overflow-tooltip
  149. />
  150. </el-table>
  151. </div>
  152. </el-col>
  153. </el-row>
  154. <span
  155. slot="footer"
  156. class="dialog-footer"
  157. >
  158. <el-button
  159. class="bs-el-button-default"
  160. @click="cancel"
  161. >
  162. 取消
  163. </el-button>
  164. <el-button
  165. type="primary"
  166. @click="submitForm('ruleForm')"
  167. >
  168. 确定
  169. </el-button>
  170. </span>
  171. </el-dialog>
  172. </template>
  173. <script>
  174. import { pageMixins } from 'packages/js/mixins/page'
  175. import Tree from './Tree'
  176. import { addOrUpdateLabel, checkRepeatLabel, getDataSetIdListByLabelId } from 'packages/js/utils/LabelConfigService'
  177. import { datasetList, getCategoryTree } from 'packages/js/utils/datasetConfigService'
  178. export default {
  179. name: 'LabelConfigAddOrUpdate',
  180. mixins: [pageMixins],
  181. data () {
  182. return {
  183. loading: false,
  184. datasetList: [],
  185. typeId: '',
  186. dataForm: {
  187. id: '',
  188. labelName: '',
  189. labelType: '',
  190. labelDesc: '',
  191. relList: []
  192. },
  193. dialogFormVisible: true,
  194. rules: {
  195. labelName: [
  196. { required: true, message: '标签名称不能为空', trigger: 'blur' },
  197. { validator: this.validateLabelName, trigger: 'blur' }
  198. ],
  199. labelType: [
  200. { required: true, message: '标签类型不能为空', trigger: 'change' }
  201. ]
  202. },
  203. // 分类树数据
  204. categoryData: [],
  205. relVisible: false,
  206. // 标签分类列表
  207. labelTypeList: [],
  208. // 选中的数据集id列表
  209. datasetIdList: []
  210. }
  211. },
  212. components: {
  213. Tree
  214. },
  215. watch: {
  216. 'dataForm.labelType': function (val) {
  217. if (val.length > 20) {
  218. this.dataForm.labelType = val.substring(0, 20)
  219. }
  220. },
  221. // datasetList变化时,根据datasetIdList设置其选中状态
  222. datasetList: {
  223. handler: function (val) {
  224. this.$nextTick(() => {
  225. if (this.$refs.mytable) {
  226. this.$refs.mytable.clearSelection()
  227. }
  228. this.datasetList.forEach((item) => {
  229. if (this.datasetIdList.includes(item.id)) {
  230. this.$refs.mytable.toggleRowSelection(item, true)
  231. }
  232. })
  233. })
  234. },
  235. deep: true
  236. }
  237. },
  238. methods: {
  239. /**
  240. * 初始化
  241. * @param row 标签信息
  242. */
  243. init (row) {
  244. this.dataForm.id = row ? row.id : ''
  245. this.dialogFormVisible = true
  246. if (row) {
  247. this.dataForm.id = row.id
  248. this.dataForm.labelName = row.labelName
  249. this.dataForm.labelType = row.labelType
  250. this.dataForm.labelDesc = row.labelDesc
  251. // 获取选中的数据集id列表
  252. getDataSetIdListByLabelId(row.id).then((list) => {
  253. this.datasetIdList = list
  254. this.buildRel()
  255. })
  256. }
  257. this.$nextTick(() => {
  258. this.getDataList()
  259. })
  260. },
  261. /**
  262. * 获取数据集列表
  263. */
  264. getDataList () {
  265. this.loading = true
  266. const params = {
  267. typeId: this.typeId
  268. }
  269. datasetList(params).then((list) => {
  270. this.datasetList = list
  271. if (!this.relVisible) {
  272. this.loading = false
  273. return
  274. }
  275. this.loading = false
  276. }).catch(() => {
  277. this.loading = false
  278. })
  279. },
  280. /**
  281. * 获取分类树
  282. */
  283. getTreeList () {
  284. getCategoryTree({ type: 'dataset' }).then((categoryTree) => {
  285. this.categoryData = categoryTree
  286. })
  287. },
  288. /**
  289. * 标签名称校验
  290. * @param rule
  291. * @param value
  292. * @param callback
  293. */
  294. validateLabelName (rule, value, callback) {
  295. checkRepeatLabel({ id: this.dataForm.id, labelName: this.dataForm.labelName }).then(repeat => {
  296. if (repeat) {
  297. callback(new Error('标签名称已存在'))
  298. } else {
  299. callback()
  300. }
  301. })
  302. },
  303. /**
  304. * 树节点点击事件
  305. * @param row
  306. * @param value
  307. */
  308. handleNodeClick (row, value) {
  309. this.$nextTick(() => {
  310. this.typeId = row.id
  311. this.getDataList()
  312. })
  313. },
  314. /**
  315. * 选中数据集
  316. * @param selection 选中的数据集列表
  317. * @param row 操作行
  318. */
  319. handleSelect (selection, row) {
  320. // 如row.id存在于datasetIdList中,则将其从datasetIdList中删除
  321. if (this.datasetIdList.includes(row.id)) {
  322. const index = this.datasetIdList.indexOf(row.id)
  323. if (index > -1) {
  324. this.datasetIdList.splice(index, 1)
  325. }
  326. return
  327. }
  328. // 如row.id不存在于datasetIdList中,则将其添加到datasetIdList中
  329. if (!this.datasetIdList.includes(row.id)) {
  330. this.datasetIdList.push(row.id)
  331. }
  332. },
  333. /**
  334. * 数据集全选
  335. * @param selection
  336. */
  337. handleSelectionAll (selection) {
  338. // 选中项为空,将datasetList中所有项从datasetIdList中删除
  339. if (selection.length === 0) {
  340. this.datasetList.forEach((dataset) => {
  341. const index = this.datasetIdList.indexOf(dataset.id)
  342. if (index > -1) {
  343. this.datasetIdList.splice(index, 1)
  344. }
  345. })
  346. return
  347. }
  348. // 选中项不为空,将datasetList中所有项添加到datasetIdList中
  349. if (selection.length > 0) {
  350. this.datasetList.forEach((dataset) => {
  351. if (!this.datasetIdList.includes(dataset.id)) {
  352. this.datasetIdList.push(dataset.id)
  353. }
  354. })
  355. }
  356. },
  357. /**
  358. * 表单关闭
  359. */
  360. handleClose () {
  361. this.$parent.addOrUpdateVisible = false
  362. },
  363. /**
  364. * 取消按钮
  365. */
  366. cancel () {
  367. this.dialogFormVisible = false
  368. this.$nextTick(() => {
  369. this.handleClose()
  370. })
  371. },
  372. /**
  373. * 提交按钮
  374. * @param formName
  375. */
  376. submitForm (formName) {
  377. this.$refs[formName].validate((valid) => {
  378. if (valid) {
  379. this.saveForm(true)
  380. } else {
  381. return false
  382. }
  383. })
  384. },
  385. /**
  386. * 保存标签信息
  387. * @param flag
  388. */
  389. saveForm (flag) {
  390. this.dataForm.relList = []
  391. this.datasetIdList.forEach(id => {
  392. const param = {
  393. datasetId: id,
  394. labelId: this.dataForm.id
  395. }
  396. this.dataForm.relList.push(param)
  397. })
  398. addOrUpdateLabel(this.dataForm).then((r) => {
  399. this.$message.success('保存成功')
  400. this.cancel()
  401. this.$parent.getDataList()
  402. // 更新一下类型
  403. this.$parent.getLabelType()
  404. })
  405. },
  406. /**
  407. * 添加关联按钮
  408. */
  409. buildRel () {
  410. this.relVisible = !this.relVisible
  411. if (this.relVisible) {
  412. this.getTreeList()
  413. this.$nextTick(() => {
  414. this.getDataList()
  415. })
  416. }
  417. },
  418. filterNode (value, data) {
  419. if (!value) return true
  420. return data.name.indexOf(value) !== -1
  421. },
  422. ellipsis (value, len) {
  423. if (!value) return ''
  424. if (value.length > len) {
  425. return value.slice(0, len) + '...'
  426. }
  427. return value
  428. },
  429. selectBlur (e) {
  430. this.dataForm.labelType = e.target.value
  431. },
  432. // 对输入字符串控制
  433. filterData () {
  434. // 此属性得到输入的文字
  435. const str = this.$refs.searchSelect.$data.selectedLabel
  436. // 控制的js
  437. if (str.length > 20) {
  438. this.$refs.searchSelect.$data.selectedLabel = str.substr(0, 20)
  439. }
  440. }
  441. }
  442. }
  443. </script>
  444. <style scoped>
  445. .el-col {
  446. height: 358px;
  447. }
  448. .tree-box {
  449. overflow-x: auto;
  450. }
  451. .form-container{
  452. padding: 0 8px;
  453. }
  454. ::v-deep .el-table{
  455. border: 1px solid transparent !important;
  456. }
  457. ::v-deep .el-table th.el-table__cell.is-leaf, .el-table td.el-table__cell{
  458. border-bottom: 1px solid transparent !important;
  459. }
  460. </style>