index.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <template>
  2. <div class="container">
  3. <el-tag
  4. class="filter-search"
  5. v-for="item in activeItems"
  6. :key="item.key"
  7. @click="toggleItem(item)"
  8. :effect="isActive(item) ? 'dark' : 'plain'">
  9. {{desced(item)}}
  10. </el-tag>
  11. <!-- <div
  12. class="filter-search"
  13. :class="{active: isActive(item)}" v-for="item in activeItems" :key="item.key"
  14. @click="toggleItem(item)"
  15. >
  16. <span>{{desced(item)}}</span>
  17. </div> -->
  18. </div>
  19. </template>
  20. <script>
  21. import { errorType } from '@/views/main/constant'
  22. import cleanDeep from 'clean-deep'
  23. const typesByUrl = {}
  24. for (const key in errorType) {
  25. if (Object.hasOwnProperty.call(errorType, key)) {
  26. const item = errorType[key];
  27. if (!typesByUrl[item.url]) {
  28. typesByUrl[item.url] = []
  29. }
  30. typesByUrl[item.url].push({
  31. ...item,
  32. key
  33. })
  34. }
  35. }
  36. // console.log(typesByUrl)
  37. export default {
  38. name: 'filter-search',
  39. props: {
  40. desc: {
  41. type: String,
  42. default: '已筛选部分数据'
  43. },
  44. searchKey: {
  45. type: String,
  46. default: 'search'
  47. },
  48. keys: {
  49. type: Array,
  50. default: () => []
  51. },
  52. moreKeys: {
  53. type: Array,
  54. default: () => []
  55. },
  56. },
  57. data() {
  58. return{
  59. initSearch: {}
  60. }
  61. },
  62. computed: {
  63. hasSearch() {
  64. return this.$route.query[this.searchKey]
  65. },
  66. hasForm() {
  67. const keys = {}
  68. for (const item of this.keys) {
  69. keys[item] = this.$route.query[item]
  70. }
  71. return !!Object.keys(cleanDeep(keys)).length
  72. },
  73. show() {
  74. return this.hasSearch || this.hasForm
  75. },
  76. activeItems() {
  77. return typesByUrl[this.$route.path]
  78. }
  79. },
  80. mounted() {
  81. this.initSearch = this.$route.query
  82. },
  83. watch: {
  84. $route() {
  85. this.$emit('setTimeForSearch')
  86. this.$emit('reload', this.show)
  87. },
  88. },
  89. methods: {
  90. desced(item) {
  91. return item.name ? `仅显示: ${item.name}` : this.desc
  92. },
  93. isActive(item) {
  94. return item.key === this.$route.query.filter_type
  95. },
  96. toggleItem(item) {
  97. if (this.isActive(item)) {
  98. this.close()
  99. } else {
  100. this.$router.replace({
  101. path: item.url,
  102. query: {
  103. ...this.$route.query,
  104. ...item.query,
  105. filter_type: item.key,
  106. [this.searchKey]: this.initSearch[this.searchKey]
  107. }
  108. })
  109. this.$emit('setTimeForSearch')
  110. // this.$emit('reload', true)
  111. }
  112. },
  113. close() {
  114. const keys = {}
  115. for (const item of [...this.keys, ...this.moreKeys]) {
  116. keys[item] = undefined
  117. }
  118. this.$router.replace({
  119. query: {
  120. ...this.$route.query,
  121. [this.searchKey]: undefined,
  122. filter_type: undefined,
  123. ...keys
  124. }
  125. })
  126. // this.$emit('reload')
  127. }
  128. }
  129. }
  130. </script>
  131. <style lang="less" scoped>
  132. .container{
  133. display: flex;
  134. }
  135. .filter-search{
  136. margin-left: 10px;
  137. display: flex;
  138. align-items: center;
  139. cursor: pointer;
  140. &.active{
  141. color: #e6a23c;
  142. }
  143. >img{
  144. width: 20px;
  145. height: auto;
  146. }
  147. >span{
  148. margin: 0 6px
  149. }
  150. >i{
  151. font-size: 16px;
  152. }
  153. }
  154. </style>