{ title: '作者', dataIndex: 'author', filterDropdown: ({ setSelectedKeys, selectedKeys, confirm, clearFilters }) => ( <div style={{ padding: 8 }}> <Input placeholder="请输入作者" value={selectedKeys[0]} onChange={e => setSelectedKeys(e.target.value ? [e.target.value] : [])} onPressEnter={() => confirm()} /> <Button type="primary" onClick={() => confirm()} style={{ margin: '5px 8px' }}> 筛选 </Button> <Button onClick={() => clearFilters()}>重置</Button> </div> ), onFilterDropdownVisibleChange: visible => { if (visible) { // setTimeout(() => this.Input.select()) } }, onFilter: (value, record) => record.author.includes(value), }, 代码报错Cannot read properties of null (reading 'includes')
时间: 2024-02-14 22:05:10 浏览: 23
这个错误是因为 `record.author` 的值为 `null` 或 `undefined` 导致的。您需要在筛选之前进行一个非空判断,可以使用如下代码:
```
onFilter: (value, record) => record.author && record.author.includes(value)
```
这样,只有 `record.author` 不为 `null` 或 `undefined` 时才会执行 `record.author.includes(value)` 这个操作。
相关问题
<template> <div> <a-table :pagination="false" :columns="columns" :dataSource="dataSource"> <!-- //循环展示数据或input输入框 --> <template v-for="col in ['abbreviation', 'fullName', 'nodes']" :slot="col" slot-scope="text, record, index"> <div :key="col"> <a-input v-if="editableData[record.key]" v-model="editableData[record.key][col]" /> <template v-else>{{ text }}</template> </div> </template> <!-- //操作 --> <template slot="operation" slot-scope="text, record, index"> <span v-if="editableData[record.key]"> <a-icon type="check" @click="save(record.key)" /> </span> <span v-else> <a-icon type="delete" @click="deleteItem(record.key)" /> <a-icon type="edit" @click="edit(record.key)" /> <a-icon type="plus" v-if="index == dataSource.length - 1" @click="addItem(record.key)" /> </span> </template> </a-table> <div @click="Edats">编辑</div> </div> </template> <script> import { cloneDeep } from 'lodash' export default { data() { return { editableData: [], //正在编辑的数组 columns: [ { title: '简称', dataIndex: 'abbreviation', scopedSlots: { customRender: 'abbreviation', }, }, { title: '全称', dataIndex: 'fullName', scopedSlots: { customRender: 'fullName', }, }, { title: '来源', dataIndex: 'nodes', scopedSlots: { customRender: 'nodes', }, }, { title: '操作', dataIndex: 'operation', scopedSlots: { customRender: 'operation' }, }, ], //表格数据 dataSource: [ { key: 0, abbreviation: '简称1', fullName: '全称1', nodes: '来源1', }, { key: 1, abbreviation: '简称2', fullName: '全称2', 实现一键保存
要实现一键保存,可以在页面中添加一个按钮,然后在点击该按钮时,遍历 `editableData` 数组,将数组中每个元素的修改后的值更新到 `dataSource` 数组中对应的元素上。
具体实现步骤如下:
1. 在页面中添加一个“保存”按钮,绑定点击事件 `handleSave`。
2. 在 `handleSave` 方法中,遍历 `editableData` 数组,将每个元素的修改后的值更新到 `dataSource` 数组中对应的元素上。
3. 更新 `dataSource` 数组后,清空 `editableData` 数组,使得页面上的所有输入框都变成文本展示。
下面是代码示例:
```
<template>
<div>
<a-table :pagination="false" :columns="columns" :dataSource="dataSource">
<!-- //循环展示数据或input输入框 -->
<template v-for="col in ['abbreviation', 'fullName', 'nodes']" :slot="col" slot-scope="text, record, index">
<div :key="col">
<a-input v-if="editableData[record.key]" v-model="editableData[record.key][col]" />
<template v-else>{{ text }}</template>
</div>
</template>
<!-- //操作 -->
<template slot="operation" slot-scope="text, record, index">
<span v-if="editableData[record.key]">
<a-icon type="check" @click="save(record.key)" />
</span>
<span v-else>
<a-icon type="delete" @click="deleteItem(record.key)" />
<a-icon type="edit" @click="edit(record.key)" />
<a-icon type="plus" v-if="index == dataSource.length - 1" @click="addItem(record.key)" />
</span>
</template>
</a-table>
<div>
<a-button type="primary" @click="handleSave">保存</a-button>
<a-button type="default" @click="handleCancel">取消</a-button>
</div>
</div>
</template>
<script>
import { cloneDeep } from 'lodash'
export default {
data() {
return {
editableData: [], //正在编辑的数组
columns: [
{
title: '简称',
dataIndex: 'abbreviation',
scopedSlots: { customRender: 'abbreviation' },
},
{
title: '全称',
dataIndex: 'fullName',
scopedSlots: { customRender: 'fullName' },
},
{
title: '来源',
dataIndex: 'nodes',
scopedSlots: { customRender: 'nodes' },
},
{
title: '操作',
dataIndex: 'operation',
scopedSlots: { customRender: 'operation' },
},
],
//表格数据
dataSource: [
{
key: 0,
abbreviation: '简称1',
fullName: '全称1',
nodes: '来源1',
},
{
key: 1,
abbreviation: '简称2',
fullName: '全称2',
nodes: '来源2',
},
],
}
},
methods: {
edit(key) {
this.editableData[key] = cloneDeep(this.dataSource.find(item => item.key === key))
},
deleteItem(key) {
this.dataSource = this.dataSource.filter(item => item.key !== key)
},
addItem(key) {
const newKey = this.dataSource.reduce((max, item) => Math.max(item.key, max), -1) + 1
this.dataSource.push({ key: newKey, abbreviation: '', fullName: '', nodes: '' })
this.edit(newKey)
},
save(key) {
const index = this.dataSource.findIndex(item => item.key === key)
Object.assign(this.dataSource[index], this.editableData[key])
delete this.editableData[key]
},
handleSave() {
for (const key in this.editableData) {
this.save(key)
}
this.editableData = []
},
handleCancel() {
this.editableData = []
},
},
}
</script>
```
const columns: ProColumns<API.BatteryInformationControl>[] = [ { title: ( "截止日期" ), dataIndex: 'dataEndDate', fixed:'left', align: "center", width: 150, }, ]; return ( <div> <Space direction="vertical" size="large" style={{display: 'flex', marginTop: '1%'}}> <div> <span> 日期: </span> <DatePicker style={{width: '20%'}} onChange={onChange} disabledDate={disabledDate} defaultValue={dayjs()} /> </div> <ProTable<API.BatteryInformationControl, API.queryBatteryInformationControlParams & API.PageParams > headerTitle={'深圳市能源换电业务电池总体情况'} rowKey="" bordered // search={false} // scroll={{x:true}} scroll={{x: 'max-content', y: 700}} request={async (params) => { console.log(params); const body = { ...params, dataQueryTime: selectedDate } delete body.current; delete body.pageSize; console.log(body); const res = await queryBatteryInformationManagement(body); console.log(res); setSummaryData(res) return res }} options={{ // show: true, density: true, fullScreen: true, setting: true, } } columns={columns} summary={ () => ( <> <ProTable.Summary.Row className='total' style={{textAlign: 'center'}}> {/* 合计行内容 */} <ProTable.Summary.Cell colSpan={2} index={0} >总计</ProTable.Summary.Cell> <ProTable.Summary.Cell index={2}>{summaryData && summaryData.total}</ProTable.Summary.Cell> </ProTable.Summary.Row> </> )} /> </Space> </div> )怎么把protable的截止时间搜索框的值改为一个时间选择器
要将ProTable的截止时间搜索框的值改为一个时间选择器,您可以按照以下步骤进行操作:
1. 修改`columns`数组,将截止日期的列定义中的`dataIndex`字段更改为一个唯一的键,例如`'dataEndDate'`改为`'endDate'`。
```jsx
const columns: ProColumns<API.BatteryInformationControl>[] = [
{
title: "截止日期",
dataIndex: 'endDate', // 修改dataIndex字段为endDate
fixed: 'left',
align: "center",
width: 150,
},
// 其他列定义
];
```
2. 在组件中创建一个新的状态变量来存储截止时间的值。例如,您可以使用useState钩子创建一个名为`selectedEndDate`的状态变量,并在时间选择器的onChange事件处理程序中更新它。
```jsx
const [selectedEndDate, setSelectedEndDate] = useState(dayjs());
const onEndDateChange = (date) => {
setSelectedEndDate(date);
};
```
3. 将时间选择器添加到组件的JSX中,并将其与新的状态变量和onChange事件处理程序关联。
```jsx
<DatePicker
style={{ width: '20%' }}
onChange={onEndDateChange}
defaultValue={selectedEndDate}
/>
```
4. 在ProTable的request属性中,将`selectedEndDate`的值添加到请求参数对象中的相应字段中。根据您提供的代码,可以将其添加到`dataQueryTime`字段中。
```jsx
request={async (params) => {
const body = {
...params,
dataQueryTime: selectedEndDate.format('YYYY-MM-DD'), // 将selectedEndDate的值添加到请求参数中
};
delete body.current;
delete body.pageSize;
// 发起请求并返回结果
const res = await queryBatteryInformationManagement(body);
setSummaryData(res);
return res;
}}
```
最终的代码示例:
```jsx
import { useState } from 'react';
import { DatePicker, ProTable } from 'antd';
const YourComponent = () => {
const [selectedEndDate, setSelectedEndDate] = useState(dayjs());
const onEndDateChange = (date) => {
setSelectedEndDate(date);
};
const columns: ProColumns<API.BatteryInformationControl>[] = [
{
title: "截止日期",
dataIndex: 'endDate',
fixed: 'left',
align: "center",
width: 150,
},
// 其他列定义
];
return (
<div>
<DatePicker
style={{ width: '20%' }}
onChange={onEndDateChange}
defaultValue={selectedEndDate}
/>
<ProTable
// ...其他属性
request={async (params) => {
const body = {
...params,
dataQueryTime: selectedEndDate.format('YYYY-MM-DD'),
};
delete body.current;
delete body.pageSize;
// 发起请求并返回结果
const res = await queryBatteryInformationManagement(body);
setSummaryData(res);
return res;
}}
>
{columns.map((column) => (
<ProTable.Column {...column} />
))}
</ProTable>
</div>
);
};
export default YourComponent;
```
请根据您的代码结构和需求进行相应的修改。希望对您有所帮助!