iview table表格中添加select选择器以及dropdown下拉菜单
1.需求
2.设计
- 查了查资料,就是在table的列里面,使用render函数
- 使那个可以伸展出另一个的选择框作为一个下拉菜单dropdown,若还是写为 select或者option是不能正常展示的
- 或者可以使用cascader级联选择(这个应该是挺好的,但是我还没有去尝试过)
3.实践
<Table ref="selection" @on-selection-change="onSelect" height="700" no-data-text="暂无数据" :row-class-name="rowClassName" :columns="columns4" :data="data1" highlight-row></Table>
{
title: '香蕉',
key: 'banana',
render: (h, params) => {
var data = this.data3;
return h('div', [
h(
"Select",
data.map(function (item) {
if (item.value !== 'three') {
return [h(
"Option",
{
props: {
value: item.value,
key: item.value
}
},item.label)]
}
else {
return h('Dropdown', {props: {trigger: "hover",placement: 'right-start'}},
[
h('DropdownItem',[
item.label,
h('Icon',
{
props: {
type: "ios-arrow-forward"
}
})
]) ,
h('DropdownMenu',{
slot:"list"
},
item.children.map(function (child) {
console.log(child)
return h('Option', {
props: {
value: child.value,
key: child.value
}
}, child.label)
})
)
])
}
})
)]);
}
}
data3: [
{
value: "one",
label: "一斤",
children: []
},
{
value: "three",
label: "三斤",
children: [
{
value: "divideOne",
label: "一份3斤"
},
{
value: "divideTwo",
label: "两份各1.5斤"
}
]
},
{
value: "five",
label: "五斤"
}
]
- 发现额外弹出的下拉菜单被遮挡,又要改css了
- 内嵌的css规定了它的宽度,我发现修改这个值是起作用的
- 不要去改变select框的宽度,而是去改变select的下拉框的宽度,否则会出现一些东西被覆盖的情况
.ivu-select-dropdown{
min-width: 182px!important;
}
- 效果图
- 反显的话需要在render函数中的select的props属性中指定value即可(value是和此demo中的value对应的,例如一斤是“one”,两份各1.5斤是"divideTwo")
"Select",{
props:{
value:'divideTwo'
}
},
4.总结
- render函数iview上没介绍太多,需要去vue的官方文档上去看
- render函数的子元素怎么用括号括起来的,我也不太了解,不过一定要括对,否则根本不是你想要的格式
- 用cascader应该会更好,更方便
- 本人写的不好或者写错的地方,希望大神可以指出来,因为我真是初学者
附
<style>
.ivu-table-wrapper {
border:none
}
.ivu-table td{
background-color: #182328;
color: #fff;
}
.ivu-table-row td {
color: #fff;
border:none
}
.ivu-table-header th{
color:#FFD3B4;
font-weight: bold;
background-color: #212c31;
border: none;
}
.ivu-table-stripe-even td{
background-color: #434343!important;
}
.ivu-table-stripe-odd td{
background-color: #282828!important;
}
.ivu-table-row-highlight td {
background-color: #d63333!important;
}
.ivu-table-row-hover td {
background-color: #d63333!important;
}
.ivu-select-dropdown{
min-width: 182px!important;
}
</style>
<template>
<div>
<Table ref="selection" @on-selection-change="onSelect" height="700" no-data-text="暂无数据" :row-class-name="rowClassName" :columns="columns4" :data="data1" highlight-row></Table>
<Button @click="handleSelectAll(true)">Set all selected</Button>
<Button @click="handleSelectAll(false)">Cancel all selected</Button>
</div>
</template>
<script>
export default {
data () {
return {
selecteds: [],
columns4: [
{
type: 'selection',
width: 60,
align: 'center'
},
{
title: '苹果',
key: 'apple'
},
{
title: '香蕉',
key: 'banana',
render: (h, params) => {
var data = this.data3;
return h('div', [
h(
"Select",{
props:{
value:'divideTwo'
}
},
data.map(function (item) {
if (item.value !== 'three') {
return [h(
"Option",
{
props: {
value: item.value,
key: item.value
}
},item.label)]
}
else {
return h('Dropdown', {props: {trigger: "hover",placement: 'right-start'}},
[
h('DropdownItem',[
item.label,
h('Icon',
{
props: {
type: "ios-arrow-forward"
}
})
]) ,
h('DropdownMenu',{
slot:"list"
},
item.children.map(function (child) {
console.log(child)
return h('Option', {
props: {
value: child.value,
key: child.value
}
}, child.label)
})
)
])
}
})
)]);
}
},
{
title: '橘子',
key: 'orange'
},
{
title: '西瓜',
key: 'watermelon'
},
{
title: '牛奶',
key: 'milk'
},
{
title: '面包',
key: 'Bread'
},
{
title: '盐',
key: 'salt'
},
{
title: '小麦',
key: 'wheat'
},
{
title: '大米',
key: 'rice'
},
{
title: '酱油',
key: 'soy'
},
{
title: '其他',
key: 'else'
}
],
data1: [
{
apple: 'John Brown',
banana: '18',
orange: 18,
watermelon: 'New York No. 1 Lake Park',
milk: '2016-10-03',
Bread: 'New York No. 1 Lake Park',
salt: '2016-10-03',
wheat: 'New York No. 1 Lake Park',
rice: '2016-10-03',
soy: 'New York No. 1 Lake Park',
else: '2016-10-03'
},{
apple: 'John Brown',
banana: '18',
orange: 18,
watermelon: 'New York No. 1 Lake Park',
milk: '2016-10-03',
Bread: 'New York No. 1 Lake Park',
salt: '2016-10-03',
wheat: 'New York No. 1 Lake Park',
rice: '2016-10-03',
soy: 'New York No. 1 Lake Park',
else: '2016-10-03'
},{
apple: 'John Brown',
banana: '18',
orange: 18,
watermelon: 'New York No. 1 Lake Park',
milk: '2016-10-03',
Bread: 'New York No. 1 Lake Park',
salt: '2016-10-03',
wheat: 'New York No. 1 Lake Park',
rice: '2016-10-03',
soy: 'New York No. 1 Lake Park',
else: '2016-10-03'
},{
apple: 'John Brown',
banana: '18',
orange: 18,
watermelon: 'New York No. 1 Lake Park',
milk: '2016-10-03',
Bread: 'New York No. 1 Lake Park',
salt: '2016-10-03',
wheat: 'New York No. 1 Lake Park',
rice: '2016-10-03',
soy: 'New York No. 1 Lake Park',
else: '2016-10-03',
_checked: true
},{
apple: 'John Brown',
banana: '18',
orange: 18,
watermelon: 'New York No. 1 Lake Park',
milk: '2016-10-03',
Bread: 'New York No. 1 Lake Park',
salt: '2016-10-03',
wheat: 'New York No. 1 Lake Park',
rice: '2016-10-03',
soy: 'New York No. 1 Lake Park',
else: '2016-10-03'
},{
apple: 'John Brown',
banana: '18',
orange: 18,
watermelon: 'New York No. 1 Lake Park',
milk: '2016-10-03',
Bread: 'New York No. 1 Lake Park',
salt: '2016-10-03',
wheat: 'New York No. 1 Lake Park',
rice: '2016-10-03',
soy: 'New York No. 1 Lake Park',
else: '2016-10-03'
},{
apple: 'John Brown',
banana: '18',
orange: 18,
watermelon: 'New York No. 1 Lake Park',
milk: '2016-10-03',
Bread: 'New York No. 1 Lake Park',
salt: '2016-10-03',
wheat: 'New York No. 1 Lake Park',
rice: '2016-10-03',
soy: 'New York No. 1 Lake Park',
else: '2016-10-03'
}
],
data3: [
{
value: "one",
label: "一斤",
children: []
},
{
value: "three",
label: "三斤",
children: [
{
value: "divideOne",
label: "一份3斤"
},
{
value: "divideTwo",
label: "两份各1.5斤"
}
]
},
{
value: "five",
label: "五斤"
}
]
}
},
methods: {
handleSelectAll (status) {
this.$refs.selection.selectAll(status);
console.log(this.selection)
},
rowClassName :function (row, index) {
if(index%2==0){
return 'ivu-table-stripe-even';
}
else return 'ivu-table-stripe-odd';
},
onSelect(selection,index){
this.selecteds = selection;
console.log(this.selecteds)
}
}
}
</script>