当全部数据加载完毕时:
有以上组件情况,点击查询更多按钮可实现懒加载功能。 主要看userList组件和父组件就可以了。
<template>
<div id="root">
<div id="container">
<div class="wrap">
<list-header @searchUser="searchUser" />
<user-list
:items="userList"
:haveMore="haveMore"
:itemBtn="itemBtnName"
@showMore="showMore"
/>
<list-footer
:items="userList"
:itemBtn="itemBtnName"
@changeAllDone="changeAllDone"
@clearAllItem="clearAllItem"
/>
</div>
</div>
</div>
</template>
script标签下的大概内容:
<script>
import ListHeader from "@/components/userData/ListHeader.vue";
import ListFooter from "@/components/userData/ListFooter.vue";
import UserList from "@/components/userData/UserList.vue";
import {
getUserList,
toBlackListUser,
toBlackListUsers,
} from "@/api/userManage";
export default {
name: "UserData",
components: {
ListHeader,
ListFooter,
UserList,
},
data() {
return {
userList: [],
itemBtnName: "拉黑",
pageInfo: {
totalPage: 0,
pageNum: 0,
},
haveMore: false,
keyword: "",
};
},
methods: {
addSeleted(dataList) {
const data = dataList.filter((user) => {
user.seleted = false;
return true;
});
return data;
},
async deleteItem(id) {
const state = (await toBlackListUser(id)).code;
console.log(state);
if (state == 200) {
this.userList = this.userList.filter((item) => item.id !== id);
}
},
async searchUser(keyword) {
this.keyword = keyword;
const APIData = (await getUserList(keyword, 1)).data;
if (APIData.userList.length > 0) {
this.userList = this.addSeleted(APIData.userList);
this.pageInfo.totalPage = APIData.pageInfo.totalPage;
this.pageInfo.pageNum = 1;
}
console.log("=====搜索用户结果:", this.userList);
if (this.pageInfo.totalPage > this.pageInfo.pageNum) {
this.haveMore = true;
} else {
this.haveMore = false;
}
},
async showMore() {
const APIData = (
await getUserList(this.keyword, this.pageInfo.pageNum + 1)
).data;
this.userList = this.userList.concat(this.addSeleted(APIData.userList));
this.pageInfo.pageNum += 1;
if (this.pageInfo.totalPage > this.pageInfo.pageNum) {
this.haveMore = true;
} else {
this.haveMore = false;
}
},
checkItem(id) {
this.userList.forEach((item) => {
if (item.id === id) item.seleted = !item.seleted;
});
},
changeAllDone(value) {
this.userList.forEach((item) => (item.seleted = value));
},
async clearAllItem() {
const idList = [];
this.userList.forEach((user) => {
if (user.seleted) {
idList.push(user.id);
}
});
const state = (await toBlackListUsers(idList)).code;
if (state == 200) {
this.userList = this.userList.filter((item) => !item.seleted);
}
},
},
mounted() {
this.$bus.$on("buttonHandleMethod", this.deleteItem);
this.$bus.$on("checkItem", this.checkItem);
},
};
</script>
userList组件的基本代码:
<template>
<div>
<ul class="main">
<user-item
v-for="item in items"
:key="item.index"
:item="item"
:itemBtn="itemBtn"
:className="className"
/>
</ul>
<button v-if="haveMore" @click="showMore">点击查询更多</button>
<div v-else>已无更多</div>
</div>
</template>
<script>
import UserItem from "@/components/userData/UserItem.vue";
export default {
name: "UserList",
components: {
UserItem,
},
props: {
items: Array,
itemBtn: String,
className: {
type: String,
default: "btn-danger",
},
haveMore: Boolean,
},
data() {
return {
moreShowBoolen: false,
};
},
methods: {
showMore() {
this.$emit("showMore");
},
},
};
</script>
方法很多种,这只是我用的其中一种。
|