[转]iview render函数常用总结(vue render函数)

原文地址:https://blog.csdn.net/weixin_43206949/article/details/89385550

iview 的render函数就是vue的render函数
iview常用在表格里面自定义内容

render函数常用的配置

h就是createdElement的简写
3个参数如下:

h("元素名称或组件名称", {
              domProps: { // 原生dom元素的一些属性
                value: 1,
                type: 'number',
                min: 1,
                innerHTML:’‘
              },
              props:{ // 传给组件数据 比喻iview  Button的type,size 等等
                type:'text',
                size:'small'
              },
              class:{ // 类
                btn:true//
              },
              attrs:{ // html属性,class
                   id:'box'
                   class:'brn2'
              },
              style:{ // 内联样式
              },
              slot:'slotName', // 组件的插槽
              on:{ // 事件 包括组件的自定义事件
                  click:()=>{
                  },
                  'on-change':()=>{
                  },
              },
              nativeOn:{ // 类似vue的.native修饰符,自定义组件常用
                  click:()=>{
                  }
              }
              }
              ,'文本啊啊啊'
     )         

h的第二个参数支持数组的形式,数组里面可以是多个h渲染出来的元素对象或字符串

eg:
1,渲染多个组件

{
          title: '操作',
          width: 150,
          render: (h, { row, index }) => {
            let btn = h('i-switch', {
              props: {
                value: row.join_status === 1 ? true : false,
                size: 'large',
                loading: row.loading
              },
              on: {
                'on-change': (val) => {
                  this.tabData[index].loading = true
                  if (!val) {
                    this.$Modal.confirm({
                      title: '你确定要禁用该加盟商吗?',
                      content: '<p>禁用将会导致该加盟商下所有人员无法登陆CRM系统</p>',
                      onOk: () => {
                        this.changeChannelStatus(row.id)
                      },
                      onCancel: () => {
                        this.tabData[index].loading = false
                      }
                    });
                    return
                  }
                  this.changeChannelStatus(row.id)
                }
              }
            }, [
                h('span', {
                  slot: 'open',
                  domProps: {
                    innerHTML: 'ON'
                  }
                }),
                h('span', {
                  slot: 'close',
                  domProps: {
                    innerHTML: 'OFF'
                  }
                })
              ]
            )
            let edit = h('a', {
              style: {
                'margin-right': '15px',
              },
              on: {
                click: () => {
                  this.$router.push({ name: 'addJoiner', query: { ...row, tit: '编辑加盟商' } })
                }
              }
            }, '编辑')
            return h('div', [edit, btn])
          }

效果图:

switch 异步时loading状态

2,渲染自定义组件

  {
          title: '状态',
          render: (h, { row }) => {
            return h(myTag, {
              props: {
                color: row.join_status === 1 ? '#52C41A' : 'red'
              },
              class: {
                'hahah': true
              },
              nativeOn: { //事件
                click: () => {
                  alert(1)
                }
              }
            }, row.join_status === 1 ? '正常' : '解约')
          }
        },



值得注意的是配置的参数vlaue支持变量。

怎么绑定v-model?

这个问题vue官方说了:是没法绑定的,只能自己实现

实现也不难

eg:

 {
          title: '价格',
          key: 'name',
          render: (h, { row, index }) => {
            let input = h('input', {
              domProps: {
                value: row.price,
                type: 'number',
                min: 1
              },
              style: {
                width: '200px',
                display: 'inline-block',
                height: '32px',
                'line-height': 1.5,
              },
              on: {
                change: (event) => { // 实现绑定数据
                  let val = event.target.value
                  this.tabData[index].price = val
                }
              }
            })
            let arr = [input]
            let tip = h('span', {
              style: {
                color: 'red',
                marginLeft: '10px'
              }
            }, '必填,最多保留两位小数')
            if (row.tip) {
              arr.push(tip)
            }
            return h('div', arr)
          }
 }         



posted @ 2019-11-12 14:33  dirgo  阅读(1712)  评论(0编辑  收藏  举报