【Vue中table单选框怎么操作】

<el-dialog title="新增" :visible.sync="addDialogVisible" width="60%" top="10vh">
      <el-table
          border
          stripe
          class="mb20"
          ref="multipleTable"
          :data="tableData"
          v-loading="tableLoading"
          tooltip-effect="dark"
          style="width: 100%"
        >
          <el-table-column label="选择" width="60">
            <template slot-scope="scope">
              <el-checkbox
                @change="handleCheck(scope.row)"
                v-model="scope.row.check"></el-checkbox>
            </template>
          </el-table-column>
          <el-table-column prop="community" label="小区名称" width="150"></el-table-column>
          <el-table-column prop="num" label="楼栋房号" width="150">
            <template slot-scope="scope">
              {{scope.row.unit}} {{scope.row.houseNumber}}
            </template>
          </el-table-column>
          <el-table-column prop="path" label="地址" show-overflow-tooltip></el-table-column>
      </el-table>
     <div slot="footer" class="dialog-footer">
       <el-button @click="addCancel()" size="mini">取 消</el-button>
       <el-button type="primary" @click="addConfirm" size="mini" :loading="btnLoading">确 定</el-button>
     </div>
  </el-dialog>

在这里插入图片描述
效果:只能单选 点击确定请求接口

methods: {
 getTableData() {
        this.tableLoading = true
        searchHouse(this.tableSearch).then(res => {
          let data = res.data.records || []
          // 为data加一个check属性来控制单选框
          data.forEach(o => {
            o.check = false;
          })
          this.tableData = data
          // console.log(this.tableData);
          this.tableTotal = Number(res.data.total)
          this.tableLoading = false
        }).catch(err => {
          this.tableLoading = false
        })
      },

 addConfirm() {
        let table = this.tableData.filter(item => item.check)
        if (!table.length) {
          this.$message.warning('请选择房屋')
          return
        }
        this.$refs['addFormRef'].validate((res) => {
          if(res) {
            let data = {
              bizopId: this.row.id,
              blockHouseNumber: table[0].block + table[0].unit + table[0].houseNumber,
              communityAddress: table[0].region + table[0].street + table[0].community,
              communityName: table[0].community,
              appointDate: this.addForm.date,
              signedHouseId: table[0].id,
            }
            this.btnLoading = true
            addLivingRec(data).then(res => {
              this.btnLoading = false
              this.addDialogVisible = false;
              this.$refs.addFormRef.resetFields();
              this.getlist()
            }).catch(e => {
              this.btnLoading = false
            })
          }
        })
      },
      
 handleCheck(row) {
        this.tableData.forEach(item => {
          // 如果选中的id不对其进行操作 其他未选中的check属性设置为false
          item.id == row.id ? '' :  item.check = false
        })
  }
Vue 实现表格单选的方法有很多种,以下是一种较为简单的实现方法: 1. 在表格的数据源添加一个选属性,用于标记该行是否被选。 2. 在表格添加一个单选框列,使用 v-model 绑定选属性。 3. 在单选框列的头部添加一个全选框,用于全选/取消全选。 4. 使用 computed 计算属性,获取选的行数据。 下面是示例代码: ``` <template> <div> <table> <thead> <tr> <th><input type="checkbox" v-model="allSelected"></th> <th>姓名</th> <th>年龄</th> <th>性别</th> </tr> </thead> <tbody> <tr v-for="(item, index) in tableData" :key="index"> <td><input type="checkbox" v-model="item.selected"></td> <td>{{ item.name }}</td> <td>{{ item.age }}</td> <td>{{ item.gender }}</td> </tr> </tbody> </table> </div> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 20, gender: '男', selected: false }, { name: '李四', age: 22, gender: '女', selected: false }, { name: '王五', age: 24, gender: '男', selected: false }, { name: '赵六', age: 26, gender: '女', selected: false }, ], allSelected: false, }; }, computed: { selectedData() { return this.tableData.filter(item => item.selected); }, }, watch: { allSelected(val) { this.tableData.forEach(item => { item.selected = val; }); }, }, }; </script> ``` 上述代码,`tableData` 是表格的数据源,其每一行数据都有一个 `selected` 属性,用于标记该行是否被选。`allSelected` 是全选框的选状态,使用 watch 监听其变化,并将表格每一行的选状态与其同步。`selectedData` 是计算属性,用于获取选的行数据。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值