a1d102791a745f63f4cea5a3cf1118676c2c94d3.svn-base 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. Table 重封装组件说明
  2. ====
  3. 封装说明
  4. ----
  5. > 基础的使用方式与 API 与 [官方版(Table)](https://vuecomponent.github.io/ant-design-vue/components/table-cn/) 本一致,在其基础上,封装了加载数据的方法。
  6. >
  7. > 你无需在你是用表格的页面进行分页逻辑处理,仅需向 Table 组件传递绑定 `:data="Promise"` 对象即可
  8. 例子1
  9. ----
  10. (基础使用)
  11. ```vue
  12. <template>
  13. <s-table
  14. ref="table"
  15. :rowKey="(record) => record.data.id"
  16. size="default"
  17. :columns="columns"
  18. :data="loadData"
  19. >
  20. </s-table>
  21. </template>
  22. <script>
  23. import STable from '@/components/table/'
  24. export default {
  25. components: {
  26. STable
  27. },
  28. data() {
  29. return {
  30. columns: [
  31. {
  32. title: '规则编号',
  33. dataIndex: 'no'
  34. },
  35. {
  36. title: '描述',
  37. dataIndex: 'description'
  38. },
  39. {
  40. title: '服务调用次数',
  41. dataIndex: 'callNo',
  42. sorter: true,
  43. needTotal: true,
  44. customRender: (text) => text + ' 次'
  45. },
  46. {
  47. title: '状态',
  48. dataIndex: 'status',
  49. needTotal: true
  50. },
  51. {
  52. title: '更新时间',
  53. dataIndex: 'updatedAt',
  54. sorter: true
  55. }
  56. ],
  57. // 查询条件参数
  58. queryParam: {},
  59. // 加载数据方法 必须为 Promise 对象
  60. loadData: parameter => {
  61. return this.$http.get('/service', {
  62. params: Object.assign(parameter, this.queryParam)
  63. }).then(res => {
  64. return res.result
  65. })
  66. },
  67. }
  68. }
  69. }
  70. </script>
  71. ```
  72. 例子2
  73. ----
  74. (简单的表格,最后一列是各种操作)
  75. ```vue
  76. <template>
  77. <s-table
  78. ref="table"
  79. size="default"
  80. :columns="columns"
  81. :data="loadData"
  82. >
  83. <span slot="action" slot-scope="text, record">
  84. <a>编辑</a>
  85. <a-divider type="vertical"/>
  86. <a-dropdown>
  87. <a class="ant-dropdown-link">
  88. 更多 <a-icon type="down"/>
  89. </a>
  90. <a-menu slot="overlay">
  91. <a-menu-item>
  92. <a href="javascript:;">1st menu item</a>
  93. </a-menu-item>
  94. <a-menu-item>
  95. <a href="javascript:;">2nd menu item</a>
  96. </a-menu-item>
  97. <a-menu-item>
  98. <a href="javascript:;">3rd menu item</a>
  99. </a-menu-item>
  100. </a-menu>
  101. </a-dropdown>
  102. </span>
  103. </s-table>
  104. </template>
  105. <script>
  106. import STable from '@/components/table/'
  107. export default {
  108. components: {
  109. STable
  110. },
  111. data() {
  112. return {
  113. columns: [
  114. {
  115. title: '规则编号',
  116. dataIndex: 'no'
  117. },
  118. {
  119. title: '描述',
  120. dataIndex: 'description'
  121. },
  122. {
  123. title: '服务调用次数',
  124. dataIndex: 'callNo',
  125. },
  126. {
  127. title: '状态',
  128. dataIndex: 'status',
  129. },
  130. {
  131. title: '更新时间',
  132. dataIndex: 'updatedAt',
  133. },
  134. {
  135. table: '操作',
  136. dataIndex: 'action',
  137. scopedSlots: {customRender: 'action'},
  138. }
  139. ],
  140. // 查询条件参数
  141. queryParam: {},
  142. // 加载数据方法 必须为 Promise 对象
  143. loadData: parameter => {
  144. return this.$http.get('/service', {
  145. params: Object.assign(parameter, this.queryParam)
  146. }).then(res => {
  147. return res.result
  148. })
  149. },
  150. }
  151. },
  152. methods: {
  153. edit(row) {
  154. // axios 发送数据到后端 修改数据成功后
  155. // 调用 refresh() 重新加载列表数据
  156. // 这里 setTimeout 模拟发起请求的网络延迟..
  157. setTimeout(() => {
  158. this.$refs.table.refresh()
  159. }, 1500)
  160. }
  161. }
  162. }
  163. </script>
  164. ```
  165. 内置方法
  166. ----
  167. 通过 `this.$refs.table` 调用
  168. `this.$refs.table.refresh()` 刷新列表 (用户新增/修改数据后,重载列表数据)
  169. > 注意:要调用 `refresh()` 需要给表格组件设定 `ref` 值
  170. 注意事项
  171. ----
  172. > 你可能需要为了与后端提供的接口返回结果一致而去修改以下代码:
  173. (需要注意的是,这里的修改是全局性的,意味着整个项目所有使用该 table 组件都需要遵守这个返回结果定义的字段。)
  174. 修改 `@/components/table/index.js` 第 106 行起
  175. ```javascript
  176. result.then(r => {
  177. this.localPagination = Object.assign({}, this.localPagination, {
  178. current: r.pageNo, // 返回结果中的当前分页数
  179. total: r.totalCount, // 返回结果中的总记录数
  180. showSizeChanger: this.showSizeChanger,
  181. pageSize: (pagination && pagination.pageSize) ||
  182. this.localPagination.pageSize
  183. });
  184. !r.totalCount && ['auto', false].includes(this.showPagination) && (this.localPagination = false)
  185. this.localDataSource = r.data; // 返回结果中的数组数据
  186. this.localLoading = false
  187. });
  188. ```
  189. 返回 JSON 例子:
  190. ```json
  191. {
  192. "message": "",
  193. "result": {
  194. "data": [{
  195. id: 1,
  196. cover: 'https://gw.alipayobjects.com/zos/rmsportal/WdGqmHpayyMjiEhcKoVE.png',
  197. title: 'Alipay',
  198. description: '那是一种内在的东西, 他们到达不了,也无法触及的',
  199. status: 1,
  200. updatedAt: '2018-07-26 00:00:00'
  201. },
  202. {
  203. id: 2,
  204. cover: 'https://gw.alipayobjects.com/zos/rmsportal/zOsKZmFRdUtvpqCImOVY.png',
  205. title: 'Angular',
  206. description: '希望是一个好东西,也许是最好的,好东西是不会消亡的',
  207. status: 1,
  208. updatedAt: '2018-07-26 00:00:00'
  209. },
  210. {
  211. id: 3,
  212. cover: 'https://gw.alipayobjects.com/zos/rmsportal/dURIMkkrRFpPgTuzkwnB.png',
  213. title: 'Ant Design',
  214. description: '城镇中有那么多的酒馆,她却偏偏走进了我的酒馆',
  215. status: 1,
  216. updatedAt: '2018-07-26 00:00:00'
  217. },
  218. {
  219. id: 4,
  220. cover: 'https://gw.alipayobjects.com/zos/rmsportal/sfjbOqnsXXJgNCjCzDBL.png',
  221. title: 'Ant Design Pro',
  222. description: '那时候我只会想自己想要什么,从不想自己拥有什么',
  223. status: 1,
  224. updatedAt: '2018-07-26 00:00:00'
  225. },
  226. {
  227. id: 5,
  228. cover: 'https://gw.alipayobjects.com/zos/rmsportal/siCrBXXhmvTQGWPNLBow.png',
  229. title: 'Bootstrap',
  230. description: '凛冬将至',
  231. status: 1,
  232. updatedAt: '2018-07-26 00:00:00'
  233. },
  234. {
  235. id: 6,
  236. cover: 'https://gw.alipayobjects.com/zos/rmsportal/ComBAopevLwENQdKWiIn.png',
  237. title: 'Vue',
  238. description: '生命就像一盒巧克力,结果往往出人意料',
  239. status: 1,
  240. updatedAt: '2018-07-26 00:00:00'
  241. }
  242. ],
  243. "pageSize": 10,
  244. "pageNo": 0,
  245. "totalPage": 6,
  246. "totalCount": 57
  247. },
  248. "status": 200,
  249. "timestamp": 1534955098193
  250. }
  251. ```
  252. 更新时间
  253. ----
  254. 该文档最后更新于: 2018-10-31 PM 08:15