element-ui 表格时间范围查询-分页
1 效果
-
前端vue+在这里插入代码片Javascript
-
element-ui的日期时间选择器选择时间范围
-
filter( )函数将时间范围内的数据过滤
-
表格分页设置
2 实现方法
- 日期时间选择器
- 响应式布局
<el-row>
<el-col :xs="4" :sm="6" :md="8" :lg="12" :xl="8">
<div>
<span style="margin: 0px 5px 0px 0px">查询时间范围</span>
<el-date-picker v-model="search_data.startTime" type="datetime">
</el-date-picker>
--
<el-date-picker v-model="search_data.endTime" type="datetime">
</el-date-picker>
</div>
</el-col>
<el-col :xs="8" :sm="6" :md="4" :lg="2" :xl="1">
<div>
<el-button type="primary" icon="search" @click="imtScreen()"
>查询</el-button
>
</div>
</el-col>
<el-col :xs="8" :sm="6" :md="4" :lg="1" :xl="1">
<el-button type="primary" @click="reset()">返回</el-button>
</el-col>
</el-row>
- 获取表格数据
getEsbData() {
console.log("requesting......");
this.$axios
.get("/esbList")
.then((res) => {
for (var i = 0; i < res.data.length; i++) {
var esb = Object();
esb["id"] = res.data[i]["id"];
esb["requestAddress"] = res.data[i]["requestAddress"];
esb["cvtJson"] = res.data[i]["cvtJson"];
esb["retJson"] = res.data[i]["retJson"];
esb["getJson"] = res.data[i]["getJson"];
var endDt = new Date(res.data[i]["endDt"]);
var startDt = new Date(res.data[i]["startDt"]);
var cvtDt = new Date(res.data[i]["cvtDt"]);
esb["startDt"] = res.data[i]["startDt"];
// 总耗时
esb["allDt"] = endDt - startDt;
// 乙方耗时
esb["yiDt"] = cvtDt - startDt;
// 对方耗时
esb["duiDt"] = endDt - cvtDt;
this.esbData.push(esb);
this.filterTableData.push(esb);
// esb["retJson"]
console.log(res.data[i]);
}
// 设置分页数据
this.setPaginations();
this.loading=false
})
.catch((err) => {
console.log(err);
});
},
- 分页设置
<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="this.paginations.page_index"
:page-sizes="this.paginations.page_sizes" :page-size="this.paginations.page_size" layout="total, sizes, prev, pager, next, jumper"
:total="esbData.length">
</el-pagination>
data() {
return {
centerDialogVisible: false,
name: "",
esbData: [],
BaoWen: "",
filterTableData: [], //经过时间筛选后得到的数据
search_data: {
startTime: '',
endTime: '',
},
loading: true,
tableData:[],
//需要给分页组件传的信息
paginations: {
page_index: 1, // 当前位于哪页
total: 0, // 总数
page_size: 5, // 1页显示多少条
page_sizes: [5, 10, 15, 20], //每页显示多少条
layout: "total, sizes, prev, pager, next, jumper" // 翻页属性
}
};
},
method(){
handleSizeChange(page_size) {
// 切换size
this.paginations.page_index = 1;
this.paginations.page_size = page_size;
this.tableData = this.esbData.filter((item, index) => {
return index < page_size;
});
},
handleCurrentChange(page) {
// 当前页
let sortnum = this.paginations.page_size * (page - 1);
let table = this.esbData.filter((item, index) => {
return index >= sortnum;
});
// 设置默认分页数据
this.tableData = table.filter((item, index) => {
return index < this.paginations.page_size;
});
},
setPaginations() {
// 总页数
this.paginations.total = this.esbData.length;
this.paginations.page_index = 1;
this.paginations.page_size = 5;
// 设置默认分页数据
this.tableData = this.esbData.filter((item, index) => {
return index < this.paginations.page_size;
});
},
}
- 实现筛选功能
imtScreen() {
//判断是否输入时间区间
if (!this.search_data.startTime || !this.search_data.endTime) {
this.$message({
type: "warning",
message: "请选择时间区间!",
});
}
const stime = this.search_data.startTime.getTime();
console.log(stime)
const etime = this.search_data.endTime.getTime();
//将筛选后的数据赋值给 allTableDate
this.esbData = this.filterTableData.filter((item) => {
//筛选后得到的数据 item 中包含数据日期 date
//创建一个数组 date,存储得到的item.date
let date = new Date(item.startDt);
let time = date.getTime();
return time >= stime && time <= etime;
});
//重新分页
this.setPaginations();
},
reset() {
this.esbData = this.filterTableData;
},
3 完整代码
<template>
<div>
<h1>ESB日志管理和数据流传统计</h1>
<el-row>
<el-col :xs="4" :sm="6" :md="8" :lg="12" :xl="8">
<div>
<span style="margin: 0px 5px 0px 0px">查询时间范围</span>
<el-date-picker v-model="search_data.startTime" type="datetime">
</el-date-picker>
--
<el-date-picker v-model="search_data.endTime" type="datetime">
</el-date-picker>
</div>
</el-col>
<el-col :xs="8" :sm="6" :md="4" :lg="2" :xl="1">
<div>
<el-button type="primary" icon="search" @click="imtScreen()"
>查询</el-button
>
</div>
</el-col>
<el-col :xs="8" :sm="6" :md="4" :lg="1" :xl="1">
<el-button type="primary" @click="reset()">返回</el-button>
</el-col>
</el-row>
<el-card shadow="always" style="margin: 20px 20px 20px 20px">
<el-table v-loading="loading" :data="tableData" style="width: 100%">
<el-table-column prop="requestAddress" label="请求地址">
</el-table-column>
<el-table-column
prop="startDt"
label="开始时间"
:formatter="dateFormat"
>
</el-table-column>
<el-table-column prop="allDt" label="总耗时ms"></el-table-column>
<el-table-column prop="yiDt" label="己方耗时ms"> </el-table-column>
<el-table-column prop="duiDt" label="对方耗时ms"> </el-table-column>
<el-table-column label="初始报文" width="100">
<template slot-scope="scope">
<el-button
type="text"
@click="handleClick('初始报文', scope.$index, scope.row)"
>初始报文</el-button
>
</template>
</el-table-column>
<el-table-column label="转换报文" width="100">
<template slot-scope="scope">
<el-button
type="text"
@click="handleClick('转换报文', scope.$index, scope.row)"
>转换报文</el-button
>
</template>
</el-table-column>
<el-table-column label="回执报文" width="100">
<template slot-scope="scope">
<el-button
type="text"
@click="handleClick('回执报文', scope.$index, scope.row)"
>回执报文</el-button
>
</template>
</el-table-column>
</el-table>
<div>
<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="this.paginations.page_index"
:page-sizes="this.paginations.page_sizes" :page-size="this.paginations.page_size" layout="total, sizes, prev, pager, next, jumper"
:total="esbData.length">
</el-pagination>
</div>
</el-card>
<!-- props 父组件传参子组件 -->
<mydialog
:centerDialogVisible="this.centerDialogVisible"
:name="this.name"
:BaoWen="this.BaoWen"
@setDialog="getDialog"
></mydialog>
</div>
</template>
<script>
// import mypagination from "./pagination.vue";
import dateselect from "./DateSelect.vue";
import datetimerange from "./Datetimerange.vue";
import mydialog from "./dialog.vue";
import ManagePage from "./ManagePage.vue";
export default {
components: {
dateselect,
datetimerange,
mydialog,
// mypagination,
},
data() {
return {
centerDialogVisible: false,
name: "",
esbData: [],
BaoWen: "",
filterTableData: [], //经过时间筛选后得到的数据
search_data: {
startTime: '',
endTime: '',
},
loading: true,
tableData:[],
//需要给分页组件传的信息
paginations: {
page_index: 1, // 当前位于哪页
total: 0, // 总数
page_size: 5, // 1页显示多少条
page_sizes: [5, 10, 15, 20], //每页显示多少条
layout: "total, sizes, prev, pager, next, jumper" // 翻页属性
}
};
},
methods: {
getEsbData() {
console.log("requesting......");
this.$axios
.get("/esbList")
.then((res) => {
for (var i = 0; i < res.data.length; i++) {
var esb = Object();
esb["id"] = res.data[i]["id"];
esb["requestAddress"] = res.data[i]["requestAddress"];
esb["cvtJson"] = res.data[i]["cvtJson"];
esb["retJson"] = res.data[i]["retJson"];
esb["getJson"] = res.data[i]["getJson"];
var endDt = new Date(res.data[i]["endDt"]);
var startDt = new Date(res.data[i]["startDt"]);
var cvtDt = new Date(res.data[i]["cvtDt"]);
esb["startDt"] = res.data[i]["startDt"];
// 总耗时
esb["allDt"] = endDt - startDt;
// 乙方耗时
esb["yiDt"] = cvtDt - startDt;
// 对方耗时
esb["duiDt"] = endDt - cvtDt;
this.esbData.push(esb);
this.filterTableData.push(esb);
// esb["retJson"]
console.log(res.data[i]);
}
// 设置分页数据
this.setPaginations();
this.loading=false
})
.catch((err) => {
console.log(err);
});
},
handleClick(baowen, inedx, row) {
//赋值
this.centerDialogVisible = true;
this.name = baowen;
this.BaoWen = row;
// console.log(this.centerDialogVisible)
},
dateFormat(row, column, cellValue, index) {
const daterc = row[column.property];
if (daterc != null) {
var date = new Date(daterc);
var year = date.getFullYear();
/* 在日期格式中,月份是从0开始,11结束,因此要加0
* 使用三元表达式在小于10的前面加0,以达到格式统一 如 09:11:05
* */
var month =
date.getMonth() + 1 < 10
? "0" + (date.getMonth() + 1)
: date.getMonth() + 1;
var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
var hours =
date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
var minutes =
date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
var seconds =
date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
// 拼接
return (
year +
"/" +
month +
"/" +
day +
" " +
hours +
":" +
minutes +
":" +
seconds
);
}
},
getDialog(dialog) {
this.centerDialogVisible = dialog;
},
imtScreen() {
//判断是否输入时间区间
if (!this.search_data.startTime || !this.search_data.endTime) {
this.$message({
type: "warning",
message: "请选择时间区间!",
});
}
const stime = this.search_data.startTime.getTime();
console.log(stime)
const etime = this.search_data.endTime.getTime();
//将筛选后的数据赋值给 allTableDate
this.esbData = this.filterTableData.filter((item) => {
//筛选后得到的数据 item 中包含数据日期 date
//创建一个数组 date,存储得到的item.date
let date = new Date(item.startDt);
let time = date.getTime();
return time >= stime && time <= etime;
});
//重新分页
this.setPaginations();
},
reset() {
this.esbData = this.filterTableData;
},
handleSizeChange(page_size) {
// 切换size
this.paginations.page_index = 1;
this.paginations.page_size = page_size;
this.tableData = this.esbData.filter((item, index) => {
return index < page_size;
});
},
handleCurrentChange(page) {
// 当前页
let sortnum = this.paginations.page_size * (page - 1);
let table = this.esbData.filter((item, index) => {
return index >= sortnum;
});
// 设置默认分页数据
this.tableData = table.filter((item, index) => {
return index < this.paginations.page_size;
});
},
setPaginations() {
// 总页数
this.paginations.total = this.esbData.length;
this.paginations.page_index = 1;
this.paginations.page_size = 5;
// 设置默认分页数据
this.tableData = this.esbData.filter((item, index) => {
return index < this.paginations.page_size;
});
},
},
mounted() {
// console.log("fasdf")
this.getEsbData();
},
};
</script>