<template>
<div class="unit">
<!-- 搜索区域 -->
<div class="search-container">
<el-input
v-model="searchForm.communityname"
placeholder="小区名称"
class="first-input"
@keyup.enter.native="handleSearch"
:maxlength="50"
></el-input>
<el-input
v-model="searchForm.unitname"
placeholder="单元名称"
class="second-input"
@keyup.enter.native="handleSearch"
:maxlength="50"
></el-input>
<el-date-picker
v-model="searchForm.createtime"
align="right"
type="date"
placeholder="建设时间"
:picker-options="pickerOptions"
class="day"
@change="handleDateChange"
></el-date-picker>
<el-select
v-model="searchForm.unitstatus"
placeholder="选择状态"
class="choose"
@change="handleStatusChange"
>
<el-option
v-for="item in statusOptions"
:key="item.value"
:label="item.label"
:value="item.value"
></el-option>
</el-select>
<el-button
type="primary"
icon="el-icon-search"
class="search"
@click="handleSearch"
:loading="searchLoading"
></el-button>
<el-button
type="primary"
icon="el-icon-plus"
class="entry"
@click="handleAdd"
>录入</el-button
>
<el-button
type="warning"
icon="el-icon-refresh-right"
class="reset"
@click="handleReset"
>重置</el-button
>
</div>
<!-- 表格区域 -->
<el-table
:data="tableData"
border
stripe
class="table-container"
v-loading="loading"
element-loading-text="正在加载数据..."
>
<el-table-column
prop="communityname"
label="小区名称"
align="center"
min-width="120"
></el-table-column>
<el-table-column
prop="unitname"
label="单元名称"
align="center"
min-width="120"
></el-table-column>
<el-table-column
prop="unitnum"
label="栋数"
align="center"
min-width="80"
></el-table-column>
<el-table-column
label="建设时间"
align="center"
min-width="160"
>
<template slot-scope="scope">
{{ formatDateTime(scope.row.createtime) }}
</template>
</el-table-column>
<el-table-column
prop="unitstatus"
label="状态"
align="center"
min-width="100"
>
<template slot-scope="scope">
<el-tag
:type="scope.row.unitstatus === 1 ? 'success' : 'warning'"
>
{{ scope.row.unitstatus === 1 ? '已建成' : '未建成' }}
</el-tag>
</template>
</el-table-column>
<el-table-column label="操作" align="center" min-width="160">
<template slot-scope="scope">
<el-button
type="text"
@click="handleDetail(scope.row)"
class="detail-btn"
>详情</el-button
>
<el-button
type="text"
@click="handleArchive(scope.row)"
class="archive-btn"
>归档</el-button
>
</template>
</el-table-column>
</el-table>
<!-- 分页区域 -->
<div class="pagination-container" v-if="total > 0">
<el-pagination
background
:current-page="pagination.currentPage"
:page-size="pagination.pageSize"
:total="total"
:page-sizes="[10, 20, 50, 100]"
layout="total, sizes, prev, pager, next, jumper"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
></el-pagination>
</div>
<!-- 空状态提示 -->
<div v-else-if="!loading && total === 0" class="empty-state">
<el-empty description="暂无符合条件的数据"></el-empty>
</div>
</div>
</template>
<script>
export default {
data() {
return {
// 搜索表单数据
searchForm: {
communityname: "",
unitname: "",
unitnum: "",
createtime: "",
unitstatus: "",
},
statusOptions: [
{ value: 1, label: "已建成" },
{ value: 2, label: "未建成" },
],
// 表格数据
tableData: [],
// 加载状态
loading: false,
// 搜索按钮加载状态
searchLoading: false,
// 分页信息
pagination: {
currentPage: 1,
pageSize: 10,
},
// 总条数
total: 0,
// 日期选择器配置
pickerOptions: {
disabledDate(time) {
return time.getTime() > Date.now();
},
shortcuts: [
{
text: "今天",
onClick(picker) {
picker.$emit("pick", new Date());
},
},
{
text: "昨天",
onClick(picker) {
const date = new Date();
date.setTime(date.getTime() - 3600 * 1000 * 24);
picker.$emit("pick", date);
},
},
{
text: "一周前",
onClick(picker) {
const date = new Date();
date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
picker.$emit("pick", date);
},
},
],
},
};
},
created() {
this.fetchTableData();
},
methods: {
formatDateTime(isoString) {
if (!isoString) return '';
const date = new Date(isoString);
const year = date.getFullYear();
const month = this.padZero(date.getMonth() + 1);
const day = this.padZero(date.getDate());
const hours = this.padZero(date.getHours());
const minutes = this.padZero(date.getMinutes());
const seconds = this.padZero(date.getSeconds());
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
},
padZero(num) {
return num < 10 ? `0${num}` : num;
},
fetchTableData() {
this.loading = true;
const searchDate = this.searchForm.createtime
? this.formatDateForSearch(this.searchForm.createtime)
: '';
const params = {
currPage: this.pagination.currentPage - 1,
pageNum: this.pagination.pageSize,
communityname: this.searchForm.communityname.trim(), // 去除首尾空格
unitname: this.searchForm.unitname.trim(),
unitnum: this.searchForm.unitnum,
createtime: searchDate,
unitstatus: this.searchForm.unitstatus
};
this.$axios.get("http://community.byesame.com/house/gethouseList", { params })
.then((res) => {
this.loading = false;
this.searchLoading = false;
this.tableData = res.data.data || [];
this.total = res.data.total || 0;
})
.catch((err) => {
this.loading = false;
this.searchLoading = false;
console.error("接口请求失败:", err);
this.$message.error("网络错误,无法获取数据");
this.tableData = [];
this.total = 0;
});
},
formatDateForSearch(date) {
if (!date) return '';
if (date instanceof Date) {
const year = date.getFullYear();
const month = this.padZero(date.getMonth() + 1);
const day = this.padZero(date.getDate());
return `${year}-${month}-${day}`;
}
return date;
},
handleArchive(row) {
this.$confirm(
`确定要将【${row.communityname}-${row.unitname}】归档吗?`,
"提示",
{
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
}
)
.then(() => {
this.loading = true;
this.$axios
.post("http://community.byesame.com/house/archiveUnit", { id: row.id })
.then((res) => {
this.loading = false;
if (res.data.code === 200) {
this.$message.success("归档成功");
this.fetchTableData();
} else {
this.$message.error(res.data.message || "归档失败");
}
})
.catch((err) => {
this.loading = false;
console.error("归档失败:", err);
this.$message.error("归档操作失败");
});
})
.catch(() => {
this.$message.info("已取消归档");
});
},
handleSearch() {
// 简单验证
if (this.searchForm.communityname.length > 50) {
this.$message.warning("小区名称不能超过50个字符");
return;
}
if (this.searchForm.unitname.length > 50) {
this.$message.warning("单元名称不能超过50个字符");
return;
}
this.searchLoading = true;
this.pagination.currentPage = 1;
this.fetchTableData();
},
handleReset() {
this.searchForm = {
communityname: "",
unitname: "",
unitnum: "",
createtime: "",
unitstatus: "",
};
this.pagination.currentPage = 1;
this.fetchTableData();
},
handleAdd() {
this.$emit("openAddDialog");
},
handleDetail(row) {
this.$emit("openDetailDialog", row);
},
handleSizeChange(val) {
this.pagination.pageSize = val;
this.fetchTableData();
},
handleCurrentChange(val) {
this.pagination.currentPage = val;
this.fetchTableData();
},
// 日期选择后自动搜索
handleDateChange() {
this.handleSearch();
},
// 状态选择后自动搜索
handleStatusChange() {
this.handleSearch();
}
},
};
</script>
<style lang='scss' scoped>
.search-container {
margin-bottom: 20px;
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 10px;
}
.first-input,
.second-input,
.day,
.choose {
width: 263px;
}
/* 移除冗余的margin-left设置,使用gap统一控制间距 */
.search {
margin-left: 10px;
width: 43.6px;
height: 36px;
}
.entry,
.reset {
width: 116.78px;
height: 36px;
font-size: 14px;
}
.entry {
background-color: #67c23a;
border-color: #67c23a;
}
.table-container {
width: 100%;
margin-bottom: 20px;
}
.pagination-container {
display: flex;
justify-content: flex-end;
align-items: center;
padding: 10px 0;
}
.detail-btn {
color: #409eff;
margin-right: 10px;
}
.archive-btn {
color: #fa5555;
}
.empty-state {
margin: 50px 0;
text-align: center;
}
/* 响应式调整 */
@media (max-width: 1200px) {
.first-input,
.second-input,
.day,
.choose {
width: 220px;
}
}
@media (max-width: 992px) {
.first-input,
.second-input,
.day,
.choose {
width: calc(50% - 15px);
}
.search, .entry, .reset {
width: calc(33.33% - 10px);
margin-left: 0;
}
}
</style>我现在能实现当我点击上一页或者下一页的时候,列表里面的内容会发生改变,但是当我点击搜索的时候,列表里面的数据不会更新成我搜索的数据,帮我实现搜索的功能
最新发布