华西海圻ELN前端工程
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
5.4 KiB

  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="el-icon-search" @click="toggleSearch()" />
  6. </el-tooltip>
  7. <el-tooltip class="item" effect="dark" content="刷新" placement="top">
  8. <el-button circle icon="el-icon-refresh" @click="refresh()" />
  9. </el-tooltip>
  10. <el-tooltip class="item" effect="dark" content="显隐列" placement="top" v-if="Object.keys(columns).length > 0">
  11. <el-button circle icon="el-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 circle icon="el-icon-menu" />
  14. <el-dropdown-menu slot="dropdown">
  15. <!-- 全选/反选 按钮 -->
  16. <el-dropdown-item>
  17. <el-checkbox :indeterminate="isIndeterminate" v-model="isChecked" @change="toggleCheckAll"> 列展示 </el-checkbox>
  18. </el-dropdown-item>
  19. <div class="check-line"></div>
  20. <template v-for="(item, key) in columns">
  21. <el-dropdown-item :key="key">
  22. <el-checkbox v-model="item.visible" @change="checkboxChange($event, key)" :label="item.label" />
  23. </el-dropdown-item>
  24. </template>
  25. </el-dropdown-menu>
  26. </el-dropdown>
  27. </el-tooltip>
  28. </el-row>
  29. <el-dialog :title="title" :visible.sync="open" append-to-body>
  30. <el-transfer
  31. :titles="['显示', '隐藏']"
  32. v-model="value"
  33. :data="transferData"
  34. @change="dataChange"
  35. ></el-transfer>
  36. </el-dialog>
  37. </div>
  38. </template>
  39. <script>
  40. export default {
  41. name: "RightToolbar",
  42. data() {
  43. return {
  44. // 显隐数据
  45. value: [],
  46. // 弹出层标题
  47. title: "显示/隐藏",
  48. // 是否显示弹出层
  49. open: false
  50. }
  51. },
  52. props: {
  53. /* 是否显示检索条件 */
  54. showSearch: {
  55. type: Boolean,
  56. default: true
  57. },
  58. /* 显隐列信息(数组格式、对象格式) */
  59. columns: {
  60. type: [Array, Object],
  61. default: () => ({})
  62. },
  63. /* 是否显示检索图标 */
  64. search: {
  65. type: Boolean,
  66. default: true
  67. },
  68. /* 显隐列类型(transfer穿梭框、checkbox复选框) */
  69. showColumnsType: {
  70. type: String,
  71. default: "checkbox"
  72. },
  73. /* 右外边距 */
  74. gutter: {
  75. type: Number,
  76. default: 10
  77. },
  78. },
  79. computed: {
  80. style() {
  81. const ret = {}
  82. if (this.gutter) {
  83. ret.marginRight = `${this.gutter / 2}px`
  84. }
  85. return ret
  86. },
  87. isChecked: {
  88. get() {
  89. return Array.isArray(this.columns) ? this.columns.every((col) => col.visible) : Object.values(this.columns).every((col) => col.visible)
  90. },
  91. set() {}
  92. },
  93. isIndeterminate() {
  94. return Array.isArray(this.columns) ? this.columns.some((col) => col.visible) && !this.isChecked : Object.values(this.columns).some((col) => col.visible) && !this.isChecked
  95. },
  96. transferData() {
  97. if (Array.isArray(this.columns)) {
  98. return this.columns.map((item, index) => ({ key: index, label: item.label }))
  99. } else {
  100. return Object.keys(this.columns).map((key, index) => ({ key: index, label: this.columns[key].label }))
  101. }
  102. }
  103. },
  104. created() {
  105. if (this.showColumnsType == 'transfer') {
  106. // transfer穿梭显隐列初始默认隐藏列
  107. if (Array.isArray(this.columns)) {
  108. for (let item in this.columns) {
  109. if (this.columns[item].visible === false) {
  110. this.value.push(parseInt(item))
  111. }
  112. }
  113. } else {
  114. Object.keys(this.columns).forEach((key, index) => {
  115. if (this.columns[key].visible === false) {
  116. this.value.push(index)
  117. }
  118. })
  119. }
  120. }
  121. },
  122. methods: {
  123. // 搜索
  124. toggleSearch() {
  125. this.$emit("update:showSearch", !this.showSearch)
  126. },
  127. // 刷新
  128. refresh() {
  129. this.$emit("queryTable")
  130. },
  131. // 右侧列表元素变化
  132. dataChange(data) {
  133. if (Array.isArray(this.columns)) {
  134. for (let item in this.columns) {
  135. const key = this.columns[item].key
  136. this.columns[item].visible = !data.includes(key)
  137. }
  138. } else {
  139. Object.keys(this.columns).forEach((key, index) => {
  140. this.columns[key].visible = !data.includes(index)
  141. })
  142. }
  143. },
  144. // 打开显隐列dialog
  145. showColumn() {
  146. this.open = true
  147. },
  148. // 单勾选
  149. checkboxChange(event, key) {
  150. if (Array.isArray(this.columns)) {
  151. this.columns.filter(item => item.key == key)[0].visible = event
  152. } else {
  153. this.columns[key].visible = event
  154. }
  155. },
  156. // 切换全选/反选
  157. toggleCheckAll() {
  158. const newValue = !this.isChecked
  159. if (Array.isArray(this.columns)) {
  160. this.columns.forEach((col) => (col.visible = newValue))
  161. } else {
  162. Object.values(this.columns).forEach((col) => (col.visible = newValue))
  163. }
  164. }
  165. },
  166. }
  167. </script>
  168. <style lang="scss" scoped>
  169. ::v-deep .el-transfer__button {
  170. border-radius: 50%;
  171. padding: 12px;
  172. display: block;
  173. margin-left: 0px;
  174. }
  175. ::v-deep .el-transfer__button:first-child {
  176. margin-bottom: 10px;
  177. }
  178. .check-line {
  179. width: 90%;
  180. height: 1px;
  181. background-color: #ccc;
  182. margin: 3px auto;
  183. }
  184. </style>