当前位置:   article > 正文

Vue使用vxe-table_vxetable文档

vxetable文档

官网:https://vxetable.cn/v3/#/table/start/use

vxe-table是对element-ui进行了二次封装,基本样式也做了调整,并且把导出、打印等功能也集成进去了,就是纯前端的打印、导出,我之前也有写过前端实现打印、导出的博文,大家感兴趣也可看一下:Vue打印功能 ,纯前端导出Excel并修改样式

这篇就介绍一下vxe-table的基本使用,其实和el-table差不多,使用起来基本一样

安装 

vxe-table依赖xe-utils库,我们这里选择按需导入,减小打包体积 

  1. pnpm add xe-utils vxe-table@legacy
  2. pnpm add babel-plugin-import -D

修改文件 .babelrc 或 babel.config.js 

  1. {
  2. "plugins": [
  3. [
  4. "import",
  5. {
  6. "libraryName": "vxe-table",
  7. "style": true // 样式是否也按需加载
  8. }
  9. ]
  10. ]
  11. }

按需引入 

我们可以在src下新建plugn文件夹,下面的vxe-table.js按需引入vxe-table 

plugin/vxe-table.js 

  1. import Vue from 'vue'
  2. import XEUtils from 'xe-utils'
  3. // 按需导入 vxeTable
  4. import {
  5. // 核心
  6. VXETable,
  7. // 功能模块
  8. Icon,
  9. Filter,
  10. Menu,
  11. Edit,
  12. Export,
  13. Keyboard,
  14. Validator,
  15. Header,
  16. Footer,
  17. // 可选组件
  18. Column,
  19. Colgroup,
  20. Grid,
  21. Toolbar,
  22. Pager,
  23. Checkbox,
  24. CheckboxGroup,
  25. Radio,
  26. RadioGroup,
  27. RadioButton,
  28. Input,
  29. Textarea,
  30. Button,
  31. Modal,
  32. Tooltip,
  33. Form,
  34. FormItem,
  35. FormGather,
  36. Select,
  37. Optgroup,
  38. Option,
  39. Switch,
  40. List,
  41. Pulldown,
  42. // 表格
  43. Table
  44. } from 'vxe-table'
  45. import zhCN from 'vxe-table/lib/locale/lang/zh-CN'
  46. // 按需加载的方式默认是不带国际化的,自定义国际化需要自行解析占位符 '{0}',例如:
  47. VXETable.setup({
  48. i18n: (key, args) => XEUtils.toFormatString(XEUtils.get(zhCN, key), args)
  49. })
  50. // 表格功能
  51. // Vue.use(Filter)
  52. // .use(Edit)
  53. // .use(Menu)
  54. // .use(Export)
  55. // .use(Keyboard)
  56. // .use(Validator)
  57. // 可选组件
  58. Vue.use(Header)
  59. .use(Footer)
  60. .use(Icon)
  61. .use(Filter)
  62. .use(Edit)
  63. .use(Menu)
  64. .use(Export)
  65. .use(Keyboard)
  66. .use(Validator)
  67. // 可选组件
  68. .use(Column)
  69. .use(Colgroup)
  70. .use(Grid)
  71. .use(Toolbar)
  72. .use(Pager)
  73. .use(Checkbox)
  74. .use(CheckboxGroup)
  75. .use(Radio)
  76. .use(RadioGroup)
  77. .use(RadioButton)
  78. .use(Input)
  79. .use(Textarea)
  80. .use(Button)
  81. .use(Modal)
  82. .use(Tooltip)
  83. .use(Form)
  84. .use(FormItem)
  85. .use(FormGather)
  86. .use(Select)
  87. .use(Optgroup)
  88. .use(Option)
  89. .use(Switch)
  90. .use(List)
  91. .use(Pulldown)
  92. // 安装表格
  93. .use(Table)
  94. // 给 vue 实例挂载内部对象,例如:
  95. // Vue.prototype.$XModal = VXETable.modal
  96. // Vue.prototype.$XPrint = VXETable.print
  97. // Vue.prototype.$XSaveFile = VXETable.saveFile
  98. // Vue.prototype.$XReadFile = VXETable.readFile

然后main.js引入该文件 

import './plugin/vxe-table'

使用 

  1. <template>
  2. <div class="vxe-table">
  3. <vxe-toolbar :refresh="{ query: refreshList }" export print custom>
  4. <template #buttons>
  5. <el-button
  6. type="primary"
  7. size="small"
  8. icon="el-icon-plus"
  9. @click="add()"
  10. >新建</el-button
  11. >
  12. <el-button
  13. type="danger"
  14. size="small"
  15. icon="el-icon-delete"
  16. @click="del()"
  17. :disabled="$refs.VXEForm && $refs.VXEForm.getCheckboxRecords().length === 0"
  18. plain
  19. >删除</el-button
  20. >
  21. <el-button
  22. type="danger"
  23. size="small"
  24. icon="el-icon-present"
  25. @click="resetSearch()"
  26. plain
  27. >重置</el-button
  28. >
  29. </template>
  30. </vxe-toolbar>
  31. <div style="height: calc(100% - 100px);">
  32. <vxe-table
  33. border="inner"
  34. auto-resize
  35. resizable
  36. :height="'auto'"
  37. :loading="loading"
  38. size="small"
  39. ref="VXETable"
  40. show-header-overflow
  41. show-overflow
  42. highlight-hover-row
  43. :menu-config="{}"
  44. :print-config="{}"
  45. :import-config="{}"
  46. :export-config="{}"
  47. @sort-change="sortChangeHandle"
  48. :sort-config="{ remote: true }"
  49. :expand-config="{ accordion: true, lazy: true }"
  50. :data="dataList"
  51. :checkbox-config="{}"
  52. >
  53. <vxe-column type="seq" width="40"></vxe-column>
  54. <vxe-column type="checkbox" width="40px"></vxe-column>
  55. <vxe-column field="contractName" title="合同名称"></vxe-column>
  56. <vxe-column field="company" title="单位名称"></vxe-column>
  57. <vxe-column field="bankName" title="开户行"></vxe-column>
  58. <vxe-column field="cardNumber" title="银行账户"></vxe-column>
  59. <vxe-column field="payAmount" title="付款金额"></vxe-column>
  60. <vxe-column field="payType" title="支付方式"></vxe-column>
  61. <vxe-column field="payDate" title="付款日期"></vxe-column>
  62. <vxe-column field="reason" title="付款原因"></vxe-column>
  63. <vxe-column field="remarks" title="付款备注"></vxe-column>
  64. <vxe-column
  65. fixed="right"
  66. align="center"
  67. width="200"
  68. title="操作"
  69. >
  70. <template slot-scope="scope">
  71. <el-button
  72. type="text"
  73. icon="el-icon-view"
  74. size="small"
  75. @click="view(scope.row)"
  76. >查看</el-button
  77. >
  78. <el-button
  79. type="text"
  80. icon="el-icon-view"
  81. size="small"
  82. @click="edit(scope.row)"
  83. >修改</el-button
  84. >
  85. <el-button
  86. type="text"
  87. icon="el-icon-view"
  88. size="small"
  89. @click="del(scope.row.id)"
  90. >删除</el-button
  91. >
  92. </template>
  93. </vxe-column>
  94. </vxe-table>
  95. <vxe-pager
  96. background
  97. size="small"
  98. :current-page="tablePage.currentPage"
  99. :page-size="tablePage.pageSize"
  100. :total="tablePage.total"
  101. :page-sizes="[10, 20, 100, 1000, { label: '全量数据', value: 1000000 }]"
  102. :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
  103. @page-change="currentChangeHandle"
  104. >
  105. </vxe-pager>
  106. </div>
  107. </div>
  108. </template>
  109. <script>
  110. export default {
  111. data() {
  112. return {
  113. loading: false,
  114. dataList: [
  115. {
  116. contractName: '测试合同',
  117. company: '测试单位名称',
  118. bankName: '测试开户行',
  119. cardNumber: '165489656589727',
  120. payAmount: '188990',
  121. payType: '银行转账',
  122. payDate: '1990-10-10',
  123. reason: '原因原因原因原因原因原因',
  124. remarks: '测试备注'
  125. }
  126. ],
  127. tablePage: {
  128. total: 0,
  129. currentPage: 1,
  130. pageSize: 10,
  131. orders: [],
  132. },
  133. dataListSelections: [],
  134. };
  135. },
  136. mounted() {},
  137. methods: {
  138. // 获取列表
  139. refreshList() {
  140. this.loading = true;
  141. this.$http({
  142. url: '/iot/wft/wftGate/list',
  143. method: 'get',
  144. params: {
  145. current: this.tablePage.currentPage,
  146. size: this.tablePage.pageSize,
  147. orders: this.tablePage.orders,
  148. contractId: this.curContractId,
  149. ...this.searchForm,
  150. }
  151. }).then(({ data }) => {
  152. this.dataList = data.records;
  153. this.tablePage.total = data.total;
  154. this.loading = false;
  155. });
  156. },
  157. // 查看
  158. view(row) {},
  159. // 修改
  160. edit(row) {},
  161. // 删除
  162. del(id) {
  163. let ids = id || this.$refs.VXETable.getCheckboxRecords().map((item) => item.id).join(",");
  164. this.$confirm(`确定删除所选项吗?`, "提示", {
  165. confirmButtonText: "确定",
  166. cancelButtonText: "取消",
  167. type: "warning",
  168. }).then(() => {
  169. this.loading = true;
  170. this.$http({
  171. url: "/iot/wft/wftGate/delete",
  172. method: "delete",
  173. params: { ids },
  174. }).then(({ data }) => {
  175. if (data && data.success) {
  176. this.$message.success(data.msg);
  177. this.refreshList();
  178. }
  179. this.loading = false;
  180. });
  181. });
  182. },
  183. // 当前页
  184. currentChangeHandle({ currentPage, pageSize }) {
  185. this.tablePage.currentPage = currentPage;
  186. this.tablePage.pageSize = pageSize;
  187. this.refreshList();
  188. },
  189. // 排序
  190. sortChangeHandle(column) {
  191. this.tablePage.orders = [];
  192. if (column.order != null) {
  193. this.tablePage.orders.push({
  194. column: this.$utils.toLine(column.property),
  195. asc: column.order === "asc",
  196. });
  197. }
  198. this.refreshList();
  199. },
  200. // 重置
  201. resetSearch() {
  202. this.$refs.searchForm.resetFields();
  203. this.refreshList();
  204. },
  205. },
  206. };
  207. </script>
  208. <style scoped>
  209. .vxe-table {
  210. width: 100%;
  211. height: 100%;
  212. }
  213. </style>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/692045
推荐阅读
相关标签
  

闽ICP备14008679号