iview table render集成switch开关

iview table render集成switch开关
       今天想要分享的是iview table render集成switch开关修改table表格的值,看文档记得看2.0的,不注意打开就成1.0然后用上了一直没有效果又没有找出原因。给出的只是一种写法思路,具体自己集成。
一、效果如下

即是打开处理switch开关,对应修改为已处理状态,关闭switch开关,修改为未处理状态。

二、template html写法
<Table highlight-row border :columns="columns1" :data="data1" ref="table" :height="tableHeight"></Table>

三、data写法,table render函数写法,
columns1: [{
 fixed: 'right',
 title: 'Action',
 key: 'action',
 width: 250,
 align: 'center',
 render:(h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                style: {
                  marginRight: '20px'
                },
                on: {
                  click: () => {
                    this.show(params.index)
                  }
                }
              }, '阅览'),
              h('strong', {
                style: {
                  marginRight: '5px'
                },
              }, '处理'),
              h('i-switch', { //数据库1是已处理,0是未处理
                props: {
                  type: 'primary',
                  value: params.row.treatment === 1  //控制开关的打开或关闭状态,官网文档属性是value
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  'on-change': (value) => {//触发事件是on-change,用双引号括起来,
                                           //参数value是回调值,并没有使用到
                    this.switch(params.index) //params.index是拿到table的行序列,可以取到对应的表格值
                  }
                }
              }, )
            ]);
          }
}]

四、methods方法
//通过开关状态判断值然后传值进行更新
   switch(index) {
      //打开是true,已经处理1
      if (this.data1[index].treatment == 1) {
        this.data1[index].treatment = 0
        this.updateFeedbackMessage(this.data1[index].id, 'treatment', this.data1[index].treatment)
      } else {
        this.updateFeedbackMessage(this.data1[index].id, 'treatment', 1)
      }
    },


    //更新反馈信息某一字段
    updateFeedbackMessage(id, key, value) {
      var vm = this
      var data = {
        id: id
      }
      data[key] = value
      vm.$http.put('/v1/suggestion', data).then(function (response) {
        if (response.data.code == '000000') {
          vm.$Message.info('更新成功');
          vm.getFeedbackMessages()//获取table数据信息,这里调用是因为修改值之后马上可以更新table值
        }
      }).catch((error) => {
        console.log(error)
      })
    },

 //获取所有反馈信息列表
    getFeedbackMessages() {
      var vm = this
      var url = '/v1/suggestions?'
      url = url + "pageNum=" + this.pageNum + '&pageSize=' + this.pageSize
      if (this.createByValue != '') {
        url = url + '&createBy=' + this.createByValue
      }
      if (this.dealModelValue != '') {
        url = url + '&treatment=' + this.dealModelValue
      }
      this.$http.get(url).then(response => {
        if (response.data.code == '000000') {
          vm.data1 = response.data.data
          vm.pageTotal = parseInt(response.data.message)
        }
      }).catch(error => {
        console.log(error)
      })
    },

注重思路,有问题一起交流

iView 是一个基于 Vue.js 的高质量 UI 组件库,它提供了丰富的组件和功能。关于您提到的 `Switch` 组件,它是一个开关按钮,可以用来切换开/关状态。在使用 iView 的 `Switch` 组件时,如果您想取消改变状态,通常可以通过绑定一个变量到 `Switch` 的 `v-model` 属性,并在状态改变的事件处理函数中更新这个变量来控制状态的变化。不过,如果您想要在状态即将改变时阻止这种改变,可以利用 `change` 事件来实现。 以下是一个简单的例子来说明如何控制 `Switch` 组件状态的改变: ```html <template> <Switch v-model="switchValue" @change="handleChange"/> </template> <script> export default { data() { return { // 初始状态可以设置为 true 或 false switchValue: true } }, methods: { handleChange(newValue) { // 在这里可以加入一些逻辑判断 // 如果满足某个条件,就阻止状态改变 if (someCondition) { // 这里可以将开关状态设置回之前的值,或者直接阻止 // 例如将状态设置回 false,不让它切换到 true this.switchValue = !newValue; // 或者使用 alert 提示用户,并且不改变状态 alert('状态改变被阻止'); // 注意:如果在 change 事件中直接修改绑定的变量的值,可能会导致组件不响应,所以一般不推荐直接修改,而是通过用户交互或其他逻辑来控制状态变化 } } } } </script> ``` 在上面的代码中,`handleChange` 方法会根据 `someCondition` 的条件判断是否要阻止状态的改变。如果需要阻止改变,可以根据实际情况将 `switchValue` 设置回原来的值,或者使用其他逻辑来处理。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值