vue的表格的状态switch实现

<el-table-column label="状态" align="center" prop="status">
  <template slot-scope="scope">
    <el-switch
      v-model="scope.row.status"
      active-value="1"
      inactive-value="0"
      @change="handleStatusChange(scope.row)"
    ></el-switch>
  </template>
</el-table-column>

 

handleStatusChange(row) {
  let text = row.status === '1' ? '启用' : '停用'
  this.$confirm('确认要"' + text + '""' + row.username+ '"吗?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(function () {
    return changeStatus(row.id, row.status)
  }).then(() => {
    this.msgSuccess(text + '成功')
  }).catch(function () {
    row.status = row.status === '0' ? '1' : '0'
  })
},
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在 Ant Design Vue 中,可以通过在表格的列定义中利用 `scopedSlots` 自定义表格列的渲染方式。具体来说,可以在 `scopedSlots` 中定义一个包含 `<a-switch>` 的自定义模板,并且将表格行数据绑定到该模板中。这样,每次表格渲染时,就会根据行数据动态渲染 `<a-switch>` 组件,并且可以根据用户的操作动态修改行数据中的对应属性值。 以下是一个简单的示例代码: ``` <template> <a-table :columns="columns" :data-source="dataSource"> <template #switch="scope"> <a-switch v-model="scope.row.enabled" @change="handleChange(scope.row)" /> </template> </a-table> </template> <script> export default { data() { return { columns: [ { title: 'Name', dataIndex: 'name', key: 'name' }, { title: 'Enabled', key: 'enabled', scopedSlots: { customRender: 'switch' } } ], dataSource: [ { name: 'John Doe', enabled: true }, { name: 'Jane Smith', enabled: false } ] } }, methods: { handleChange(row) { console.log(`Switch value changed for row ${row.name}: ${row.enabled}`) // You can perform other actions here, such as making an API call to update the row data } } } </script> ``` 在这个示例中,我们定义了一个包含 `<a-switch>` 的自定义模板 `switch`,并且将其绑定到表格列的 `scopedSlots` 中。在该模板中,我们使用 `v-model` 指令将 `<a-switch>` 绑定到当前行数据中的 `enabled` 属性,并且定义了一个 `@change` 事件处理函数 `handleChange`,以便在用户修改开关状态时进行相应的处理。最后,我们将表格的列定义为一个包含开关模板的自定义列,并将数据源设置为一个包含两个对象的数组。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值