ActionColumns.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <div class="action-columns" ref="root">
  3. <a-popover v-model="visible" placement="bottomRight" trigger="click" :get-popup-container="() => $refs.root">
  4. <div slot="title">
  5. <a-checkbox :indeterminate="indeterminate" :checked="checkAll" @change="onCheckAllChange" class="check-all" />列展示
  6. <a-button @click="resetColumns" style="float: right" type="link" size="small">重置</a-button>
  7. </div>
  8. <a-list style="width: 100%" size="small" :key="i" v-for="(col, i) in columns" slot="content">
  9. <a-list-item>
  10. <a-checkbox v-model="col.visible" @change="e => onCheckChange(e, col)"/>
  11. <template v-if="col.title">
  12. {{col.title}}:
  13. </template>
  14. <slot v-else-if="col.slots && col.slots.title" :name="col.slots.title"></slot>
  15. <template slot="actions">
  16. <a-tooltip title="固定在列头" :mouseEnterDelay="0.5" :get-popup-container="() => $refs.root">
  17. <a-icon :class="['left', {active: col.fixed === 'left'}]" @click="fixColumn('left', col)" type="vertical-align-top" />
  18. </a-tooltip>
  19. <a-tooltip title="固定在列尾" :mouseEnterDelay="0.5" :get-popup-container="() => $refs.root">
  20. <a-icon :class="['right', {active: col.fixed === 'right'}]" @click="fixColumn('right', col)" type="vertical-align-bottom" />
  21. </a-tooltip>
  22. <a-tooltip title="添加搜索" :mouseEnterDelay="0.5" :get-popup-container="() => $refs.root">
  23. <a-icon :class="{active: col.searchAble}" @click="setSearch(col)" type="search" />
  24. </a-tooltip>
  25. </template>
  26. </a-list-item>
  27. </a-list>
  28. <a-icon class="action" type="setting" />
  29. </a-popover>
  30. </div>
  31. </template>
  32. <script>
  33. import cloneDeep from 'lodash.clonedeep'
  34. export default {
  35. name: 'ActionColumns',
  36. props: ['columns', 'visibleColumns'],
  37. data() {
  38. return {
  39. visible: false,
  40. indeterminate: false,
  41. checkAll: true,
  42. checkedCounts: this.columns.length,
  43. backColumns: cloneDeep(this.columns)
  44. }
  45. },
  46. watch: {
  47. checkedCounts(val) {
  48. this.checkAll = val === this.columns.length
  49. this.indeterminate = val > 0 && val < this.columns.length
  50. },
  51. columns(newVal, oldVal) {
  52. if (newVal != oldVal) {
  53. this.checkedCounts = newVal.length
  54. this.formatColumns(newVal)
  55. }
  56. }
  57. },
  58. created() {
  59. this.formatColumns(this.columns)
  60. },
  61. methods: {
  62. onCheckChange(e, col) {
  63. if (!col.visible) {
  64. this.checkedCounts -= 1
  65. } else {
  66. this.checkedCounts += 1
  67. }
  68. },
  69. fixColumn(fixed, col) {
  70. if (fixed !== col.fixed) {
  71. this.$set(col, 'fixed', fixed)
  72. } else {
  73. this.$set(col, 'fixed', undefined)
  74. }
  75. },
  76. setSearch(col) {
  77. this.$set(col, 'searchAble', !col.searchAble)
  78. if (!col.searchAble && col.search) {
  79. this.resetSearch(col)
  80. }
  81. },
  82. resetSearch(col) {
  83. // col.search.value = col.dataType === 'boolean' ? false : undefined
  84. col.search.value = undefined
  85. col.search.backup = undefined
  86. },
  87. resetColumns() {
  88. const {columns, backColumns} = this
  89. let counts = columns.length
  90. backColumns.forEach((back, index) => {
  91. const column = columns[index]
  92. column.visible = back.visible === undefined || back.visible
  93. if (!column.visible) {
  94. counts -= 1
  95. }
  96. if (back.fixed !== undefined) {
  97. column.fixed = back.fixed
  98. } else {
  99. this.$set(column, 'fixed', undefined)
  100. }
  101. this.$set(column, 'searchAble', back.searchAble)
  102. // column.searchAble = back.searchAble
  103. this.resetSearch(column)
  104. })
  105. this.checkedCounts = counts
  106. this.visible = false
  107. this.$emit('reset', this.getConditions(columns))
  108. },
  109. onCheckAllChange(e) {
  110. if (e.target.checked) {
  111. this.checkedCounts = this.columns.length
  112. this.columns.forEach(col => col.visible = true)
  113. } else {
  114. this.checkedCounts = 0
  115. this.columns.forEach(col => col.visible = false)
  116. }
  117. },
  118. getConditions(columns) {
  119. const conditions = {}
  120. columns.filter(item => item.search.value !== undefined && item.search.value !== '' && item.search.value !== null)
  121. .forEach(col => {
  122. conditions[col.dataIndex] = col.search.value
  123. })
  124. return conditions
  125. },
  126. formatColumns(columns) {
  127. for (let col of columns) {
  128. if (col.visible === undefined) {
  129. this.$set(col, 'visible', true)
  130. }
  131. if (!col.visible) {
  132. this.checkedCounts -= 1
  133. }
  134. }
  135. }
  136. }
  137. }
  138. </script>
  139. <style scoped lang="less">
  140. .action-columns{
  141. display: inline-block;
  142. .check-all{
  143. margin-right: 8px;
  144. }
  145. .left,.right{
  146. transform: rotate(-90deg);
  147. }
  148. .active{
  149. color: @primary-color;
  150. }
  151. }
  152. </style>