index.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <div class="top-right-btn" :style="style">
  3. <el-row>
  4. <el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top" v-if="search">
  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 class="item" effect="dark" content="显隐列" placement="top" v-if="columns">
  11. <el-button circle icon="Menu" @click="showColumn()" v-if="showColumnsType == 'transfer'"/>
  12. <el-dropdown trigger="click" :hide-on-click="false" style="padding-left: 12px" v-if="showColumnsType == 'checkbox'">
  13. <el-button size="mini" circle icon="Menu" />
  14. <template #dropdown>
  15. <el-dropdown-menu>
  16. <template v-for="item in columns" :key="item.key">
  17. <el-dropdown-item>
  18. <el-checkbox :checked="item.visible" @change="checkboxChange($event, item.label)" :label="item.label" />
  19. </el-dropdown-item>
  20. </template>
  21. </el-dropdown-menu>
  22. </template>
  23. </el-dropdown>
  24. </el-tooltip>
  25. </el-row>
  26. <el-dialog :title="title" v-model="open" append-to-body>
  27. <el-transfer
  28. :titles="['显示', '隐藏']"
  29. v-model="value"
  30. :data="columns"
  31. @change="dataChange"
  32. ></el-transfer>
  33. </el-dialog>
  34. </div>
  35. </template>
  36. <script setup>
  37. const props = defineProps({
  38. /* 是否显示检索条件 */
  39. showSearch: {
  40. type: Boolean,
  41. default: true,
  42. },
  43. /* 显隐列信息 */
  44. columns: {
  45. type: Array,
  46. },
  47. /* 是否显示检索图标 */
  48. search: {
  49. type: Boolean,
  50. default: true,
  51. },
  52. /* 显隐列类型(transfer穿梭框、checkbox复选框) */
  53. showColumnsType: {
  54. type: String,
  55. default: "checkbox",
  56. },
  57. /* 右外边距 */
  58. gutter: {
  59. type: Number,
  60. default: 10,
  61. },
  62. })
  63. const emits = defineEmits(['update:showSearch', 'queryTable']);
  64. // 显隐数据
  65. const value = ref([]);
  66. // 弹出层标题
  67. const title = ref("显示/隐藏");
  68. // 是否显示弹出层
  69. const open = ref(false);
  70. const style = computed(() => {
  71. const ret = {};
  72. if (props.gutter) {
  73. ret.marginRight = `${props.gutter / 2}px`;
  74. }
  75. return ret;
  76. });
  77. // 搜索
  78. function toggleSearch() {
  79. emits("update:showSearch", !props.showSearch);
  80. }
  81. // 刷新
  82. function refresh() {
  83. emits("queryTable");
  84. }
  85. // 右侧列表元素变化
  86. function dataChange(data) {
  87. for (let item in props.columns) {
  88. const key = props.columns[item].key;
  89. props.columns[item].visible = !data.includes(key);
  90. }
  91. }
  92. // 打开显隐列dialog
  93. function showColumn() {
  94. open.value = true;
  95. }
  96. if (props.showColumnsType == 'transfer') {
  97. // 显隐列初始默认隐藏列
  98. for (let item in props.columns) {
  99. if (props.columns[item].visible === false) {
  100. value.value.push(parseInt(item));
  101. }
  102. }
  103. }
  104. // 勾选
  105. function checkboxChange(event, label) {
  106. props.columns.filter(item => item.label == label)[0].visible = event;
  107. }
  108. </script>
  109. <style lang='scss' scoped>
  110. :deep(.el-transfer__button) {
  111. border-radius: 50%;
  112. display: block;
  113. margin-left: 0px;
  114. }
  115. :deep(.el-transfer__button:first-child) {
  116. margin-bottom: 10px;
  117. }
  118. :deep(.el-dropdown-menu__item) {
  119. line-height: 30px;
  120. padding: 0 17px;
  121. }
  122. </style>