iview table自定义columns组件实践(input、select、button等)

iview table单元格插入组件

如下图所示:可以在table单元格中自定义各种组件
示例
主要是通过自定义columns

<Table  
	border
	:columns="columns"
    :data="data" >
</Table>

1、span文案

columns: [
  {
    title: 'span',
    key: 'span',
    render: (h, params) => {
      return h("span", 'span应用');
    }
  }
]

2、div代码块

columns: [
  {
    title: 'div',
    key: 'div',
    render: (h, params) => {
      return h("div", 'div应用'); //文案内容可以加\n换行
    }
  }
]

3、icon 图标

columns: [
  {
    title: 'icon',
    key: 'icon',
    render: (h, params) => {
      return h("Icon", {
        props: {
          type: "ios-checkmark"
        }
      }, '')
    }
  }
]

4、Tag 标签

 columns: [
  {
    title: 'Tag',
    key: 'Tag',
    render: (h, params) => {
      return h("Tag", {
        props: {
          color: "success"
        }
      }, '联调中')
    }
  }
]

5、switch 开关

 columns: [
  {
    title: 'switch',
    key: 'status',
    render: (h, params) => {
      return h('i-switch', {
        props: {
          value: params.row.status ? true : false, // 根据状态判断是开还是关
          size: 'large',
          // 'before-change': this.change
        },
        on: {
          'on-change': (e) => {
            this.change(e) // switch的改变事件
          }
        }
      },
      // 开/关的文案
      [ 
        h('span', { slot: 'open' }, 'ON'),  
        h('span', { slot: 'close' }, 'OFF'),
      ]
     )
    }
// 哭死, iview (before-change:阻止切换) 无法获取当前列表项数据,当前render写法mousedown.native又不生效,果断更换element的el-switch
	return h('el-switch', {
      props: {
        value: params.row.status ? true : false
      },
      on: {
        change: (e) => {
          this.change(e, params.row)
        }
      }
    })
  }
]

6、Button 按钮

columns: [
 {
    title: 'Button',
    key: 'Button',
    render: (h, params) => {
      return h('Button', {
        props: { // 属性
          type: 'primary',
          size: 'small'
        },
        style: { //样式
          marginRight: '5px'
        },
        on: { // 事件
          click: () => {
            this.show(params.row)
          }
        }
      }, '详情')
    }
  }
]

7、Tooltip 文字提示

columns: [
  {
    title: 'Tooltip',
    key: "Tooltip",
    width: 200,
    align: "center",
    render: (h, params) => {
      const text = "超出5行隐藏;超出5行隐藏;超出5行隐藏;超出5行隐藏;超出5行隐藏;超出5行隐藏;超出5行隐藏;超出5行隐藏;超出5行隐藏;超出5行隐藏;超出5行隐藏"
      return h("div", [
        h(
          "Tooltip",
          {
            props: {
              theme: "light",
              maxWidth: 500,
              placement: "left-start",
            },
          },
          [
            h(
              "div", // 表格中的文案
              {
                style: {  // 超出5行隐藏并生成省略号
                  display: "-webkit-box",
                  "-webkit-box-orient": "vertical",
                  "-webkit-line-clamp": 5,
                  overflow: "hidden",
                  width: "100%",
                },
              },
              text
            ),
            h(
              "span", // 弹窗展示的文案
              {
                slot: "content",
              },
              text
            ),
          ]
        ),
      ]);
    },
  },
]

8、Input 输入框

columns: [
  {
    title: 'Input',
    key: 'Input',
    render: (h, params) => {
      return h('div', [
        h('Input', {
          props: { // 指定输入框target.value
            value: params.row.name,
            placeholder: '请输入'
          },
          on: {
            input: (e) => { // 动态设置data中的值
              this.$set(this.data[params.index], 'name', e);
            }
          }
        })
      ])
    }
  },
]

9、select 下拉框

columns: [
  {
    title: 'Select',
    key: 'Select',
    render: (h, params) => {
      return h('div', [
        h('Select', {
            props:{ //属性
                value: '',
                placeholder: '渠道',
                transfer: true
            },
            on: {  // 改变事件
                'on-change':(value) => {
                    this.data[params.index].channel= value;
                }
            },
        },  // 下拉项数据赋值
        this.channelList.map((obj)=>{
            return h('Option', {
                props: {value: '渠道名称'}
            });
        })
        )
      ])
    }
  }
]

以上就是各组件在table中的应用!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值