vue+iview 在data函数中为表格添加日期选择器
时间: 2023-12-10 15:39:16 浏览: 181
可以使用iview中的DatePicker组件,在data函数中定义一个日期变量,然后在表格的列定义中使用render函数来渲染DatePicker组件,具体代码如下:
```html
<template>
<div>
<Table :columns="columns" :data="tableData"></Table>
</div>
</template>
<script>
import { Table, DatePicker } from 'iview'
export default {
components: {
Table,
DatePicker
},
data() {
return {
tableData: [
{
name: '张三',
birthday: '1990-01-01'
},
{
name: '李四',
birthday: '1995-05-05'
}
],
columns: [
{
title: '姓名',
key: 'name'
},
{
title: '生日',
key: 'birthday',
render: (h, params) => {
return h(DatePicker, {
props: {
value: params.row.birthday,
type: 'date'
},
on: {
'on-change': (value) => {
params.row.birthday = value
}
}
})
}
}
]
}
}
}
</script>
```
在render函数中,DatePicker组件的props中设置了其类型为date,即显示日期选择器;同时将表格中每行数据的birthday字段绑定到DatePicker组件的value属性上,使得初始显示的日期为表格中对应行的生日;最后在DatePicker组件的on-change事件中,将选择的日期赋值给对应行的birthday字段,从而实现了表格中日期选择器的功能。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)