9d96e91d5fc7775c9b4b07be227af4777c6b6e94.svn-base 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <template>
  2. <div class="standard-table">
  3. <div class="alert">
  4. <a-alert type="info" :show-icon="true">
  5. <div slot="message">
  6. 已选择&nbsp;<a style="font-weight: 600">{{ selectedRows.length }}</a>&nbsp;&nbsp;
  7. <template v-for="(item, index) in needTotalList" v-if="item.needTotal">
  8. {{ item.title }} 总计&nbsp;
  9. <a :key="index" style="font-weight: 600">
  10. {{ item.customRender ? item.customRender(item.total) : item.total }}
  11. </a>&nbsp;&nbsp;
  12. </template>
  13. <a style="margin-left: 24px" @click="onClearSelected">清空</a>
  14. </div>
  15. </a-alert>
  16. </div>
  17. <a-table
  18. :size="size"
  19. :bordered="bordered"
  20. :loading="loading"
  21. :columns="columns"
  22. :dataSource="current"
  23. :rowKey="rowKey"
  24. :pagination="pagination"
  25. :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: updateSelect }"
  26. >
  27. </a-table>
  28. </div>
  29. </template>
  30. <script>
  31. export default {
  32. name: "StandardTable",
  33. // props: ['bordered', 'loading', 'columns', 'data', 'rowKey', 'pagination', 'selectedRows'],
  34. props: {
  35. /**
  36. * 数据加载函数,返回值必须是 Promise
  37. * 默认情况下必须传递 data 参数;
  38. * 如果使用本地数据渲染表格,业务代码中将获取本地数据包装为 Promise 即可。
  39. *
  40. * currentData 用于向外暴露表格当前渲染的数据,
  41. * 业务开发中也可以直接修改 currentData,从而重新渲染表格(仅推荐用于客户端排序、数据过滤等场景)
  42. */
  43. data: {
  44. type: Function,
  45. required: true
  46. },
  47. dataSource: {
  48. type: Array,
  49. default () {
  50. return []
  51. }
  52. },
  53. columns: {
  54. type: Array,
  55. required: true
  56. },
  57. /* pagination: {
  58. type: Object,
  59. default () {
  60. return {}
  61. }
  62. },*/
  63. pageSize: {
  64. type: Number,
  65. default: 10
  66. },
  67. pageNum: {
  68. type: Number,
  69. default: 1
  70. },
  71. pageSizeOptions: {
  72. type: Array,
  73. default () {
  74. return ['10', '20', '30', '40', '50']
  75. }
  76. },
  77. responseParamsName: {
  78. type: Object,
  79. default () {
  80. return {}
  81. }
  82. },
  83. bordered: {
  84. type: Boolean,
  85. default: false
  86. },
  87. /**
  88. * 表格大小风格,default, middle, small
  89. */
  90. size: {
  91. type: String,
  92. default: 'default'
  93. },
  94. rowKey: {
  95. type: String,
  96. default: ''
  97. },
  98. selectedRows: {
  99. type: Array,
  100. default: null
  101. }
  102. },
  103. data () {
  104. return {
  105. needTotalList: [],
  106. selectedRowKeys: [],
  107. loading: true,
  108. total: 0,
  109. pageNumber: this.pageNum,
  110. currentPageSize: this.pageSize,
  111. defaultCurrent: 1,
  112. sortParams: {},
  113. current: [],
  114. pagination: {},
  115. paramsName: {},
  116. }
  117. },
  118. created () {
  119. //数据请求参数配置
  120. this.paramsName = Object.assign(
  121. {},
  122. {
  123. pageNumber: "pageNo",
  124. pageSize: "pageSize",
  125. total: "totalCount",
  126. results: "data",
  127. sortColumns: "sortColumns"
  128. },
  129. this.responseParamsName
  130. );
  131. this.needTotalList = this.initTotalList(this.columns)
  132. // load data
  133. this.loadData( { pageNum: this.pageNumber } )
  134. },
  135. methods: {
  136. updateSelect (selectedRowKeys, selectedRows) {
  137. this.selectedRowKeys = selectedRowKeys
  138. let list = this.needTotalList
  139. this.needTotalList = list.map(item => {
  140. return {
  141. ...item,
  142. total: selectedRows.reduce((sum, val) => {
  143. return sum + val[item.dataIndex]
  144. }, 0)
  145. }
  146. })
  147. this.$emit('change', selectedRowKeys, selectedRows)
  148. },
  149. initTotalList (columns) {
  150. const totalList = []
  151. columns.forEach(column => {
  152. if (column.needTotal) {
  153. totalList.push({ ...column, total: 0 })
  154. }
  155. })
  156. return totalList
  157. },
  158. loadData (params) {
  159. let that = this
  160. that.loading = true
  161. params = Object.assign({}, params)
  162. const remoteParams = Object.assign({}, that.sortParams)
  163. remoteParams[that.paramsName.pageNumber] = params.pageNum || that.pageNumber
  164. remoteParams[that.paramsName.pageSize] = params.pageSize || that.currentPageSize
  165. if (params.pageNum) {
  166. that.pageNumber = params.pageNum
  167. }
  168. if (params.pageSize) {
  169. that.currentPageSize = params.pageSize
  170. }
  171. let dataPromise = that.data(remoteParams)
  172. dataPromise.then( response => {
  173. if (!response) {
  174. that.loading = false
  175. return
  176. }
  177. let results = response[that.paramsName.results]
  178. results = (results instanceof Array && results) || []
  179. that.current = results
  180. that.$emit("update:currentData", that.current.slice())
  181. that.$emit("dataloaded", that.current.slice())
  182. that.total = response[that.paramsName.total] * 1
  183. that.pagination = that.pager()
  184. that.loading = false
  185. }, () => {
  186. // error callback
  187. that.loading = false
  188. })
  189. },
  190. // eslint-disable-next-line
  191. onPagerChange (page, pageSize) {
  192. this.pageNumber = page
  193. this.loadData({ pageNum: page })
  194. },
  195. onPagerSizeChange (current, size) {
  196. this.currentPageSize = size
  197. /*
  198. if (current === this.pageNumber) this.loadData()
  199. console.log('page-size-change', current, size)
  200. */
  201. },
  202. onClearSelected () {
  203. this.selectedRowKeys = []
  204. this.updateSelect([], [])
  205. },
  206. pager () {
  207. return {
  208. total: this.total,
  209. showTotal: total => `共有 ${total} 条`,
  210. showSizeChanger: true,
  211. pageSizeOptions: this.pageSizeOptions,
  212. pageSize: this.pageSize,
  213. defaultCurrent: this.defaultCurrent,
  214. onChange: this.onPagerChange,
  215. onShowSizeChange: this.onPagerSizeChange
  216. }
  217. }
  218. },
  219. watch: {
  220. 'selectedRows': function (selectedRows) {
  221. this.needTotalList = this.needTotalList.map(item => {
  222. return {
  223. ...item,
  224. total: selectedRows.reduce( (sum, val) => {
  225. return sum + val[item.dataIndex]
  226. }, 0)
  227. }
  228. })
  229. }
  230. }
  231. }
  232. </script>
  233. <style scoped>
  234. .alert {
  235. margin-bottom: 16px;
  236. }
  237. </style>