【问题标题】:How to select multiple row in Table row ?Using(ctrl+click) and (Shift+click) in react js如何在表格行中选择多行?在反应js中使用(ctrl+click)和(Shift+click)
【发布时间】:2022-02-11 16:33:10
【问题描述】:

请帮助我,当用户按下 ctrl + 左键单击表格行应该突出显示该行时,我不知道如何解决该问题,而在 shift + 左键单击它应该突出显示用户从 1 行单击的所有行数据到 2 最后一行(例如 react-Table 多行选择)。 不使用任何 3 方库。

代码:-

  const ClickHighlight = (event, ID, ChannelName) => {
        if (event.ctrlKey) {

        }

      if(event.shiftKey) {
           console.log("helloShift");
        }



    }




    return (
        <div className="mainContent">
            <div className="tableHeaderBody">
                <div className="TableText">PlayList</div>  <div className="ClossIcon"><FaCircle style={{ color: "#FC0000", width: "10px", height: "10px", alignItems: "right" }} /></div>
            </div>
            <div className="tableBody" >
                <table className="table" id="tableStudent" data-multiple-select-row="true"
                    data-click-to-select="true">
                    <Header
                        headers={headers}
                    />
                    <tbody>
                        {comments.map((comment) => {
                           
                            return (
                                <tr key={comment.idx} tabIndex={comment.idx} className="border_bottom"  onMouseDown={(e) => ClickHighlight(e, comment.ClipName, comment.ChannelName)}>
                                    <td style={{ color: "white", width: "200px" }}>
                                        <img src={`data:image/jpeg;base64,${base64}`} alt="Clip Thumbnail" width="50%" />
                                    </td>
                                    <td style={{ color: "white", width: "440px" }}>{comment.ClipName}</td>
                                    <td style={{ color: "white", width: "250px" }}>{comment.SlugName}</td>
                                    <td style={{ color: "white", width: "250px" }}>{comment.ChannelName}</td>

请帮忙。

【问题讨论】:

  • 您是否尝试过将突出显示的行(或其 ID)保持在状态?这样您就可以在 clickhandler 中添加和删除它们
  • 不 你能举个例子说明我该怎么做吗?请

标签: javascript node.js reactjs


【解决方案1】:

我假设您的组件是一个功能性组件,但希望这能让您了解如何去做。我还假设 comments 是您组件的道具:

const [highlightedRows, setHighlightedRows] = useState([]);

const ClickHighlight = (event, id, ChannelName) => {
  if (event.ctrlKey || event.shiftKey) {
    // add to highlighted
    setHighlightedRows((current) => {
      if (current.includes(id)) {
        // row is already highlighted. Unhighlight but keep the others
        return current.filter((entry) => entry !== id);
      } else {
        // add row to the current list of highlighted ones
        return [...current, id];
      }
    });
  } else {
    // highlight clicked row and de-highlight others
    setHighLightedRows([id]);
  }
}

现在,在您的返回语句中,您可以通过匹配注释的idx 来使用状态将行渲染为突出显示或不显示。在这里,我刚刚为每个突出显示的项目设置了黄色背景,但您可以随意设置样式。

<tbody>
  {comments.map((comment) => {
    const isHighlighted = highlightedRows.includes(comment.idx);
    // we can now conditionally render based on if this flag is true or not           
    return (
      <tr
        key={comment.idx}
        tabIndex={comment.idx}
        className="border_bottom"
        onMouseDown={(e) => ClickHighlight(e, comment.idx, comment.ChannelName)}
        style={ isHighlighted ? { backgroundColor: 'yellow' } : {}}
        >
         <td style={{ color: "white", width: "200px" }}>
           <img src={`data:image/jpeg;base64,${base64}`} alt="Clip Thumbnail" width="50%" />
         </td>
         <td style={{ color: "white", width: "440px" }}>{comment.ClipName}</td>
         <td style={{ color: "white", width: "250px" }}>{comment.SlugName}</td>
         <td style={{ color: "white", width: "250px" }}>{comment.ChannelName}</td>
         ...

【讨论】:

  • 谢谢,适用于 ctrl 你能告诉我吗,这可以让 shiftkey 突出显示所有行开始到结束 else if (event.shiftKey) { setHighlightedRows((current) => { if (current .includes(id)) { return current.filter((previousRow) => previousRow !== id); } else { previousRow.push(id); console.log(previousRow); return [...current,previousRow] ; });
  • 这是更复杂的功能,因为每一行都需要知道它在顺序中的位置,以便它可以正确更新状态。您的过滤器将不起作用,因为 previousRow 只会与 id 匹配一次
猜你喜欢
  • 2015-07-16
  • 2022-07-11
  • 1970-01-01
  • 2017-09-11
  • 1970-01-01
  • 2014-01-03
  • 2013-07-31
  • 2012-09-09
  • 2012-01-28
相关资源
最近更新 更多