在iview的Table中添加Select(render)

首先對Render進行分析,在iview官方的文檔中,找到了table插入Button的例子:javascript

 

[javascript]  view plain  copy
 
  1. {  
  2. title: 'Action',  
  3. key: 'action',  
  4. width: 150,  
  5. align: 'center',  
  6. render: (h, params) => {  
  7.     return h('div', [  
  8.         h('Button', {  
  9.             props: {  
  10.                 type: 'primary',  
  11.                 size: 'small'  
  12.             },  
  13.             style: {  
  14.                 marginRight: '5px'  
  15.             },  
  16.             on: {  
  17.                 click: () => {  
  18.                     this.show(params.index)  
  19.                 }  
  20.             }  
  21.         }, 'View'),  
  22.         h('Button', {  
  23.             props: {  
  24.                 type: 'error',  
  25.                 size: 'small'  
  26.             },  
  27.             on: {  
  28.                 click: () => {  
  29.                     this.remove(params.index)  
  30.                 }  
  31.             }  
  32.         }, 'Delete')  
  33.     ]);  
  34. }  

這是Table的表頭定義中的一段,意思是建立兩個按鈕,一個名爲View,一個名爲Delete,在疑惑h是什麼的時候,看到網上一哥們的回答頓時茅廁頓開,問題地址,render參數中h能夠看作是 createElement。能夠看出上面的例子大概表現爲一個div中包含兩個Button,又根據生成Button的結構能夠把這段代碼簡化一下,寫爲:vue

 

 

[javascript]  view plain  copy
 
  1. render: (h, params) => {  
  2.     return h('Button', {  
  3.             props: {  
  4.                 type: 'primary',  
  5.                 size: 'small'  
  6.             },  
  7.             style: {  
  8.                 marginRight: '5px'  
  9.             },  
  10.             on: {  
  11.                 click: () => {  
  12.                     this.show(params.index)  
  13.                 }  
  14.             }  
  15.         }, 'View'),  
  16.     );  
  17. }  

在學vue的時候,有看到父組件和子組件之間的交互使用了props,咱們在iview的文檔中,看到Button的API包括type、size,由此可知,props能夠直接聲明子組件的API值內容,on中寫的天然就是它的觸發事件了。java

 

好,如今開始寫Table組件中的Select組件:segmentfault

 

[javascript]  view plain  copy
 
  1. render: (h, params) => {  
  2.     return h('Select', {  
  3.         props:{  
  4.             value: this.data[params.index].volumeType,  
  5.         },  
  6.         on: {  
  7.             'on-change':(event) => {  
  8.                 this.data[params.index].volumeType = event;  
  9.             }  
  10.         },  
  11.     },  
  12.     [  
  13.         h('Option',{  
  14.             props: {  
  15.                 value: '1'  
  16.             }  
  17.         },'option1'),  
  18.         h('Option',{  
  19.             props: {  
  20.                 value: '2'  
  21.             }  
  22.         },'option2')  
  23.     ]  
  24.     );  
  25. },  

能夠看到效果:數組

 

好,如今咱們實現了基本的渲染。如今咱們實現動態改變option的內容,與組件的data結合起來,畢竟當數據量大的時候,總不能一個一個的寫上去。iview

觀察render的第三個參數爲一個對象數組,咱們可不能夠使用便利數據數組的方式生成呢?(廢話)this

直接上代碼,在數組的地方寫入:spa

 

[javascript]  view plain  copy
 
  1. this.volumeTypes.map(function(type){  
  2.     return h('Option', {  
  3.         props: {value: type}  
  4.     }, type);  
  5. })  


其中,this.volumeTypes就是咱們的列數據,固然,這是最基本的綁定的寫法,若是想使用對象數組,自行研究,很easy的~.net

 

這是咱們的最終結果:對象

 

[javascript]  view plain  copy
 
  1. {  
  2.     title: '卷類型',  
  3.     key: 'volumeType',  
  4.     align: 'center',  
  5.     render: (h, params) => {  
  6.         return h('Select', {  
  7.             props:{  
  8.                 value: this.data[params.index].volumeType,  
  9.             },  
  10.             on: {  
  11.                 'on-change':(value) => {  
  12.                     this.data[params.index].volumeType = value;  
  13.                 }  
  14.             },  
  15.         },  
  16.         this.volumeTypes.map(function(type){  
  17.             return h('Option', {  
  18.                 props: {value: type}  
  19.             }, type);  
  20.         })  
  21.         );  
  22.     },  
  23. },    

end。

相關文章
相關標籤/搜索