前端-naive ui如何渲染表格中的开关switch、单选框checkbox、按钮

目的:在表格中渲染如下效果

做法:在column的配置项中,利用h函数来渲染你想要的效果,h函数的具体使用可以参考链接渲染函数 & JSX | Vue.js (vuejs.org)

 {
    key: 'isEnabled', title: '启用状态',
    searchType: 'select',
    searchOptions: () => qiyongOption.value,
    render: (row) => [
      h(
        NPopconfirm,
        {
          onPositiveClick: async () => {
            row.isEnabled = row.isEnabled === 1 ? 0 : 1
            for (const key in row) {
              if (Object.hasOwnProperty.call(row, key)) {
                if (row[key] === '-') {
                  row[key] = null;
                }
              }
            }
            //编辑并刷新列表
            await editList(row).then((res: any) => {
            }).catch((err) => {
              row.isEnabled = row.isEnabled === 1 ? 0 : 1
            });
            tableRef.value?.reload()
          }
        },
        {
          trigger: () => {
            return h(
              NSwitch,
              {
                value: row.isEnabled,
                checkedValue: 1,
                uncheckedValue: 0,
              },
              { default: () => '' }
            )
          },
          default: () => {
            return '确认启用/停用吗?'
          }
        }
      )
    ]
  },
{
    key: 'maintenanceReminder', title: '保养提醒', hideInSearch: true,
    render(row, index) {
      return h(NCheckbox, {
        checked: row.maintenanceReminder,
        onUpdateChecked: async (newVal) => {
          row.maintenanceReminder = newVal;
          let temp = row.forceShutdown
          if (row.maintenanceReminder === false) {
            row.forceShutdown = false
          }
          //刷新列表
          await editList(row).then((res: any) => {
            tableRef.value?.reload()
          }).catch(() => {
            row.maintenanceReminder = !row.maintenanceReminder;
            row.forceShutdown = temp;
          });
        },
      },);
    }
  },
  {
    key: 'forceShutdown', title: '强制停机', hideInSearch: true,
    render(row, index) {
      return h(NCheckbox, {
        disabled: !row.maintenanceReminder,
        checked: row.forceShutdown,
        onUpdateChecked: async (newVal) => {
          row.forceShutdown = newVal;
          console.log("强制停机现在是", row.forceShutdown);
          //刷新列表
          await editList(row).then((res: any) => {
            tableRef.value?.reload()
          }).catch(() => {
            row.forceShutdown = !row.forceShutdown;
          });
        },
      });
    }
  },

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值