index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <template>
  2. <div class="v-table">
  3. <a-spin :spinning="localLoading" tip="Loading...">
  4. <ve-table
  5. ref="tableRef"
  6. style="width:100%"
  7. :max-height="scroll.y"
  8. :scroll-width="scroll.x"
  9. :columns="veColumns"
  10. :table-data="localDataSource"
  11. :row-key-field-name="rowKeyName"
  12. :border-x="true"
  13. :border-y="bordered"
  14. :border-around="bordered"
  15. :column-width-resize-option="columnWidthResizeOption"
  16. :columnHiddenOption="columnHiddenOption"
  17. :row-style-option="{clickHighlight: true}"
  18. :cellSelectionOption="{enable: false}"
  19. :virtual-scroll-option="{enable: true}"
  20. :sort-option="sortOption"
  21. />
  22. <div v-show="localDataSource.length==0" :style="{height:scroll.y+'px'}" class="empty-data"><a-empty description="暂无数据" :image="simpleImage"/></div>
  23. <div class="ve-pagination" :class="'ve-pagination-align-'+pageAlign">
  24. <div class="ve-page-left" v-if="pageAlign=='left'"><slot name="page"></slot></div>
  25. <a-pagination
  26. v-show="showPagination"
  27. size="small"
  28. :total="localPagination.total"
  29. v-model="localPagination.current"
  30. :pageSize="localPagination.pageSize"
  31. :showTotal="total => `共 ${total} 条记录`"
  32. :pageSizeOptions="localPagination.pageSizeOptions"
  33. :show-quick-jumper="localPagination.showQuickJumper"
  34. @change="paginationChange"
  35. show-size-changer
  36. @showSizeChange="paginationShowSizeChange"
  37. />
  38. <div class="ve-page-right" v-if="pageAlign=='right'"><slot name="page"></slot></div>
  39. </div>
  40. </a-spin>
  41. </div>
  42. </template>
  43. <script>
  44. import { Empty } from 'ant-design-vue'
  45. export default {
  46. name: 'VTable',
  47. props: {
  48. // 列
  49. columns: {
  50. type: Array,
  51. default: () => []
  52. },
  53. // load data function
  54. data: {
  55. type: Function,
  56. required: true
  57. },
  58. // 默认是否加载表格数据
  59. defaultLoadData: {
  60. type: Boolean,
  61. default: true
  62. },
  63. // 表格宽度和高度
  64. scroll: {
  65. type: Object,
  66. default: () => ({ x: 0, y: 400 })
  67. },
  68. // 是否显示边框
  69. bordered: {
  70. type: Boolean,
  71. default: true
  72. },
  73. // 表格默认key name
  74. rowKeyName: {
  75. type: String,
  76. default: 'id'
  77. },
  78. // 默认隐藏的列,传 filed 值
  79. defaultHiddenColumnKeys: {
  80. type: Array,
  81. default: () => []
  82. },
  83. // 分页配置
  84. pagination: {
  85. type: Object
  86. },
  87. // 是否显示分页
  88. showPagination: {
  89. type: Boolean,
  90. default: true
  91. },
  92. // 分页对齐方式,left ,center,right
  93. pageAlign: {
  94. type: String,
  95. default: 'center'
  96. }
  97. },
  98. computed: {
  99. veColumns () {
  100. return this.convertColumns(this.columns)
  101. }
  102. },
  103. data () {
  104. return {
  105. localLoading: false,
  106. simpleImage: Empty.PRESENTED_IMAGE_SIMPLE,
  107. localDataSource: [],
  108. sortObj: null,
  109. isSucceed: true, // 是否请求成功
  110. // 拖到列宽
  111. columnWidthResizeOption: {
  112. // default false
  113. enable: true,
  114. // column resize min width
  115. minWidth: 30,
  116. // column size change
  117. sizeChange: ({ column, differWidth, columnWidth }) => {}
  118. },
  119. columnHiddenOption: {
  120. // default hidden column keys
  121. defaultHiddenColumnKeys: this.defaultHiddenColumnKeys
  122. },
  123. // 排序
  124. sortOption: {
  125. // sort always
  126. sortAlways: false,
  127. sortChange: (params) => {
  128. this.sortChange(params)
  129. }
  130. },
  131. // 分页
  132. localPagination: Object.assign({
  133. current: 1,
  134. pageSize: 10,
  135. showSizeChanger: true,
  136. showQuickJumper: false
  137. }, this.pagination)
  138. }
  139. },
  140. created () {
  141. if (this.defaultLoadData) {
  142. this.loadData()
  143. }
  144. },
  145. methods: {
  146. // 转换colums数据结构,兼容老页面
  147. convertColumns (columns) {
  148. const ret = []
  149. const _this = this
  150. for (let i = 0; i < columns.length; i++) {
  151. const item = columns[i]
  152. ret.push({
  153. field: item.field || item.dataIndex,
  154. key: item.key || 'col-' + i,
  155. title: item.title,
  156. // 自定义头部单元格表头
  157. renderHeaderCell: ({ column }, h) => {
  158. if (item.slots) {
  159. return _this.$scopedSlots[item.slots.title]()
  160. }
  161. if (item.align != 'center') {
  162. return (
  163. <div style="text-align:center;">{column.title}</div>
  164. )
  165. }
  166. return h('span', column.title)
  167. },
  168. width: item.width.indexOf('%') >= 0 ? item.width.replace('%', '') * 12 : item.width, // 百分比转数字
  169. align: item.align,
  170. fixed: item.fixed,
  171. sortBy: item.sortBy || item.sorter ? '' : undefined,
  172. children: item.children ? _this.convertColumns(item.children) : undefined,
  173. // 自定义单元格
  174. scopedSlotsKey: item.scopedSlots && item.scopedSlots.customRender,
  175. renderBodyCell: ({ row, column, rowIndex }, h) => {
  176. const text = row[column.field]
  177. if (column.scopedSlotsKey) {
  178. return _this.$scopedSlots[column.scopedSlotsKey](text, row, rowIndex, column)
  179. }
  180. if (item.ellipsis && column.field) {
  181. return (
  182. <a-tooltip placement="right">
  183. <template slot="title">
  184. <span>{text}</span>
  185. </template>
  186. <span style="width:100%;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">{text}</span>
  187. </a-tooltip>
  188. )
  189. }
  190. return item.customRender ? h('span', item.customRender(text)) : h('span', text || '--')
  191. }
  192. })
  193. }
  194. return ret
  195. },
  196. // hidden columns
  197. hideColumns (keys) {
  198. this.$refs['tableRef'].hideColumnsByKeys(keys)
  199. },
  200. // show cloumns
  201. showColumns (keys) {
  202. this.$refs['tableRef'].showColumnsByKeys(keys)
  203. },
  204. /**
  205. * 加载数据方法
  206. * @param {Object} pagination 分页选项器
  207. * @param {Object} filters 过滤条件
  208. * @param {Object} sorter 排序条件
  209. */
  210. loadData (pagination, filters, sorter) {
  211. this.localLoading = true
  212. const parameter = Object.assign({
  213. pageNo: this.showPagination && this.localPagination.current || 1,
  214. pageSize: this.showPagination && this.localPagination.pageSize || 10
  215. },
  216. this.sortObj && { sortField: this.sortObj.field, sortOrder: this.sortObj.order } || {}
  217. )
  218. const result = this.data(parameter)
  219. // 对接自己的通用数据接口需要修改下方代码中的 r.pageNo, r.count, r.data
  220. if ((typeof result === 'object' || typeof result === 'function') && typeof result.then === 'function') {
  221. result.then(r => {
  222. const list = r.list || r.data || r || []
  223. this.isSucceed = !!r
  224. this.localPagination = this.showPagination && Object.assign({}, this.localPagination, {
  225. current: r.pageNo || 1, // 返回结果中的当前分页数
  226. total: Number(r.count) || 0, // 返回结果中的总记录数
  227. pageSize: r.pageSize || this.localPagination.pageSize
  228. }) || false
  229. // 为防止删除数据后导致页面当前页面数据长度为 0 ,自动翻页到上一页
  230. if (list.length === 0 && this.showPagination && this.localPagination.current > 1) {
  231. this.localPagination.current--
  232. this.loadData()
  233. return
  234. }
  235. // 这里用于判断接口是否有返回 r.count 且 this.showPagination = true 且 pageNo 和 pageSize 存在 且 count 小于等于 pageNo * pageSize 的大小
  236. // 当情况满足时,表示数据不满足分页大小,关闭 table 分页功能
  237. try {
  238. if ((['auto', true].includes(this.showPagination) && r.count <= (r.pageNo * this.localPagination.pageSize))) {
  239. // this.localPagination.hideOnSinglePage = true
  240. }
  241. } catch (e) {
  242. this.localPagination = false
  243. }
  244. this.localDataSource = list // 返回结果中的数组数据
  245. this.localLoading = false
  246. }).catch(err => {
  247. this.clearTable()
  248. })
  249. }
  250. },
  251. // 页码变更
  252. paginationChange (pageNumber) {
  253. this.localPagination.current = pageNumber
  254. this.loadData()
  255. },
  256. paginationShowSizeChange (current, pageSize) {
  257. this.localPagination.current = current
  258. this.localPagination.pageSize = pageSize
  259. this.loadData()
  260. },
  261. // 排序
  262. sortChange (params) {
  263. this.sortObj = null
  264. for (const a in params) {
  265. if (params[a]) {
  266. this.sortObj = {
  267. field: a,
  268. order: params[a] + 'end'
  269. }
  270. }
  271. }
  272. this.loadData()
  273. },
  274. /**
  275. * 表格重新加载方法
  276. * 如果参数为 true, 则强制刷新到第一页
  277. * @param Boolean bool
  278. */
  279. refresh (bool = false) {
  280. bool && (this.localPagination = Object.assign({}, {
  281. current: 1, pageSize: this.localPagination.pageSize
  282. }))
  283. this.loadData()
  284. },
  285. // 重置表格为空
  286. clearTable () {
  287. this.localLoading = false
  288. this.localDataSource = []
  289. this.sortObj = null
  290. this.clearSelected()
  291. this.localPagination = Object.assign({}, {
  292. current: 1, pageSize: this.localPagination.pageSize, total: 0
  293. })
  294. },
  295. /**
  296. * 清空 table 已选中项
  297. */
  298. clearSelected () {
  299. if (this.rowSelection) {
  300. this.selectedRows = []
  301. this.selectedRowKeys = []
  302. this.updateSelect()
  303. }
  304. }
  305. }
  306. }
  307. </script>
  308. <style lang="less" scoped>
  309. .v-table {
  310. width: 100%;
  311. > div{
  312. height: 100%;
  313. }
  314. .ant-spin-container{
  315. height: 100%;
  316. }
  317. .empty-data{
  318. color: #999;
  319. text-align: center;
  320. position: absolute;
  321. bottom: 0;
  322. width: 100%;
  323. display: flex;
  324. align-items: center;
  325. flex-direction: column;
  326. justify-content: center;
  327. }
  328. .ve-pagination{
  329. display:flex;
  330. align-items: center;
  331. padding: 20px 0 10px 0;
  332. }
  333. .ve-pagination-align-right{
  334. justify-content: flex-end;
  335. }
  336. .ve-pagination-align-left{
  337. justify-content: flex-start;
  338. }
  339. .ve-pagination-align-center{
  340. justify-content: center;
  341. }
  342. /deep/ .ve-table .ve-table-container .ve-table-content-wrapper table.ve-table-content tbody.ve-table-body tr.ve-table-body-tr td.ve-table-body-td,
  343. .ve-table .ve-table-container .ve-table-content-wrapper table.ve-table-content tbody.ve-table-body tr.ve-table-expand-tr td.ve-table-body-td{
  344. div:empty::before {
  345. content:'--'
  346. }
  347. }
  348. }
  349. </style>