【Vue3+naive】封装自定义表格内容

1、新建工具方法 render.ts

import { h } from 'vue'
import { alpha } from '@/utils/common'
import { useDesignStore } from '@/store/modules/designStore/designStore'
import { NPopconfirm } from 'naive-ui'
const useDesignStoreState = useDesignStore()
interface optionType {
  func: (row: any) => void
  onClick?: boolean
  arguments?: any
  textColor?: any
  joinParams?: string
  iconFontStyle?: {
    icon: string
    size?: number
    color?: string
  }
}
/**
 *
 * @param row  当前行数据
 * @param field  当前字段
 * @param func  当前方法
 * @param tooltip  是否需要 tooltip
 * @param special  是否需要 特殊显示 需要新增一条属性值为 ***Desc:row[field]Desc 的字段
 * @param textColor  字体显示颜色
 * @returns 返回 tooltipRender
 * @option 配置对象
 */
const tooltipRender = (
  row: any,
  field: string,
  option: optionType = { func: function () {}, onClick: true },
  tooltip: boolean = true,
  special: boolean = false,
  textColor: any = alpha(<string>useDesignStoreState.getAppTheme)
) => {
  option = {
    onClick: true,
    ...option
  }
  return tooltip
    ? h(
        NPopconfirm,
        {
          trigger: 'hover',
          showIcon: false,
          positiveText: null,
          negativeText: null,
          arrowStyle: {
            background: 'rgba(51, 54, 57, 0.1)'
          },
          style: {
            background: 'rgba(51, 54, 57, 0.9)',
            color: 'white',
            maxWidth: '200px'
          }
        },
        {
          trigger: () => {
            return h(
              'span',
              {
                style: {
                  color: textColor,
                  cursor: 'pointer',
                  overflow: 'hidden',
                  textOverflow: 'ellipsis',
                  whiteSpace: 'nowrap',
                  with: '100%',
                  display: 'block'
                  // justifyContent: 'center'
                },
                onClick: () => option.onClick && option.func(row)
              },
              { default: () => row[field] }
            )
          },
          default: () => {
            return h('div', {}, special ? row[`${field}Desc`] : row[field])
          }
        }
      )
    : h(
        'span',
        {
          style: {
            color: textColor,
            cursor: 'pointer'
          },
          onClick: () => option.onClick && option.func(row)
        },
        { default: () => row[field] }
      )
}

2、使用

 // 引用 
 import { tooltipRender } from '@/utils/render'
   {
      align: 'center',
      title: '',
      width: 70,
      key: 'projectName',
      render(row: any, index: any) {
        return tooltipRender(row, 'projectName', { func: goDetails })
      }
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值