index.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. let fiterName = item.filterName
  93. return item.filterName?item.filterName :item.name ? `仅显示: ${item.name}` : this.desc
  94. },
  95. isActive(item) {
  96. return item.key === this.$route.query.filter_type
  97. },
  98. toggleItem(item) {
  99. if (this.isActive(item)) {
  100. this.close()
  101. } else {
  102. this.$router.replace({
  103. path: item.url,
  104. query: {
  105. ...this.$route.query,
  106. ...item.query,
  107. filter_type: item.key,
  108. [this.searchKey]: this.initSearch[this.searchKey]
  109. }
  110. })
  111. this.$emit('setTimeForSearch')
  112. // this.$emit('reload', true)
  113. }
  114. },
  115. close() {
  116. const keys = {}
  117. for (const item of [...this.keys, ...this.moreKeys]) {
  118. keys[item] = undefined
  119. }
  120. this.$router.replace({
  121. query: {
  122. ...this.$route.query,
  123. [this.searchKey]: undefined,
  124. filter_type: undefined,
  125. ...keys
  126. }
  127. })
  128. // this.$emit('reload')
  129. }
  130. }
  131. }
  132. </script>
  133. <style lang="less" scoped>
  134. .container{
  135. display: flex;
  136. }
  137. .filter-search{
  138. margin-left: 10px;
  139. display: flex;
  140. align-items: center;
  141. cursor: pointer;
  142. &.active{
  143. color: #e6a23c;
  144. }
  145. >img{
  146. width: 20px;
  147. height: auto;
  148. }
  149. >span{
  150. margin: 0 6px
  151. }
  152. >i{
  153. font-size: 16px;
  154. }
  155. }
  156. ::v-deep .el-tag--plain {
  157. background: transparent;
  158. }
  159. </style>