index.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <template>
  2. <el-tooltip :disabled="!showDesc" :content="desc">
  3. <div
  4. :class="[
  5. 'table-header-cell',
  6. 'el-table-v2__header-cell-text',
  7. 'el-table__header-cell-text',
  8. {
  9. 'table-header-cell-space-between':
  10. (filterable && filterStyle === 'arrow') || sortable,
  11. },
  12. ]"
  13. >
  14. <template v-if="filterable && filterStyle === 'underline'">
  15. <span :class="['filter-button-wrapper']">
  16. <span
  17. :class="['filter-button', { 'filter-button-active': active }]"
  18. ref="buttonRef"
  19. v-click-outside="clickOutsideHandler"
  20. >{{ `${showDesc ? '*' : ''}${label}` }}</span
  21. >
  22. </span>
  23. </template>
  24. <template v-else>
  25. <span style="flex: 1">{{ `${showDesc ? '*' : ''}${label}` }}</span>
  26. </template>
  27. <div
  28. v-if="(filterable && filterStyle === 'arrow') || sortable"
  29. class="button-wrapper"
  30. >
  31. <div
  32. v-if="filterable && filterStyle === 'arrow'"
  33. ref="buttonRef"
  34. v-click-outside="clickOutsideHandler"
  35. :class="[
  36. 'filter-arrow',
  37. { 'arrow-active': active, 'arrow-expand': expand },
  38. ]"
  39. >
  40. <el-icon>
  41. <CaretBottom />
  42. </el-icon>
  43. </div>
  44. <div v-if="sortable" class="sort-button" @click="sortChange">
  45. <el-icon
  46. :color="sortRule ? '#2d7cff' : '#000000'"
  47. :size="sortRule ? 16 : ''"
  48. >
  49. <SortUp v-show="sortRule === 'ascending'" />
  50. <SortDown v-show="sortRule === 'descending'" />
  51. <Sort v-show="!sortRule" />
  52. </el-icon>
  53. </div>
  54. </div>
  55. <el-popover
  56. ref="popoverRef"
  57. :virtual-ref="buttonRef"
  58. virtual-triggering
  59. trigger="click"
  60. :width="224"
  61. class="table-header-cell-popover"
  62. placement="bottom"
  63. @show="
  64. () => {
  65. expand = true
  66. }
  67. "
  68. @hide="
  69. () => {
  70. expand = false
  71. }
  72. "
  73. >
  74. <el-button-group size="small" type="default" class="select-buttons">
  75. <el-button @click="selectAll">全选</el-button>
  76. <el-button @click="selectReverse">反选</el-button>
  77. </el-button-group>
  78. <el-select
  79. v-model="selections"
  80. size="default"
  81. placeholder="筛选"
  82. multiple
  83. filterable
  84. default-first-option
  85. collapse-tags
  86. clearable
  87. :teleported="false"
  88. >
  89. <el-option
  90. v-for="(option, index) in groupedOptions"
  91. :key="option.value + index"
  92. :value="option.value"
  93. :label="option.label"
  94. />
  95. </el-select>
  96. </el-popover>
  97. </div>
  98. </el-tooltip>
  99. </template>
  100. <script setup lang="ts">
  101. import { PropType } from 'vue'
  102. import _ from 'lodash'
  103. import { CaretBottom, Sort, SortUp, SortDown } from '@element-plus/icons-vue'
  104. import { ClickOutside as vClickOutside } from 'element-plus'
  105. const props = defineProps({
  106. label: {
  107. type: String,
  108. default: '',
  109. },
  110. desc: {
  111. type: String,
  112. default: ''
  113. },
  114. showDesc: {
  115. type: Boolean,
  116. },
  117. filterStyle: {
  118. type: String as PropType<'arrow' | 'underline'>,
  119. default: 'arrow',
  120. },
  121. filterOptions: {
  122. type: Array as PropType<{ value: string, label: string }[]>,
  123. },
  124. filterValues: {
  125. type: Array<string>,
  126. },
  127. sortable: {
  128. type: Boolean,
  129. },
  130. sortRule: {
  131. type: String,
  132. default: '',
  133. },
  134. })
  135. const emit = defineEmits(['update:filterValues', 'update:sortRule'])
  136. const selections = ref<string[]>([])
  137. const groupedOptions = computed(() => {
  138. if (props.filterOptions) {
  139. const selected = selections.value.map(value => {
  140. const sameOption = props.filterOptions?.find(option => option.value === value)
  141. if (sameOption) {
  142. return sameOption
  143. } else {
  144. return {
  145. value,
  146. label: value
  147. }
  148. }
  149. })
  150. const unselected = props.filterOptions.filter(option => !selections.value.includes(option.value))
  151. return [
  152. ..._.orderBy(selected, o => o.value),
  153. ..._.orderBy(unselected, o => o.value)
  154. ]
  155. } else {
  156. return []
  157. }
  158. })
  159. const expand = ref(false)
  160. const active = computed(() => !!props.filterValues?.length)
  161. const filterable = computed(() => !!props.filterOptions)
  162. watchEffect(() => {
  163. if (props.filterValues) {
  164. selections.value = props.filterValues
  165. }
  166. })
  167. watchEffect(() => {
  168. emit('update:filterValues', selections.value)
  169. })
  170. const selectAll = () => {
  171. selections.value.push(...props.filterOptions!.reduce((pre: string[], { value }) => {
  172. if (!selections.value.includes(value)) {
  173. pre.push(value)
  174. }
  175. return pre
  176. }, []))
  177. }
  178. const selectReverse = () => {
  179. selections.value = props.filterOptions!.reduce((pre: string[], { value }) => {
  180. if (!selections.value.includes(value)) {
  181. pre.push(value)
  182. }
  183. return pre
  184. }, [])
  185. }
  186. const buttonRef = ref()
  187. const popoverRef = ref()
  188. const clickOutsideHandler = () => {
  189. unref(popoverRef).popperRef?.delayHide?.()
  190. }
  191. const sortChange = () => {
  192. const sortRule =
  193. props.sortRule === ''
  194. ? 'ascending'
  195. : props.sortRule === 'ascending'
  196. ? 'descending'
  197. : ''
  198. emit('update:sortRule', sortRule)
  199. }
  200. </script>
  201. <style lang="scss" scoped>
  202. .table-header-cell {
  203. flex: 1;
  204. height: 100%;
  205. display: flex;
  206. justify-content: center;
  207. align-items: center;
  208. text-align: center;
  209. line-height: 14px;
  210. &.table-header-cell-space-between {
  211. justify-content: space-between;
  212. }
  213. .filter-button-wrapper {
  214. flex: 1;
  215. cursor: pointer;
  216. .filter-button {
  217. position: relative;
  218. &::after {
  219. content: '';
  220. display: block;
  221. width: calc(100% + 4px);
  222. position: absolute;
  223. bottom: -4px;
  224. left: -2px;
  225. border-bottom: 1px solid #101116;
  226. }
  227. &:hover {
  228. color: #2d7cff;
  229. }
  230. &-active,
  231. &:hover {
  232. &::after {
  233. border-bottom: 2px solid #2d7cff;
  234. }
  235. }
  236. }
  237. }
  238. .button-wrapper {
  239. width: 14px;
  240. height: 100%;
  241. display: flex;
  242. flex-direction: column;
  243. justify-content: space-around;
  244. align-items: center;
  245. position: relative;
  246. &::after {
  247. content: '';
  248. display: block;
  249. width: 100%;
  250. height: 100%;
  251. position: absolute;
  252. z-index: 0;
  253. background-color: #000000;
  254. opacity: 0.1;
  255. }
  256. > div {
  257. z-index: 1;
  258. flex: 1;
  259. display: flex;
  260. justify-content: center;
  261. align-items: center;
  262. cursor: pointer;
  263. }
  264. .filter-arrow {
  265. transition: 0.3s transform;
  266. &.arrow-expand {
  267. transform: rotate(-180deg);
  268. }
  269. &.arrow-active {
  270. color: #2d7cff;
  271. font-weight: bold;
  272. }
  273. }
  274. }
  275. }
  276. .el-select-dropdown__item.hover {
  277. background: #d2d6df;
  278. }
  279. .select-buttons {
  280. margin-bottom: 10px;
  281. .el-button {
  282. margin-right: 10px;
  283. }
  284. }
  285. .el-table-v2__main .button-wrapper {
  286. position: relative;
  287. z-index: 0;
  288. }
  289. </style>