获取并控制elmentui列表中指定元素

<!-- 使用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>    


Element UI ,我们可以使用 `$refs` 来获取组件的引用,然后使用 `filter` 方法进行模糊查询。以下是一个简单的例子: ```html <template> <div> <el-input v-model="searchText" placeholder="输入关键字搜索" @input="search"></el-input> <el-table ref="table" :data="tableData" style="width: 100%"> <el-table-column prop="name" label="名称"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> </div> </template> <script> export default { data() { return { tableData: [ { id: 1, name: '张三', age: 20, address: '北京市' }, { id: 2, name: '李四', age: 22, address: '上海市' }, { id: 3, name: '王五', age: 25, address: '广州市' }, { id: 4, name: '赵六', age: 28, address: '深圳市' }, ], searchText: '', }; }, methods: { search() { const keyword = this.searchText.trim(); if (keyword === '') { this.$refs.table.clearFilter(); } else { this.$refs.table.filter((value, key) => { return value.toString().toLowerCase().indexOf(keyword.toLowerCase()) !== -1; }); } }, }, }; </script> ``` 这段代码,我们使用 `ref` 给 `<el-table>` 组件创建了一个引用,然后在 `search` 方法通过 `$refs` 获取了这个引用。当用户输入关键字时,我们使用 `filter` 方法对表格数据进行模糊查询。如果关键字为空,则使用 `clearFilter` 方法清除过滤结果。如果关键字不为空,则使用 `filter` 方法对表格数据进行过滤,过滤条件为每个单元格的值是否包含关键字。最后,我们在模板使用 `v-model` 绑定 `searchText` 的值,当用户输入关键字时,会自动触发 `search` 方法进行查询。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值