demo-table.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <el-container class="container">
  3. <el-header height="36px" style="line-height:36px;">
  4. <div class="filter-box">
  5. <el-input placeholder="请输入内容" class="input-with-select" :clearable="true" size="mini" v-model="fieldValue">
  6. <el-select slot="prepend" placeholder="请选择" width="110" v-model="fieldName">
  7. <el-option v-for="(item, index) in filters" :label="item" :value="item" :key="'filter' + index"></el-option>
  8. </el-select>
  9. <el-button slot="append" icon="el-icon-search" @click="query"></el-button>
  10. </el-input>
  11. </div>
  12. <el-button icon="el-icon-plus" type="primary" size="mini" v-if="showAction">添加</el-button>
  13. </el-header>
  14. <el-main>
  15. <el-table border style="width: 100%;overflow: auto;" size="mini">
  16. <el-table-column v-for="(item, index) in fields" :label="item" :key="'field' + index"></el-table-column>
  17. <el-table-column label="操作" v-if="showAction"></el-table-column>
  18. </el-table>
  19. </el-main>
  20. <el-footer height="36px">
  21. <el-pagination
  22. background
  23. @size-change="handleSizeChange"
  24. @current-change="handleCurrentChange"
  25. :current-page="page"
  26. :page-sizes="[10, 20, 50, 100, 200]"
  27. :page-size="size"
  28. :total="total"
  29. layout="total, sizes, prev, pager, next, jumper"
  30. ></el-pagination>
  31. </el-footer>
  32. </el-container>
  33. </template>
  34. <script>
  35. export default {
  36. name: 'demo-table',
  37. props: {
  38. fields: Array,
  39. filters: Array,
  40. readonly: Boolean,
  41. },
  42. data() {
  43. return {
  44. loading: false,
  45. dataSource: [],
  46. total: 0,
  47. page: 1,
  48. size: 20,
  49. fieldName: '',
  50. fieldValue: '',
  51. showAction: !this.readonly,
  52. };
  53. },
  54. mounted() {
  55. this.query();
  56. },
  57. methods: {
  58. async query() {
  59. this.loading = true;
  60. setTimeout(() => {
  61. this.loading = false;
  62. }, 1000);
  63. },
  64. handleSizeChange(val) {
  65. console.log(`每页 ${val} 条`);
  66. this.size = val;
  67. if (this.total === 0) return;
  68. const pages = Math.floor((this.total + val) / val);
  69. if (pages < this.page) this.page = pages;
  70. this.query();
  71. },
  72. handleCurrentChange(val) {
  73. console.log(`当前页: ${val}`);
  74. this.page = val;
  75. this.query();
  76. },
  77. },
  78. };
  79. </script>
  80. <style lang="less" scoped>
  81. .container {
  82. width: 100%;
  83. height: 100%;
  84. }
  85. .el-table {
  86. height: 100%;
  87. overflow: auto;
  88. }
  89. .filter-box {
  90. width: 280px;
  91. display: inline-block;
  92. .el-select {
  93. width: 100px;
  94. }
  95. }
  96. </style>