<!-- 使用scope.row表格当前行的数据对象 --> 这是核心。
<el-card style="margin-top: 1rem">
<el-table :data="tableData">
<el-table-column label="开屏" >
<template slot-scope="scope">
<!-- 使用scope.row表格当前行的数据对象 -->
<el-switch
v-model="scope.row.kaiping"
@change="rowKaiping(scope.row.id,scope.row.kaiping)"
active-color="#13ce66"
inactive-color="#ff4949">
</el-switch>
</template>
</el-table-column>
为了区分,红色的就是被控制的组件
以上图片完整代码展示
<template>
<div class="page">
<el-card style="margin-top: 20px">
<el-form :inline="true" :model="formInline" class="demo-form-inline">
<el-form-item label="版本号">
<el-input v-model="formInline.orderNumber" @keyup.enter.native="onSubmit" placeholder="请输入版本号"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="onSubmit" >查询</el-button>
</el-form-item>
</el-form>
</el-card>
<el-card style="margin-top: 1rem">
<el-table :data="tableData">
<el-table-column label="ID" prop="id"></el-table-column>
<el-table-column label="版本号" prop="version"></el-table-column>
<el-table-column label="下载路径" prop="url"></el-table-column>
<el-table-column label="更新内容" prop="text"></el-table-column>
<el-table-column label="应用渠道" prop="channel"></el-table-column>
<!-- 开屏列,包含开关 -->
<el-table-column label="开屏" >
<template slot-scope="scope">
<!-- 使用scope.row表格当前行的数据对象 -->
<el-switch
v-model="scope.row.kaiping"
@change="rowKaiping(scope.row.id,scope.row.kaiping)"
active-color="#13ce66"
inactive-color="#ff4949">
</el-switch>
</template>
</el-table-column>
<!-- 全屏列,包含开关 -->
<el-table-column label="全屏" >
<template slot-scope="scope">
<!-- 使用scope.row来访问当前行的数据 -->
<el-switch
v-model="scope.row.quanping"
@change="rowQuanping(scope.row.id,scope.row.quanping)"
active-color="#13ce66"
inactive-color="#ff4949">
</el-switch>
</template>
</el-table-column>
<!-- 信息流 -->
<el-table-column label="信息流" >
<template slot-scope="scope">
<!-- 使用scope.row来访问当前行的数据 -->
<el-switch
v-model="scope.row.xinxiliu"
@change="rowXinxiliu(scope.row.id,scope.row.xinxiliu)"
active-color="#13ce66"
inactive-color="#ff4949">
</el-switch>
</template>
</el-table-column>
<!-- draw广告 -->
<el-table-column label="draw广告" >
<template slot-scope="scope">
<!-- 使用scope.row来访问当前行的数据 -->
<el-switch
v-model="scope.row.draw"
@change="rowDraw(scope.row.id,scope.row.draw)"
active-color="#13ce66"
inactive-color="#ff4949">
</el-switch>
</template>
</el-table-column>
<!-- 激励视频 -->
<el-table-column label=" 激励视频" >
<template slot-scope="scope">
<!-- 使用scope.row来访问当前行的数据 -->
<el-switch
v-model="scope.row.jilishipin"
@change="rowjilishipin(scope.row.id,scope.row.jilishipin)"
active-color="#13ce66"
inactive-color="#ff4949">
</el-switch>
</template>
</el-table-column>
<!-- banner广告 -->
<el-table-column label=" banner广告" >
<template slot-scope="scope">
<!-- 使用scope.row来访问当前行的数据 -->
<el-switch
v-model="scope.row.banner"
@change="rowBanner(scope.row.id,scope.row.banner)"
active-color="#13ce66"
inactive-color="#ff4949">
</el-switch>
</template>
</el-table-column>
<el-table-column label="是否强制更新" prop="state"></el-table-column>
<el-table-column label="更新时间" prop="time"></el-table-column>
<el-table-column label="操作">
<template slot-scope="{ row }">
<!-- <el-button type="text" size="small" @click="$router.push(`/news/category/appedit?id=${row.id}`)">编辑</el-button> -->
<el-popconfirm title="确定删除吗?" @confirm="onDelete(row.id)">
<el-button type="text" size="small" slot="reference" style="color: #f56c6c">删除</el-button>
</el-popconfirm>
</template>
</el-table-column>
</el-table>
<!-- 分页代码 -->
<el-pagination
style="text-align: center; margin-top: 10px;"
class="pagination-right"
background layout="prev, pager, next"
@current-change="handlePageChange"
:total=totalPage>
</el-pagination>
</el-card>
</div>
</template>
<script>
import axios from 'axios';
export default {
data() {
return {
tableData: [],
created() {
this.getCategories(); // 组件创建时立即获取数据
},
currentPage: 1,
totalPage: '',
url:`http://localhost:9001/app-update/localUpdate`,
formInline: {
orderNumber: '',
region: ''
}
};
},
// vue钩子 页面加载时执行
mounted() {
this.getCategories();
if (!sessionStorage.getItem('state')) {
this.$router.push('/')
}
},
methods: {
// // 处理分页数据
handlePageChange(newPage) {
// newPage 是用户点击的页码
this.currentPage = newPage;
console.log(`当前页码: ${newPage}`);
// 可以选择在这里调用getCategories()来立即获取新页码的数据
if (!this.formInline.orderNumber.trim()) {
this.getCategories();
return;
}
this.onSubmit();
},
// 控制开屏开关方法
rowKaiping(id,kaiping){
console.log(this.url)
console.log("id"+id)
console.log("当前值"+kaiping)
// 数据改变
let data={
id,
kaiping
}
this.$http.post(this.url,data).then(resp=>{
if (resp.data==true) {
this.$message.success('修改成功');
} else {
this.$message.error('修改失败');
}
})
},
// 控制全屏开关方法
rowQuanping(id,quanping){
console.log("id"+id)
console.log("当前值"+quanping)
// 数据改变
let data={
id,
quanping
}
this.$http.post(this.url,data).then(resp=>{
if (resp.data==true) {
this.$message.success('修改成功');
} else {
this.$message.error('修改失败');
}
})
},
// 控制信息流开关方法
rowXinxiliu(id,xinxiliu){
console.log("id"+id)
console.log("当前值"+xinxiliu)
// 数据改变
let data={
id,
xinxiliu
}
this.$http.post(this.url,data).then(resp=>{
if (resp.data==true) {
this.$message.success('修改成功');
} else {
this.$message.error('修改失败');
}
})
},
// 控制draw开关方法
rowDraw(id,draw){
console.log("id"+id)
console.log("当前值"+draw)
// 数据改变
let data={
id,
draw
}
this.$http.post(this.url,data).then(resp=>{
if (resp.data==true) {
this.$message.success('修改成功');
} else {
this.$message.error('修改失败');
}
})
},
// 控制激励视频开关方法
rowjilishipin(id,jilishipin){
console.log("id"+id)
console.log("当前值"+jilishipin)
// 数据改变
let data={
id,
jilishipin
}
this.$http.post(this.url,data).then(resp=>{
if (resp.data==true) {
this.$message.success('修改成功');
} else {
this.$message.error('修改失败');
}
})
},
// 控制banner开关方法
rowBanner(id,banner){
console.log("id"+id)
console.log("当前值"+banner)
// 数据改变
let data={
id,
banner
}
this.$http.post(this.url,data).then(resp=>{
if (resp.data==true) {
this.$message.success('修改成功');
} else {
this.$message.error('修改失败');
}
})
},
getCategories() {
let url = `http://127.0.0.1:9001/app-update/selectList?page=${this.currentPage}`;
this.$http.get(url).then(res => {
console.log(res);
this.tableData= res.data.data;
this.totalPage = res.data.count * 10;
console.log(this.totalPage);
});
},
async onDelete(id) {
let url=`http://127.0.0.1:9001/app-update/localDelete?id=${id}`;
this.$http.get(url).then(resp=>{
if (resp.data==true) {
this.$message.success('删除成功');
this.getCategories();
} else {
this.$message.error('删除失败');
}
})
},
onSubmit() {
if (!this.formInline.orderNumber.trim()) {
// alert('请输入用户名!');
this.handlePageChange(1)
this.getCategories();
return;
}
let url = `http://127.0.0.1:9001/app/likeSelect?version=${this.formInline.orderNumber}&page=${this.currentPage}&pageSize=10`;
this.$http.get(url).then(res => {
console.log(res.data);
if (res.data.data.length == 0) {
url = `http://127.0.0.1:9001/app/likeSelect?version=${this.formInline.orderNumber}&page=1&pageSize=10`;
this.$http.get(url).then(result => {
this.categories = result.data.data;
console.log(this.categories);
this.totalPage = result.data.count * 10;
return;
});
}
this.categories = res.data.data;
this.totalPage = res.data.count * 10;
});
}
}
};
</script>
<style>
.pagination-right {
/* 假设父元素有足够的宽度来容纳分页控件,并且你希望分页控件紧贴右侧 */
display: flex; /* 使用 Flexbox 布局 */
justify-content: flex-end; /* 使得子元素(分页控件)在主轴的末尾对齐,即右对齐 */
/* 如果需要,还可以添加一些额外的样式来确保分页控件的布局 */
width: 60%; /* 使得分页控件填满父元素的宽度(可选) */
box-sizing: border-box; /* 包含 padding 和 border 在 width 内(可选) */
}
</style>