Element UI 分页和条件查询示例代码
<template>
<div>
<el-form :inline="true" :model="searchForm">
<el-form-item label="姓名">
<el-input v-model="searchForm.name"></el-input>
</el-form-item>
<el-form-item label="年龄">
<el-input v-model.number="searchForm.age"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="handleSearch">查询</el-button>
</el-form-item>
</el-form>
<pre><code><el-table :data="tableData" border>
<el-table-column prop="name" label="姓名"></el-table-column>
<el-table-column prop="age" label="年龄"></el-table-column>
</el-table>
<el-pagination
:current-page="currentPage"
:page-size="pageSize"
:total="total"
@current-change="handlePageChange"
layout="total, prev, pager, next, jumper"
></el-pagination>
</code></pre>
</div>
</template>
<script>
export default {
data() {
return {
searchForm: {
name: '',
age: null,
},
tableData: [],
currentPage: 1,
pageSize: 10,
total: 0,
}
},
methods: {
handleSearch() {
// 发送请求获取数据
this.fetchTableData()
},
handlePageChange(currentPage) {
this.currentPage = currentPage
this.fetchTableData()
},
fetchTableData() {
// 发送请求获取数据
// 并更新 tableData 和 total
},
},
}
</script>
<p>在上面的代码中,我们定义了一个带条件查询和分页的表格。搜索条件包括姓名和年龄,分页使用了 element-ui 的分页组件。在 <code>handleSearch</code> 方法中,我们发送请求并更新表格数据,同时重置当前页为第一页。在 <code>handlePageChange</code> 方法中,我们更新当前页并重新获取数据。<code>fetchTableData</code> 方法是获取数据的实际方法,我们可以在其中发送请求,获取数据,并更新 <code>tableData</code> 和 <code>total</code> 属性。</p>
<p>需要注意的是,我们在 <code>v-model</code> 中使用了 <code>v-model.number</code>,这是因为我们的年龄是一个数字类型,而 <code>v-model</code> 默认会将输入的内容转为字符串类型,使用 <code>v-model.number</code> 可以将其转为数字类型。</p>
原文地址: https://www.cveoy.top/t/topic/nTKA 著作权归作者所有。请勿转载和采集!