[英]How to delete specific row table using an onClick function in DOM?
[英]How to select a row in a specific column without triggering the onClick function for that entire row in React Table
我已經設置了我的 React Table,當用戶單擊一行時,它們將被帶到另一個頁面,但是現在我在第一列中添加了一個復選框,允許用戶突出顯示該行,但是當我單擊復選框,它將用戶帶到另一個頁面,因為它會觸發單擊整行時調用的函數。
我試圖獲取列訪問器名稱,如果單擊的列是第一個,則不要將用戶路由到另一個頁面,但是我無法獲取有關所選列的信息。 我花了很多時間查看文檔,但無法弄清楚。
我真的很感激任何幫助,因為我通常很難掌握 React Table
記錄表.js
import React from "react";
import {
useTable,
useFlexLayout,
useFilters,
useGlobalFilter,
useRowSelect
} from "react-table";
import _ from "lodash";
// noinspection ES6CheckImport
import { useHistory } from "react-router-dom";
import Table from "react-bootstrap/Table";
// Define a default UI for filtering
function GlobalFilter({
preGlobalFilteredRows,
globalFilter,
setGlobalFilter
}) {
return (
<span>
<input
value={globalFilter || ""}
onChange={e => {
setGlobalFilter(e.target.value || undefined); // Set undefined to remove the filter entirely
}}
placeholder={" Search by any criteria"}
style={{
fontSize: "2.0rem",
width: "100%"
}}
/>
</span>
);
}
const IndeterminateCheckbox = React.forwardRef(
({ indeterminate, ...rest }, ref) => {
const defaultRef = React.useRef();
const resolvedRef = ref || defaultRef;
React.useEffect(() => {
resolvedRef.current.indeterminate = indeterminate;
}, [resolvedRef, indeterminate]);
return (
<>
<input type="checkbox" ref={resolvedRef} {...rest} />
</>
);
}
);
export const RecordTable = ({ forms }) => {
//TODO figure out why react-table crashes if forms is not set to data variable below first
const data = forms;
let history = useHistory();
const columns = React.useMemo(
() => [
{
Header: "Disclaimer Form Records",
columns: [
{
Header: "First Name",
{
Header: "Time In",
accessor: "timeIn"
},
{
Header: "Time Out",
accessor: "timeOut"
},
{
Header: "€ Price",
accessor: "totalGroupPrice",
disableFilters: true,
disableSortBy: true
}
]
}
],
[]
);
const defaultColumn = React.useMemo(
() => ({
//minWidth: 20,
maxWidth: 150,
width: 120
}),
[]
);
const rowOnClick = (rowObject, column) => {
console.log("rowObject.original: ", rowObject.original);
console.log("column.id: ", column);
history.push("/viewform", { ...rowObject.original });
};
// Use the state and functions returned from useTable to build your UI
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
state,
preGlobalFilteredRows,
setGlobalFilter,
setHiddenColumns,
} = useTable(
{
columns,
data,
defaultColumn
},
useFilters,
useGlobalFilter,
useFlexLayout,
useRowSelect,
hooks => {
hooks.visibleColumns.push(columns => [
// Let's make a column for selection
{
accessor: "active",
Header: "Active",
width: 50,
maxWidth: 50,
minWidth: 50,
Cell: ({ row }) => (
<div>
<IndeterminateCheckbox {...row.getToggleRowSelectedProps()} />
</div>
)
},
...columns
]);
}
);
// Render the UI for your table
return (
<div>
<div>Records: {rows.length}</div>
<GlobalFilter
preGlobalFilteredRows={preGlobalFilteredRows}
globalFilter={state.globalFilter}
setGlobalFilter={setGlobalFilter}
/>
<div
style={{
height: 500,
border: "2px solid grey",
borderRadius: "5px",
overflow: "scroll"
}}>
<Table striped bordered hover responsive={"md"} {...getTableProps()}>
<thead>
{headerGroups.map(headerGroup => (
<tr {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map(column => (
<th {...column.getHeaderProps()}>
{column.render("Header")}
</th>
))}
</tr>
))}
</thead>
<tbody {...getTableBodyProps()}>
{rows.map((row, column, i) => {
prepareRow(row);
return (
<tr
{...row.getRowProps({
onClick: () => rowOnClick(row, column)
})}>
{row.cells.map(cell => {
return (
<td {...cell.getCellProps()}>{cell.render("Cell")}</td>
);
})}
</tr>
);
})}
</tbody>
</Table>
</div>
</div>
);
};
您需要在復選框上設置onClick={e => e.stopPropagation()}
。
從技術上講,點擊發生在多個元素上。 復選框、單元格、行等。
在 JS 中,事件是在頂部元素上創建的,然后通過位於鼠標光標下的所有 DOM 元素傳播(“冒泡”)。 事件在不同的層中冒泡,所有元素都可能對其做出反應。 但是你確實想阻止它在你的情況下冒泡。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.