tool-select-category.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <template>
  2. <div class="module-box link-select">
  3. <div class="module-box__title">
  4. <label class="module-box__label">{{title}}</label>
  5. </div>
  6. <div class="link-select__confirm">
  7. <div class="btn" v-if="selectedCategoryList && selectedCategoryList.length === 0" @click="openDialog">
  8. <span class="iconfont">&#xe685;</span> 添加类别
  9. </div>
  10. <div class="info" v-else>
  11. <div><span>{{selectedCategoryList[0].categoryName}}</span><span v-if="selectedCategoryList.length > 1">+{{selectedCategoryList.length-1}}</span></div>
  12. <div class="operation">
  13. <i class="iconfont" @click="openDialog">&#xe66c;</i>
  14. <i class="iconfont" @click="deleteCategory">&#xe633;</i>
  15. </div>
  16. </div>
  17. </div>
  18. <el-dialog width="500" title="选择类别" :visible.sync="categoryVisible">
  19. <el-select v-if="terminal === 4"
  20. v-model="categoryValue"
  21. multiple
  22. collapse-tags
  23. placeholder="请选择">
  24. <el-option
  25. v-for="item in categoryList"
  26. :key="item.id"
  27. :label="item.categoryName"
  28. :value="item.id">
  29. </el-option>
  30. </el-select>
  31. <el-cascader v-else
  32. ref="cascader"
  33. :options="categoryList" :props="{ multiple: true,label: 'categoryName',value: 'id',children: 'childs' }"
  34. clearable></el-cascader>
  35. <span slot="footer" class="dialog-footer">
  36. <el-button @click="categoryVisible = false">取 消</el-button>
  37. <el-button type="primary" @click="categoryChanged">确 定</el-button>
  38. </span>
  39. </el-dialog>
  40. </div>
  41. </template>
  42. <script>
  43. import api from '@@/components/canvasShow/config/api'
  44. import {sendReqMixin} from '@@/components/canvasShow/config/mixin'
  45. import {checkEmptyChild} from '@@/config/common'
  46. import { mapGetters } from 'vuex'
  47. export default {
  48. name: 'tool-select-category',
  49. mixins: [sendReqMixin],
  50. data () {
  51. return {
  52. props: { multiple: true },
  53. selectName: '',
  54. categoryValue: [],
  55. selectedCategoryList: [],
  56. categoryList: [],
  57. categoryVisible: false
  58. }
  59. },
  60. props: {
  61. title: {
  62. type: String,
  63. default: '链接'
  64. },
  65. category: {
  66. type: Array,
  67. default: () => []
  68. }
  69. },
  70. mounted () {
  71. this.getCategory()
  72. this.selectedCategoryList = this.category
  73. },
  74. computed: {
  75. ...mapGetters([
  76. 'terminal'
  77. ])
  78. },
  79. methods: {
  80. // 获取类别
  81. getCategory () {
  82. var _this = this
  83. let params = {
  84. url: api.getClassify,
  85. method: 'GET'
  86. }
  87. this.sendReq(params, (res) => {
  88. _this.categoryList = res.data
  89. checkEmptyChild(_this.categoryList)
  90. })
  91. },
  92. // 打开添加弹窗
  93. openDialog () {
  94. this.categoryVisible = true
  95. },
  96. // 类别选择
  97. categoryChanged () {
  98. this.categoryVisible = false
  99. if (this.terminal === 4) {
  100. this.selectedCategoryList = this.categoryList.filter((item) => {
  101. for (let i = 0; i < this.categoryValue.length; i++) {
  102. if (this.categoryValue[i] === item.id) {
  103. return true
  104. }
  105. }
  106. })
  107. } else {
  108. let nodesArr = this.$refs['cascader'].getCheckedNodes()
  109. if (nodesArr) {
  110. nodesArr = nodesArr.filter((item) => {
  111. return item.children.length === 0
  112. })
  113. this.selectedCategoryList = []
  114. for (let i = 0; i < nodesArr.length; i++) {
  115. this.selectedCategoryList.push(nodesArr[i].data)
  116. }
  117. }
  118. }
  119. this.$emit('update:category', this.selectedCategoryList)
  120. },
  121. // 删除选择
  122. deleteCategory () {
  123. this.categoryValue = []
  124. this.$emit('update:category', [])
  125. }
  126. }
  127. }
  128. </script>
  129. <style lang="scss" scoped>
  130. .link-select{
  131. &__select{
  132. width: 100%;
  133. }
  134. &__confirm{
  135. margin-top: 10px;
  136. .btn{
  137. text-align: center;
  138. background-color: $mainColor;
  139. color: #fff;
  140. height: 36px;
  141. line-height: 36px;
  142. border-radius: 4px;
  143. cursor: pointer;
  144. }
  145. .info{
  146. height: 36px;
  147. line-height: 36px;
  148. border-radius: 4px;
  149. padding: 0 10px;
  150. border:1px solid $mainColor;
  151. display: flex;
  152. justify-content: space-between;
  153. span{
  154. background-color: #f4f4f5;
  155. border:1px solid #e9e9eb;
  156. color: #909399;
  157. height: 26px;
  158. padding: 0 8px;
  159. line-height: 24px;
  160. border-radius: 4px;
  161. margin: 5px 6px 5px 0px;
  162. display: inline-block;
  163. }
  164. .operation{
  165. float: right;
  166. i{
  167. width: 35px;
  168. display: inline-block;
  169. height: 35px;
  170. line-height: 35px;
  171. text-align: center;
  172. cursor: pointer;
  173. }
  174. }
  175. }
  176. }
  177. }
  178. </style>