element-ui 单选框点击整个行为选中状态

element-ui 带单选框的表格

效果:不只是带单选框,点击当前行单选框选中状态
网上查了一些发现很多都是只能点击当前radio选中当前行,配合element-ui的单选table时发现两个的选择状态是不一致的,所以调整了一下
效果

提供下思路:

1.保证不止是点击单选框,点击列表某行也能够选中,通过设置highlight-current-row和@current-change="handleCurrentChange"实现

2.radio为单选框的值,选中后为当前行数,翻页后为保证重新选中状态需要重制

3.我的项目里需求是组件化形式,单选框选中值传递给父组件,父组件可能会有默认数据传入,需要在打开时设置点选状态

部门关键代码

<template>
  <el-table
    :data="tableData"
    ref="orderTable"
    @current-change="handleCurrentChange"
    tooltip-effect="light"
    highlight-current-row
    :cell-style="cellStyle"
   >

<!--

&nbsp; 为空,不加这个radio的label会显示index数字,注意从0开始的;radio会全选的问题是label相同导致的
disabled设置单选框是否可以被选择
-->
    <el-table-column label="选择" width="50" center>
      <template slot-scope="scope">
        <el-radio
          class="radio"
          v-model="radio"
          :label="scope.$index"
          @change.native="getCurrentRow(scope.$index)"
          :disabled="scope.row.Enable==1?false:true">
        &nbsp;</el-radio>
      </template>
    </el-table-column>
  </el-table>
    <el-pagination
    background
    layout="total, prev, pager, next"
    :current-page.sync="pagination.pageNum"
    :page-size="pagination.pageSize"
    :total="pagination.total"
    @current-change="changePage"
    :pager-count="5"
   ></el-pagination>

</template>
<script>
export default {
  data() {
    return {
      currentRow: null,
      radio: false,
      tableData: [],

  },  porps:{    //父组件传递过来的初始选中值,根据自己项目需求设置    chooseData:{      type:Object    }  },  watch:{    //观察是否有父组件传递的初始值或者变化,重新选中    chooseData(val){      if(val){        this.radio = false        this.getInitChoose()      }    }  },
  methods:{    
      getList() {
        this.isListLoading = true;
          getTableData().then(res => {

            this.tableData = res.item;
             //每次数据改变重新判断单选状态
            this.getInitChoose();

          })
      },
      //设置单选框选择状态
      getInitChoose() {
        if (this.chooseData) {
          let index = this.tableData.findIndex(
          item => item.userUuid == this.chooseData.id
        );
        if (index > -1) {
          this.radio = index;
        }
      },
      //由于翻页后重新获取列表了,需要把选择框选中状态取消
      changePage(pageNum) {
        this.pagination.pageNum = pageNum;
        this.radio = false
        this.getList()
      },

/* current-change    当表格的当前行发生变化的时候会触发该事件,如果要高亮当前行,请打开表格的 highlight-current-row 属性    currentRow, oldCurrentRow */
//保证点击当前行的任意位置不止是单选框也能够选择

      handleCurrentChange(val) {
        if (val && val.Enable == 1) {
          this.currentRow = val;
          let index = this.tableData.findIndex(
          item => item.userUuid == this.currentRow.userUuid
        )
        if (index > -1) {
          this.radio = index;
        }

        this.$emit(‘data‘,val.pkg)

      },
      getCurrentRow(index) {
        this.radio = index   
      },

   }
}

</script>

原博主:https://www.cnblogs.com/calamus/p/8569196.html

原文地址:https://www.cnblogs.com/wasbg/p/11416566.html

时间: 2024-08-05 13:44:53

element-ui 单选框点击整个行为选中状态的相关文章

jQuery实现点击单选按钮切换选中状态效果

实现效果:第一次点击单选按钮时选中该按钮,再次点击时取消选中该单选按钮. 关键就是要将单选按钮原来的值保存起来,第二次点击时.如果原来选中则取消,否则选中. 看代码吧,是用jQuery实现的,功能虽小.知识点不少啊..... Js代码   $(document).ready(function(){ var old = null; //用来保存原来的对象 $("input[name='sex']").each(function(){//循环绑定事件 if(this.checked){ o

label标签(实现单选框点字也能选中)

性别单选框: 原文地址:https://www.cnblogs.com/jihongtao/p/10291234.html

element ui table单选框点击全选问题

<template slot-scope="scope"> <el-radio-group v-model="scope.row.HandleState" size="small"> <el-radio :label="0" @change="operation(scope.row, RepairOrChangeListTwo[0].value,scope.row.ID)"&g

vue基于 element ui 的按钮点击节流

vue的按钮点击节流 场景: 1.在实际使用中,当我们填写表单,点击按钮提交的时候,当接口没返回之前,迅速的点击几次,就会造成多次提交. 2.获取验证码,不频繁的获取. 3.弹幕不能频繁的发 基于这几个场景,对 element-ui 的按钮进行扩展 节流 主要使用到了 vue的 $listeners 和 $attrs $listeners:子组件里面,获取父组件对子组件 v-on 的所有监听事件 $attrs: 包含了父作用域中不作为 prop 被识别 (且获取) 的特性绑定 (class 和 

jquery使被点击标签置于选中状态并发GET请求

$(document).ready(function(){ $("li.shop-list-menu-item").click(function(){ $("li.shop-list-menu-item").removeClass('active'); $(this).addClass('active'); var nodeVal = $(this).children('span').text(); if($.trim(nodeVal) == '卡牌') { url

jQuery对下拉框、单选框、多选框的处理

下拉框: 1 //得到下拉菜单的选中项的文本(注意中间有空格) 2 var cc1 = $(".formc select[@name='country'] option[@selected]").text(); 3 4 //得到下拉菜单的选中项的值 5 var cc2 = $('.formc select[@name="country"]').val(); 6 7 //得到下拉菜单的选中项的ID属性值 8 var cc3 = $('.formc select[@na

表单-图片浏览上传-单选框(二)

一.图片浏览上传 1.依然[table]标签包含, 2.[input]包含了[type]等于[file]. <table border="1" align="center"> <tr> <td>图片上传</td> <td><input type="file"</td> </tr> <tr> </table>  二.单选框——点击文字不

【前端JS】radio 可单选可点击取消选中

一般情况下 radio 单选框只能实现多选一的效果,但是一旦选择其中一个后,这个单选框就不可点击取消其选中状态了,这种功能在某些业务环境下并不适用,有时我们既需要单选框的多选一效果,也需要复选框的可点击取消效果.为此本文提供一种 JQuery 写法. /* 1. 取消与当前控件name 相同的所有控件的选中状态 2. 选中当前控件 3. 如果当前控件在点击前是选中状态,则点击后取消其选中状态 */ $("input:radio").click(function(){ var domNa

基于CSS3自定义发光radiobox单选框

之前我们分享过一些CSS3和HTML5实现的自定义checkbox和Radiobox,比如纯CSS3美化Checkbox和Radiobox按钮,不仅外观唯美,而且Radiobox选中时还有动画效果.今天我们要来分享一款CSS3实现的自定义发光radiobox单选框插件,该radiobox选中时也有滑块的动画特效. 在线预览   源码下载 实现的代码. html代码: <style> html { display: flex; justify-content: center; align-ite