BuyerclassSelectorContent.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <template>
  2. <div class="selector-content" v-if="selectorType === 'card'" key="s-content">
  3. <div class="search-container">
  4. <el-input v-model.trim="searchContent" placeholder="搜索" prefix-icon="el-icon-search"></el-input>
  5. </div>
  6. <div class="select-list scrollbar" ref="selectList">
  7. <div
  8. v-for="(item, key) in cateListMap"
  9. :key="key"
  10. class="select-group-container"
  11. :class="{
  12. global: key === '#'
  13. }"
  14. >
  15. <div class="index-anchor" :id="key" :data-index="key" v-if="key !== '#'">{{ key }}</div>
  16. <div class="select-group">
  17. <button v-for="(cate, i) in item" :key="999-i"
  18. class="j-button-item"
  19. :class="{
  20. active: cate.selected,
  21. [cate.id]: true
  22. }"
  23. @click="changeCateState($event,cate)">{{ cate.name }}
  24. </button>
  25. <button
  26. :class="{
  27. active: notClass.selected,
  28. [notClass.id]: true
  29. }"
  30. class="j-button-item"
  31. v-if="item[0].name === '全部'"
  32. @click="changeCateState($event,notClass)"
  33. >匹配未分类行业</button>
  34. <img class="icon-quesion" src="@/assets/images/icon/question.png" v-if="item[0].name === '全部'" @click="dialogTip = true" />
  35. </div>
  36. </div>
  37. </div>
  38. <el-dialog
  39. custom-class="buyer-dialog"
  40. title="匹配未分类行业"
  41. :visible.sync="dialogTip"
  42. width="24%"
  43. center
  44. :show-close="false"
  45. append-to-body
  46. >
  47. <p style="font-size:14px;color:#1D1D1D;line-height:22px;">每条信息的采购单位行业属性由机器自动识别,会存在少数无法识别进行分类的情况。</p>
  48. <p style="margin-top:4px;font-size:14px;color:#686868;line-height:22px;">注:选择全部行业时,未分类行业默认被选中,关闭无效</p>
  49. <span slot="footer" class="dialog-footer">
  50. <el-button style="background: #2cb7ca;border-radius: 6px;border:0;" type="primary" @click="dialogTip = false">我知道了</el-button>
  51. </span>
  52. </el-dialog>
  53. </div>
  54. <div class="selector-content" :class="{ 'no-more': !showMore }" v-else-if="selectorType === 'line'" key="s-content">
  55. <span class="action-button show-more" @click="showMore = !showMore">
  56. <span class="action-text">{{ showMore ? '收起' : '更多' }}</span>
  57. <span class="el-icon-arrow-down" :class="showMore ? 'rotate180' : ''"></span>
  58. </span>
  59. <div
  60. v-for="(item, key) in cateListMap"
  61. :key="key"
  62. class="select-group-container"
  63. >
  64. <!-- <div class="index-anchor" :id="key" :data-index="key" v-if="key !== '#'">{{ key }}</div> -->
  65. <div class="select-group">
  66. <button v-for="(cate, i) in item" :key="999-i"
  67. class="j-button-item bgc"
  68. :class="{
  69. active: cate.selected,
  70. [cate.id]: true,
  71. all: cate.name === '全部'
  72. }"
  73. @click="changeCateState($event,cate)">{{ cate.name }}</button>
  74. </div>
  75. </div>
  76. </div>
  77. </template>
  78. <script>
  79. import { Input, Dialog, Button } from 'element-ui'
  80. import { cateListMapExp } from '@/assets/js/selector.js'
  81. import { debounce, getRandomString } from '@/utils/'
  82. export default {
  83. name: 'buyerclass-selector-content',
  84. components: {
  85. [Input.name]: Input,
  86. [Dialog.name]: Dialog,
  87. [Button.name]: Button
  88. },
  89. props: {
  90. selectorType: {
  91. type: String,
  92. default: 'card' // card/line
  93. },
  94. initCate: {
  95. type: Array,
  96. default () {
  97. return [] // '财政', '传媒', '城管', '出版广电'
  98. }
  99. }
  100. },
  101. data () {
  102. return {
  103. searchContent: '',
  104. // 原始数据
  105. cateListMapExp,
  106. // 页面中循环的数据
  107. cateListMap: {},
  108. cateExp: {
  109. name: '全部',
  110. selected: false,
  111. level: 0,
  112. id: ''
  113. },
  114. // indexBar数据
  115. indexList: [],
  116. showMore: false,
  117. dialogTip: false,
  118. notClass: {
  119. id: 'bc-ppwfl',
  120. level: 1,
  121. name: '匹配未分类',
  122. selected: true
  123. }
  124. }
  125. },
  126. watch: {
  127. initCate (newVal, oldVal) {
  128. // console.log(...arguments)
  129. this.setCateState(newVal)
  130. },
  131. searchContent: debounce(function (newVal, oldVal) {
  132. const search = newVal
  133. const index = this.getIndexWithString(search)
  134. if (index) {
  135. this.$nextTick(() => {
  136. const wrapper = document.querySelector('.buyerclass-selector.s-card')
  137. this.$refs.selectList.scrollTop = wrapper.querySelector(`[data-index=${index}]`).offsetTop
  138. })
  139. }
  140. }, 300)
  141. },
  142. created () {
  143. this.initCateMap()
  144. this.setCateState(this.initCate)
  145. },
  146. methods: {
  147. // 初始化数据
  148. initCateMap () {
  149. const cateListMap = {}
  150. for (const key in this.cateListMapExp) {
  151. this.indexList.push(key)
  152. const cateArr = []
  153. this.cateListMapExp[key].forEach(cateName => {
  154. const cateExp = JSON.parse(JSON.stringify(this.cateExp))
  155. cateExp.name = cateName
  156. cateExp.id = `bc-${getRandomString(8).toLowerCase()}`
  157. if (cateName !== this.cateExp.name) {
  158. cateExp.level = 1
  159. } else {
  160. cateExp.level = 0
  161. cateExp.selected = true
  162. }
  163. cateArr.push(cateExp)
  164. })
  165. cateListMap[key] = cateArr
  166. }
  167. for (const k in cateListMap) {
  168. this.$set(this.cateListMap, k, cateListMap[k])
  169. }
  170. },
  171. changeCateState (e, item) {
  172. // 循环所有数据,判断并改变状态
  173. switch (item.level) {
  174. case 0: {
  175. this.setCateState()
  176. break
  177. }
  178. case 1: {
  179. this.cateListMap['#'][0].selected = false
  180. item.selected = !item.selected
  181. break
  182. }
  183. default: {
  184. console.log('未知level')
  185. }
  186. }
  187. if (item.level !== 0) {
  188. const allSelected = this.checkAllSelectedState()
  189. if (allSelected.allSelected || allSelected.allNotSelected) {
  190. this.setCateState()
  191. }
  192. }
  193. if (this.selectorType === 'line') {
  194. this.onChange()
  195. }
  196. },
  197. // 检查是否全部选中了/全部不选中
  198. checkAllSelectedState () {
  199. const allSelectedArr = []
  200. for (const key in this.cateListMap) {
  201. // 跳过全部进行筛选
  202. if (key === '#') continue
  203. this.cateListMap[key].forEach(item => {
  204. allSelectedArr.push(item.selected)
  205. })
  206. }
  207. return {
  208. // 找不到false,就说明全部被选中
  209. allSelected: allSelectedArr.indexOf(false) === -1,
  210. // 找不到true,就说明没有一个被选中
  211. allNotSelected: allSelectedArr.indexOf(true) === -1
  212. }
  213. },
  214. /**
  215. * 初始化页面选中状态
  216. * @param { Array | undefined } data 要恢复的数据
  217. */
  218. setCateState (data) {
  219. // 设置全部按钮
  220. if (!data || data.length === 0) {
  221. // 其他全部设置不选中,全部按钮设置选中
  222. for (const key in this.cateListMap) {
  223. this.cateListMap[key].forEach(item => {
  224. item.selected = false
  225. })
  226. }
  227. this.cateListMap['#'][0].selected = true
  228. } else {
  229. this.setCateState()
  230. this.cateListMap['#'][0].selected = false
  231. if (Array.isArray(data)) {
  232. for (const key in this.cateListMap) {
  233. this.cateListMap[key].forEach(function (item) {
  234. if (data.indexOf(item.name) !== -1) {
  235. item.selected = true
  236. }
  237. })
  238. }
  239. } else {
  240. console.error('参数必须为undefined或数组')
  241. }
  242. }
  243. },
  244. // 获取选中的数据
  245. getSelected () {
  246. const data = []
  247. for (const key in this.cateListMap) {
  248. // 跳过全部进行查找
  249. if (key === '#') continue
  250. this.cateListMap[key].forEach(item => {
  251. if (item.selected) {
  252. data.push(item.name)
  253. }
  254. })
  255. }
  256. return data
  257. },
  258. getIndexWithString (s = '') {
  259. if (!s) return
  260. let giveMyIndex = ''
  261. for (const key in this.cateListMapExp) {
  262. const wowFindIt = this.cateListMapExp[key].find(item => {
  263. return item.includes(s)
  264. })
  265. if (wowFindIt) {
  266. giveMyIndex = key
  267. break
  268. }
  269. }
  270. return giveMyIndex
  271. },
  272. onChange () {
  273. const selected = this.getSelected()
  274. this.$emit('onChange', selected)
  275. }
  276. }
  277. }
  278. </script>
  279. <style lang="scss" scoped>
  280. .s-card {
  281. font-size: 14px;
  282. .select-group-container {
  283. border-bottom: 1px solid rgba(0,0,0,.05);
  284. .select-group {
  285. display: flex;
  286. flex-wrap: wrap;
  287. align-items: center;
  288. margin: 0 16px;
  289. }
  290. }
  291. .icon-quesion{
  292. width: 18px;
  293. height: 18px;
  294. margin-left: 2px;
  295. cursor: pointer;
  296. }
  297. }
  298. .s-line {
  299. .selector-content {
  300. position: relative;
  301. display: flex;
  302. flex-wrap: wrap;
  303. width: 100%;
  304. padding-right: 45px;
  305. &.no-more {
  306. height: 38px;
  307. overflow: hidden;
  308. }
  309. }
  310. .select-group-container {
  311. display: flex;
  312. flex-wrap: wrap;
  313. .select-group {
  314. display: flex;
  315. flex-wrap: wrap;
  316. }
  317. }
  318. }
  319. </style>