gpt4 book ai didi

javascript - 在表中单击时调用方法并传递链接值?

转载 作者:行者123 更新时间:2023-11-30 19:56:43 25 4
gpt4 key购买 nike

我有一个自定义表格组件,并希望在单击时传递表格元素的值。问题是我正在使用的自定义表格组件正在触发 react 路由器,我不确定如何将 onClick 事件处理程序分配给表格中的“链接”元素。

这里是我为链接赋值的地方,也是我想调用我的方法 (getPreauthorizedLink) 的地方:

fetch(config.api.urlFor('xxx'))
.then((response) => response.json())
.then((data) => {

data.array.forEach(element => {
element.link = (<a href={element.link}>Report</a>)
})

this.setState({reportJSON: data.array || [], tableIsBusy: false})

})
.catch((err) => _self.setState({tableIsBusy: false }));

这就是我尝试实现推荐解决方案之一的方式:

   fetch(config.api.urlFor('xxx'))
.then((response) => response.json())
.then((data) => {

data.array.forEach(element => {
element.link = (<a onClick={this.getPreauthorizedLink(element.link)}>Report</a>)
})

this.setState({reportJSON: data.array || [], tableIsBusy: false})

})
.catch((err) => _self.setState({tableIsBusy: false }));

这是我的表格实现:

<div className="table-responsive">
<Busy isBusy={this.state.tableIsBusy}>
<Table
headers = {[
'Date',
'Title',
'Link'
]}
data = {reportJSON}
/>
</Busy>
</div>

这是我的自定义表格组件:

class Table extends React.Component {
componentWillMount() {
this.componentWillReceiveProps(this.props);
}

componentWillReceiveProps(nextProps) {
const idColumn = nextProps.idColumn || '',
labelColumn = nextProps.labelColumn || '',
headers = nextProps.headers || [],
data = nextProps.data || {},
path = nextProps.path || '',
linkPath = nextProps.linkPath || '',
params = nextProps.params || {},
page = parseInt(nextProps.page || 1),
pageSize = parseInt(nextProps.pageSize || 10),
totalRecords = parseInt(nextProps.totalRecords || 0),
orderByColumns = nextProps.orderByColumns || [],
orderBy = nextProps.orderBy || [],
dir = nextProps.dir === 'desc' ? 'desc' : 'asc',
visible = parseInt(nextProps.visible || 7);

this.setState({
idColumn: idColumn,
labelColumn: labelColumn,
headers: headers.map(v => ({label : _.get(v, "label", v), tooltip : _.get(v, "tooltip", _.get(v, "label", v)) }) ),
data: data,
path: path,
linkPath: linkPath,
params: params,
page: page,
pageSize: pageSize,
totalRecords: totalRecords,
orderByColumns: orderByColumns,
orderBy: orderBy,
dir: dir,
visible: visible
});
}

render() {
let { idColumn, labelColumn, headers, data, path, linkPath, params, page, pageSize, totalRecords, orderByColumns, orderBy, dir, visible } = this.state;
const columns = data && data.length ? Object.keys(data[0]) : [];

orderByColumns = orderByColumns.length ? orderByColumns : columns.filter((col) => col !== idColumn);

const isDataToDisplay = Boolean(data.length > 0);


return (
<React.Fragment>
{
isDataToDisplay
? (
<table className="table table-striped">
<thead>
<tr>
{columns.filter((col) => col !== idColumn).map((col, i) =>
path && path.indexOf(':orderBy') > -1 && path.indexOf(':dir') > -1 && orderByColumns[i] ? (
<th key={i}><LinkToPath path={path} params={params} replace={{ orderBy: orderByColumns[i], dir: orderBy === orderByColumns[i] && dir === 'asc' ? 'desc' : 'asc' }}>{_.get(headers[i], "label") || col}</LinkToPath></th>
) : (

<th key={i}>
<Tooltip
placement="top"
trigger={['hover']}
overlay={<div style={{maxWidth: 300}}>{ _.get(headers[i], "tooltip", _.get(headers[i], "label", headers[i] || col))}</div>}
arrowContent={<div className="rc-tooltip-arrow-inner"></div>}
>
<span>{_.get(headers[i], "label") || col}</span>
</Tooltip>
</th>
)
)}
</tr>
</thead>
<tbody>
{data.map((row, i) =>
<tr key={i}>
{columns.filter((col) => col !== idColumn).map((col) => ({ key: col, value: row[col] })).map((data, j) =>
data.key === labelColumn && linkPath.indexOf(`:${data.key}`) ? (
<td key={j}><Link to={`${linkPath.replace(`:${idColumn}`, row[idColumn])}`}>{data.value}</Link></td>
) : (
<td key={j}>{data.value}</td>
)
)}
</tr>
)}
</tbody>
</table>
)
: (
"None"
)
}
<PaginatedLinks onPageChanged={this.props.onPageChanged} path={path} params={params} page={page} pageSize={pageSize} totalRecords={totalRecords} visible={visible} />
</React.Fragment>
);
}
}

export default Table;

如何在单击“链接”元素时调用方法 (getPreauthorizedLink) 并将该表元素的值传递给表?

最佳答案

应该是这样的

const clickMethodWithValue = val => () => { clickMethod(val); }

<Link onClick={clickMethodWithValue(data.value)} ... >{data.value}</Link>

关于javascript - 在表中单击时调用方法并传递链接值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53909395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com