动态更改React-Table标题的data-prefix(仅在单元格中有效)

huangapple go评论59阅读模式
英文:

Dynamically change data-prefix of React-Table Header (Only works in Cell)

问题

我正在尝试在按钮点击时动态更改图标的 data-prefix 从 "far" 变为 "fas"。这将把文件夹图标更改为变暗的文件夹图标,以显示正在运行的过程。

当点击按钮时,我运行以下 JavaScript 代码:

document.getElementById("DownloadSelectedFolder").setAttribute("data-prefix", "fas");

这是表格和列:

const {
  getTableProps,
  getTableBodyProps,
  headerGroups,
  page,
  nextPage,
  previousPage,
  canNextPage,
  canPreviousPage,
  pageOptions,
  gotoPage,
  pageCount,
  setPageSize,
  state,
  rows,
  rowIndex,
  prepareRow,
  selectedFlatRows,
  onChangeSelectedRowsId,
  toggleAllRowsSelected,
} = useTable({
  columns,
  data: Documents,
  selectedRows: setSelectedRows,
  // Add this
  autoResetSelectedRows: false,
  autoResetSelectedCell: false,
  autoResetSelectedColumn: false,
},
useSortBy,
usePagination,
useRowSelect,
hooks => {
  hooks.visibleColumns.push(columns => [
    ...columns,
    {
      id: 'selection',
      Header: ({ getToggleAllRowsSelectedProps }) => (
        <div>
          <button
            className="border rounded p-2 mb-2"
            onClick={() => mergePDFs(selectedFlatRows.map(row => row.original))}
          >
            <i id="DownloadSelectedFolder" className="far fa-folder-open action mr-2" disabled></i>
          </button>
          <Checkbox {...getToggleAllRowsSelectedProps()} />
        </div>
      ),
      disableSortBy: true,
      Cell: ({ row }) => 
      <div>
        <Checkbox {...row.getToggleRowSelectedProps()} />
        <span onClick={() => openDocument(row.id)}>
          <i id={row.id} className="far fa-folder-open action mr-2" disabled></i>
        </span>
      </div>
    }
  ])
})

如果图标放在单元格(行)中,这段 JavaScript 代码可以正常工作。但如果将图标放在标题(列)中,它将不再起作用...我不确定为什么会这样。有什么建议吗?

英文:

I am trying to dynamically change the data-prefix of an icon from far to fas when a button is clicked. This will change the folder icon into a darken folder icon to show a process is running.

When the button is clicked I run this javascript:

document.getElementById(&quot;DownloadSelectedFolder&quot;).setAttribute(&quot;data-prefix&quot;,&quot;fas&quot;);

Here is the table and column:

		const {
getTableProps,
getTableBodyProps,
headerGroups,
page,
nextPage,
previousPage,
canNextPage,
canPreviousPage,
pageOptions,
gotoPage,
pageCount,
setPageSize,
state,
rows,
rowIndex,
prepareRow,
selectedFlatRows,
onChangeSelectedRowsId,
toggleAllRowsSelected,
} = useTable({
columns,
data: Documents,
selectedRows : setSelectedRows,
//Add this
autoResetSelectedRows: false,
autoResetSelectedCell: false,
autoResetSelectedColumn: false,
},
useSortBy, 
usePagination,
useRowSelect,
hooks =&gt; {
hooks.visibleColumns.push(columns =&gt; [
...columns,
{
id: &#39;selection&#39;,
Header: ({ getToggleAllRowsSelectedProps }) =&gt; (
&lt;div&gt; 
&lt;button 
className=&quot;border rounded p-2 mb-2&quot;
onClick={() =&gt;   mergePDFs(selectedFlatRows.map(row =&gt; row.original))}
&gt; 
&lt;i id=&quot;DownloadSelectedFolder&quot; className=&quot;far fa-folder-open action mr-2&quot; disabled&gt;&lt;/i&gt;
&lt;/button&gt;
&lt;Checkbox {...getToggleAllRowsSelectedProps()} /&gt;
&lt;/div&gt;
),
disableSortBy: true,
Cell: ({ row }) =&gt; 
&lt;div&gt;
&lt;Checkbox {...row.getToggleRowSelectedProps()} /&gt;
&lt;span  onClick={() =&gt; openDocument(row.id)}&gt;
&lt;i id={row.id} className=&quot;far fa-folder-open action mr-2&quot; disabled&gt;&lt;/i&gt;
&lt;/span&gt;
&lt;/div&gt;
}
])

This JavaScript code for the icon does work if I have the icon placed in the cell (rows). When it is placed in the header (column) it no longer works... I am not sure why this is the case. Any advice?

答案1

得分: 1

你可以使用useState创建一个自定义按钮组件,以在图标上添加/移除前缀。

const DownloadButton = ({ onClick }) => {
  const [selected, setSelected] = useState(false);

  return (
    <button
      className="border rounded p-2 mb-2"
      onClick={onClick}> 
      <i className={`${selected ? 'fas' : 'far'} fa-folder-open action mr-2`} disabled />
    </button>
  )
}

<DownloadButton onClick={() => mergePDFs(selectedFlatRows.map(row => row.original))} />
英文:

You could create a custom button component with useState to add/remove the prefix on the icon.

const DownloadButton = ({ onClick }) =&gt; {
  const [selected, setSelected] = useState(false);

  return (
    &lt;button
      className=&quot;border rounded p-2 mb-2&quot;
      onClick={onClick}&gt; 
      &lt;i className={`${selected ? &#39;fas&#39; : &#39;far&#39;} fa-folder-open action mr-2`} disabled /&gt;
    &lt;/button&gt;
  )
}

&lt;DownloadButton onClick={() =&gt; mergePDFs(selectedFlatRows.map(row =&gt; row.original))} /&gt;

huangapple
  • 本文由 发表于 2023年6月9日 05:54:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定