vue 结合 Element UI 之 Table 和 Pagination 组件实现分页功能

组件代码:

<template>
  <div class="table">
  <!--表格-->
  <el-table border stripe id="iTable" :data="list">
     <!--数据列-->
    <el-table-column v-for="(column, index) in columns" :prop="column.prop" :key="index" :label="column.label">
      <template slot-scope="scope">
        <template v-if="column.formatter">
          <span v-html="column.formatter(scope.row)"></span>
        </template>
        <template v-else><span>{{scope.row[column.prop]}}</span></template>
      </template>
    </el-table-column>
    <!--按钮操作组-->
    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-dropdown trigger="click" @command="handleCommand">
          <el-button type="primary" size="mini">
            操作<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu v-if="scope.row.type" slot="dropdown">
            <el-dropdown-item v-for="(btn,key) in scope.row.type" :key="key" :command='[btn.label,scope.row]'>{{btn.label}}</el-dropdown-item>
          </el-dropdown-menu>
          <el-dropdown-menu v-else slot="dropdown">
            <el-dropdown-item v-for="(btn,key) in operates" :key="key" :command='[btn.label,scope.row]'>{{btn.label}}</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </template>
    </el-table-column>
  </el-table>
  <!--分页-->
  <div class="block">
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page.PageIndex" :page-sizes="[10, 20, 30,50,100]" 
    :page-size="page.PageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalCount">
    </el-pagination>
  </div>
 </div>
</template>
<script>
export default {
  props:{
    list: {
      type: Array,
      default: []
    },
    page:{
      type:Object,
      default:null
    },
    columns:{
      type:Array,
      default:[]
    },
    operates: {
      type: Array,
      default:[]
    },
    totalCount:{
      type:Number,
      default:0
    }
  },
  methods:{
     // 切换每页显示的数量
    handleSizeChange (size) {
      this.$emit('handleSizeChange', size);
      this.pageIndex = 1;
    },
    // 切换页码
    handleCurrentChange (index) {
      this.$emit('handleCurrentChange', index);
      this.pageIndex = index;
    },
   // 操作
    handleCommand(command){
      this.$emit('handleCommand', command);
    }
  }
}
</script>

引用组件代码:

<template>
     <div class="table-page">
        <i-table  :columns="columns" :list="list" :operates="operates" :totalCount="totalCount" :page="page" @handleSizeChange="handleSizeChange"
        @handleCurrentChange = "handleCurrentChange" :tabelLoading="tabelLoading" @handleCommand="handleCommand">
        </i-table>
    </div>
</template>
<script>
import iTable from './iTable'
export default {
    components: {iTable},
    data(){
        return{
            tabelLoading:false,
            totalCount:6,
            page: {
                PageSize: 10,
                PageIndex: 1,
                ConditionJson: {}
            },
            list:[
                {"id":"10000","title":"哈哈哈"},
                {"id":"10001","title":"哈哈dfsf哈"},
                {"id":"10002","title":"哈哈4234哈"},
                {"id":"10003","title":"哈666哈哈"},
                {"id":"10004","title":"哈234324哈哈"},
                {"id":"10005","title":"哈哈r哈"},
            ],
            columns: [
            {
                prop: 'id',
                label: '编号',
                formatter: (row) => {
                    if(row.id == "10000"){
                        return `<span>编号1</span>`
                    }else{
                        return row.id
                    }
                }
            },
            {
                prop: 'title',
                label: '标题',
                formatter: (row) => {
                    return `<span style="white-space: nowrap;color: dodgerblue;">${row.title}</span>`
                }
            }],
            operates:  [
            {
                label: '编辑'
            },
            {
                label: '删除'
            }]// 列操作按钮`
        }
        
    },
    methods:{
        // 切换每页显示的数量
        handleSizeChange (size) {
            this.limit = size
            console.log(' this.limit:', this.limit)
        },
        // 切换页码
        handleCurrentChange (index) {
            this.page = index
            console.log(' this.page:', this.page)
        },
        // 操作
        handleCommand(command){
            console.log(command)
        }
    }
}
</script>

 

 

转载于:https://my.oschina.net/glysunset/blog/1835148

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue Element UI提供了el-pagination组件实现表格数据的分页功能。以下是实现步骤: 1. 在Vue组件中引入el-pagination组件并定义分页属性 ``` <template> <div> <el-table :data="tableData" border> //表格内容 </el-table> <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination> </div> </template> <script> export default { data() { return { tableData: [], //表格数据 total: 0, //总记录数 pageSize: 10, //每页记录数 currentPage: 1 //当前页码 }; }, methods: { handleSizeChange(val) { this.pageSize = val; this.getData(); }, handleCurrentChange(val) { this.currentPage = val; this.getData(); }, getData() { //获取数据,并更新this.tableData和this.total } } }; </script> ``` 2. 在el-pagination组件中绑定事件和属性 - @size-change:当每页显示条数改变时触发的事件,调用handleSizeChange方法更新pageSize并重新获取数据。 - @current-change:当当前页码改变时触发的事件,调用handleCurrentChange方法更新currentPage并重新获取数据。 - :current-page:当前页码,绑定到currentPage属性。 - :page-sizes:每页显示条数数组,可以自定义,默认为[10, 20, 30, 40]。 - :page-size:每页显示条数,绑定到pageSize属性。 - layout:分页组件布局。其中total表示总记录数,sizes表示每页显示条数选择器,prev表示上一页按钮,pager表示页码按钮,next表示下一页按钮,jumper表示跳转输入框和确定按钮。 - :total:总记录数,绑定到total属性。 3. 在getData方法中获取数据并更新表格数据和总记录数 ``` getData() { //计算分页查询的参数 const start = (this.currentPage - 1) * this.pageSize; const limit = this.pageSize; //发起分页查询请求 axios.get('/api/data', { params: { start, limit } }).then(response => { this.tableData = response.data.rows; //更新表格数据 this.total = response.data.total; //更新总记录数 }); } ``` 以上就是使用Vue Element UI实现表格数据分页功能的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值