order-BaseTable.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <template>
  2. <div class="table-wrap" v-loading='loading'>
  3. <div class="table-page-search-wrapper" v-if="condition.length">
  4. <el-form :inline="true" class="demo-form-inline" label-width='80px' size="small">
  5. <el-row :gutter='10'>
  6. <el-col :span='span' v-for='v in condition' :key='v.dataIndex'>
  7. <el-form-item :label="layout === 'include' ? '' : v.title" style="display: flex">
  8. <el-input v-model='queryParam[v.dataIndex]' :placeholder="layout === 'include' ? v.title : '请输入'"
  9. v-if="v.type === 'input'" clearable />
  10. <el-date-picker v-model='queryParam[v.dataIndex]' valueFormat="YYYY-MM-DD"
  11. v-else-if="v.type === 'time-range'" :placeholder="layout === 'include' ? v.title : '请选择'" />
  12. <el-select v-model='queryParam[v.dataIndex]' :placeholder="layout === 'include' ? v.title : '请选择'"
  13. :default-value='v.defaultValue' v-else-if="v.type === 'select'" clearable>
  14. <el-option :value='v1.value' :label="v1.label" v-for='v1 in v.options' :key='v1.value'></el-option>
  15. </el-select>
  16. </el-form-item>
  17. </el-col>
  18. <el-col :span='24 - condition.length % (24 / span) * span'>
  19. <span class="table-page-search-submitButtons">
  20. <el-button type="primary" @click='onQueryClick' size="small">
  21. 查询
  22. </el-button>
  23. <el-button style="margin-left: 8px" @click='reset' size="small">
  24. 重置
  25. </el-button>
  26. <slot name='append-btn'></slot>
  27. </span>
  28. </el-col>
  29. </el-row>
  30. </el-form>
  31. </div>
  32. <el-table :data='tableData' style="margin: 6px 0;" v-bind="$attrs" v-on='$listeners' ref='table'>
  33. <el-table-column type="selection" width="55" v-if="isSelection">
  34. </el-table-column>
  35. <el-table-column v-bind='v' :prop='v.dataIndex' :label='v.title' v-for="(v, i) in columns" :key='i'>
  36. <template slot-scope="scope">
  37. <!-- 格式化函数 -->
  38. <span
  39. v-if="v.scopedSlots && v.scopedSlots.customRender && Object.prototype.toString.call(v.scopedSlots.customRender) === '[object Function]'">
  40. {{ v.scopedSlots.customRender(scope.row[v.dataIndex], scope.row, scope.$index) }}
  41. </span>
  42. <!-- 格式化插槽 -->
  43. <slot v-else-if="v.scopedSlots && v.scopedSlots.customRender" v-bind='scope' :name="v.scopedSlots.customRender">
  44. </slot>
  45. <span v-else>{{ scope.row[v.dataIndex] }}</span>
  46. </template>
  47. </el-table-column>
  48. </el-table>
  49. <el-pagination :layout="paginationSize === 'mini' ? 'prev, pager, next' : 'total, sizes, prev, pager, next, jumper'"
  50. :currentPage.sync='localPagination.currentPage' :pageCount.sync='localPagination.totalPage'
  51. :style="paginationSize === 'mini' ? 'display: flex;justify-content: center;' : 'display: flex;justify-content: flex-end;'"
  52. :page-sizes="[5, 10, 20, 30, 50]" @size-change="queryTable" @current-change="queryTable" v-if='showPagination'>
  53. </el-pagination>
  54. </div>
  55. </template>
  56. <script>
  57. export default {
  58. name: 'BaseTable',
  59. props: {
  60. columns: {
  61. default: () => [],
  62. required: true,
  63. },
  64. condition: {
  65. default: () => [],
  66. required: false
  67. },
  68. queryFunction: {
  69. required: false
  70. },
  71. pagination: {
  72. required: false,
  73. default: () => ({
  74. pageSize: 10,
  75. }),
  76. },
  77. paginationText: {
  78. required: false,
  79. default: () => ({
  80. currentPage: 'currentPage', total: 'total', totalPage: 'totalPage', data: 'data'
  81. }),
  82. },
  83. span: {
  84. required: false,
  85. default: 6
  86. },
  87. paginationSize: {
  88. required: false,
  89. default: 'small'
  90. },
  91. layout: {
  92. required: false,
  93. default: 'normal'
  94. },
  95. // 无需查询的静态数据
  96. injectData: {
  97. required: false,
  98. default: null
  99. },
  100. isSelection: {
  101. required: false,
  102. default: false
  103. },
  104. rowKey: {
  105. required: false,
  106. default: 'id'
  107. },
  108. },
  109. data() {
  110. return {
  111. tableData: [],
  112. queryParam: {}, // 暂存
  113. confirmParam: {}, // 点击查询键后赋值
  114. localPagination: Object.assign({
  115. currentPage: 1,
  116. total: 10
  117. }, this.pagination),
  118. loading: false,
  119. selectedRows: [],
  120. selectedRowKeys: [],
  121. showPagination: true
  122. };
  123. },
  124. async created() {
  125. this.condition.forEach(v => {
  126. v.defaultValue && this.$set(this.queryParam, v.dataIndex, v.defaultValue)
  127. })
  128. if (this.injectData) {
  129. this.tableData = this.injectData
  130. this.showPagination = false
  131. } else {
  132. this.queryTable()
  133. }
  134. },
  135. watch: {
  136. injectData: {
  137. handler(v) {
  138. this.tableData = v
  139. },
  140. deep: true
  141. }
  142. },
  143. methods: {
  144. async submit() {
  145. },
  146. onQueryClick() {
  147. this.confirmParam = JSON.parse(JSON.stringify(this.queryParam))
  148. this.localPagination.currentPage = 1
  149. this.queryTable()
  150. },
  151. async queryTable() {
  152. this.loading = true
  153. try {
  154. const { confirmParam, paginationText, localPagination } = this
  155. const queryParamFilter = {}
  156. Object.keys(confirmParam).forEach(key => confirmParam[key] && (queryParamFilter[key] = confirmParam[key]))
  157. const queryResult = await this.queryFunction(
  158. {
  159. [paginationText.currentPage]: localPagination.currentPage,
  160. query: queryParamFilter
  161. }
  162. )
  163. this.tableData = queryResult[paginationText.data]
  164. this.localPagination = {
  165. currentPage: queryResult[paginationText.currentPage],
  166. total: queryResult[paginationText.total],
  167. totalPage: queryResult[paginationText.totalPage]
  168. }
  169. } finally {
  170. this.loading = false
  171. }
  172. },
  173. reset() {
  174. this.queryParam = {}
  175. this.confirmParam = {}
  176. this.queryTable()
  177. },
  178. /**
  179. * 表格重新加载方法
  180. * 如果参数为 true, 则强制刷新到第一页
  181. * @param Boolean bool
  182. */
  183. refresh(bool = false) {
  184. bool && (this.localPagination.currentPage = 1)
  185. this.queryTable()
  186. },
  187. test(v) {
  188. console.log(v)
  189. }
  190. },
  191. };
  192. </script>
  193. <style lang="scss" scoped></style>