index.vue 3.2 KB

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