gpt4 book ai didi

javascript - 如何使 React 表中的列数据可点击?

转载 作者:行者123 更新时间:2023-12-01 15:23:14 25 4
gpt4 key购买 nike

我正在使用 React Table (React Bootstrap Table-2) 在页面中显示一个表并使用来自数据库 API 的数据填充它。我想将其中一列中的值显示为链接(hrefs)。此特定列仅包含 URL。我想要实现的是,如果我点击每一行的 url(“显示报告”) - 它应该打开一个带有相应行 ID 的新选项卡。如何在 React Bootstrap 表 2 中实现这一点?
我试过了:

{
datafield : "report",
text : "Show report",
accessor : "link",
Cell : (e) => <a href={e.value}> {e.value} </a>
},

import React, { useState, useEffect } from "react";
import logo from "./logo.svg";
import "./App.css";
import axios from "axios";
import BootstrapTable from "react-bootstrap-table-next";
import paginationFactory from "react-bootstrap-table2-paginator";
import * as ReactBootstrap from "react-bootstrap";
import filterFactory, { textFilter } from "react-bootstrap-table2-filter";
import ToolkitProvider, { Search } from "react-bootstrap-table2-toolkit";

const App = () => {
const [list, setList] = useState([]);
const [loading, setLoading] = useState(false);
const { SearchBar } = Search;

const getListData = async () => {
try {
const data = await axios.get("http://localhost:4000/results");
console.log(data);
setList(data.data);
setLoading(true);
} catch (e) {
console.log(e);
}
};

const columns = [
{ dataField: "dept_name", text: "DepartMent" },
{ dataField: "Tr_type", text: "Transmission Type", sort: true },
{ dataField: "E_type", text: "Entry Type", sort: true },
{ dataField: "Msg_des", text: "Message Description", sort: true },
{ dataField: "cr_date", text: "Created Date", sort: true },
{ dataField: "ch", text: "Channel", sort: true },
{ dataField: "Del", text: "Delivered", sort: true },
{ dataField: "fl", text: "Failed", sort: true },
{
dataField: "report",
text: "Show Detailed Report",
},
];

useEffect(() => {
getListData();
}, []);

return (
<div className="App">
{loading ? (
<ToolkitProvider keyField="name" data={list} columns={columns} search>
{(props) => (
<div>
<div className="serSec">
<h3 className="hdrOne">Search:</h3>
<SearchBar {...props.searchProps} />
</div>

<div class="table-responsive">
<BootstrapTable
{...props.baseProps}
filter={filterFactory()}
pagination={paginationFactory()}

/>
</div>
</div>
)}
</ToolkitProvider>
) : (
<ReactBootstrap.Spinner animation="border" />
)}
</div>
);
};

export default App;

最佳答案

列数据添加如下:

{
dataField : "report",
text : "Show report",,
formatter: (cell, row) => <a href={cell}> {cell} </a>
}

关于javascript - 如何使 React 表中的列数据可点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63224741/

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