iview的table組件中加入超連接組件,可編輯組件,選擇組件,日期組件


這篇文章主要介紹了iview的table組件中使用選擇框,日期,可編輯表格,超連接等組件。
javascript

1.select選擇組件

// tableColumn數組響應對象須要傳入一個固定的option數組,若是該數組是異步動態生成的該組件不能正常渲染,由於在獲取option數組以前table渲染時option是undefined。
supportSelect(item) {
     item.render = (h, params)=>{
         return h('Select', {
             props: {
                 value: params.row[params.column.key],
                 size: 'small',
                 transfer: true
             },
             on: {
                 'on-change': (val)=>{
                     this.insideTableData[params.index][params.column.key] = val
                 }
             },
         },item.option.map(item=>{
             return h('Option', {
                 props: {
                     value: item.value || item,
                     label: item.label || item
                 }
             }, item.label || item)
         }))
     }
     return item
 }

2.可編輯表格

// 可編輯表格使用了contenteditable屬性,使得表格編輯更加簡單幹淨
supportEdit(item, index){
    item.render = (h, params)=>{
        return h('div', {
            style: {
                padding: '4px 0',
                width: '100%'
            },
            props: {
                value: this.insideTableData[params.index][params.column.key]
            },
            attrs: {
                contenteditable: this.editable,
                title: '點擊可編輯'
            },
            on: {
                'blur': evt=>{
                    evt.target.value = evt.target.innerText || params.row[params.column.key]
                    params.row[params.column.key] = evt.target.innerText
                    this.insideTableData[params.index][params.column.key] = evt.target.innerText
                }
            }
        }, params.row[params.column.key])
    }
    return item
}

3.日期組件

// 使用iview的DatePicker組件渲染就行
supportDate(item){
    item.render = (h, params)=>{
        return h('DatePicker', {
            props: {
                clearable: false,
                placeholder: '請選擇日期',
                value: params.row[params.column.key],
                type: 'date',
                format: 'yyyy-MM-dd',
                size: 'small'
            },
            on: {
                'on-change': (val)=>{
                    this.insideTableData[params.index][params.column.key] = val
                }
            }
        })
    }
    return item
}

4.表格中添加超連接

// 這裏的handleLink方法是在table組件中定義好的使用$emit讓父組件調用
supportLink(item){
    item.render = (h, params)=>{
        return h('a', {
            style: {
                textDecoration: 'underline'
            },
            on: {
                'click': ()=>{
                    this.handleLink(params.row)
                }
            }
        }, params.row[params.column.key])
    }
    return item
}
相關文章
相關標籤/搜索