index.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <template>
  2. <div class="top-right-btn" :style="style">
  3. <el-row>
  4. <el-tooltip v-if="search" class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top">
  5. <el-button circle icon="Search" @click="toggleSearch()" />
  6. </el-tooltip>
  7. <el-tooltip class="item" effect="dark" content="刷新" placement="top">
  8. <el-button circle icon="Refresh" @click="refresh()" />
  9. </el-tooltip>
  10. <el-tooltip v-if="columns" class="item" effect="dark" content="显示/隐藏列" placement="top">
  11. <div class="show-btn">
  12. <el-popover placement="bottom" trigger="click">
  13. <div class="tree-header">显示/隐藏列</div>
  14. <el-tree
  15. ref="columnRef"
  16. :data="columns"
  17. show-checkbox
  18. node-key="key"
  19. :props="{ label: 'label', children: 'children' } as any"
  20. @check="columnChange"
  21. ></el-tree>
  22. <template #reference>
  23. <el-button circle icon="Menu" />
  24. </template>
  25. </el-popover>
  26. </div>
  27. </el-tooltip>
  28. </el-row>
  29. </div>
  30. </template>
  31. <script setup lang="ts">
  32. import { propTypes } from '@/utils/propTypes';
  33. const props = defineProps({
  34. showSearch: propTypes.bool.def(true),
  35. columns: propTypes.fieldOption,
  36. search: propTypes.bool.def(true),
  37. gutter: propTypes.number.def(10)
  38. });
  39. const columnRef = ref<ElTreeInstance>();
  40. const emits = defineEmits(['update:showSearch', 'queryTable']);
  41. const style = computed(() => {
  42. const ret: any = {};
  43. if (props.gutter) {
  44. ret.marginRight = `${props.gutter / 2}px`;
  45. }
  46. return ret;
  47. });
  48. // 搜索
  49. function toggleSearch() {
  50. emits('update:showSearch', !props.showSearch);
  51. }
  52. // 刷新
  53. function refresh() {
  54. emits('queryTable');
  55. }
  56. // 更改数据列的显示和隐藏
  57. function columnChange(...args: any[]) {
  58. props.columns?.forEach((item) => {
  59. item.visible = args[1].checkedKeys.includes(item.key);
  60. });
  61. }
  62. // 显隐列初始默认隐藏列
  63. onMounted(() => {
  64. props.columns?.forEach((item) => {
  65. if (item.visible) {
  66. columnRef.value?.setChecked(item.key, true, false);
  67. // value.value.push(item.key);
  68. }
  69. });
  70. });
  71. </script>
  72. <style lang="scss" scoped>
  73. :deep(.el-transfer__button) {
  74. border-radius: 50%;
  75. display: block;
  76. margin-left: 0px;
  77. }
  78. :deep(.el-transfer__button:first-child) {
  79. margin-bottom: 10px;
  80. }
  81. .my-el-transfer {
  82. text-align: center;
  83. }
  84. .tree-header {
  85. width: 100%;
  86. line-height: 24px;
  87. text-align: center;
  88. }
  89. .show-btn {
  90. margin-left: 12px;
  91. }
  92. </style>