简体   繁体   English

在 React JS 中:如何使用 MUI 中的“复制全部”图标从网格中复制所有文本

[英]In React JS : How to copy the all text from grid using "Copy All" icon from MUI

Hi everyone, In my grid, I added "Copy All" material UI icons.大家好,在我的网格中,我添加了“复制所有”材质 UI 图标。 My grid has 2 columns and 2 rows.我的网格有 2 列和 2 行。 I need to copy all column and rows value with copy all icon is clicked.我需要单击复制所有图标来复制所有列和行值。

Code:代码:

const list=[{
id:1,
name:'aaa'
},
{id:2,
name:'bbb'},
{id:3,
name:'ccb'},
{id:4,
name:'babb'},
]
<div>
<CopyAllIcon/>
<Grid container rowSpacing={2}>
{list.map((item)=>{
<Grid xs={6}>
{item.name})}}
</Grid>
</Grid>
</div>

I don't know how to copy the all value in the clipboard.我不知道如何复制剪贴板中的所有值。

My expected result is: Which clicks the copyAll icon, In my clipboard write as aaa, bbb, ccb, babb.我的预期结果是:单击 copyAll 图标,在我的剪贴板中写入 aaa、bbb、ccb、babb。

CopyAllIcon like all MaterialIcon icons is nothing but a dumb SVG-based image/icon. CopyAllIcon和所有 MaterialIcon 图标一样,不过是一个基于 SVG 的愚蠢图像/图标。 It's not some smart component that actually does any copying or writing to the clipboard.它不是一个真正对剪贴板进行任何复制或写入的智能组件。

In order to implement actual writing to the clipboard, you'd have to write your own function using the [Web API][1] and call that function from a button's onClick method.为了实现对剪贴板的实际写入,您必须使用 [Web API][1] 编写自己的 function 并从按钮的onClick方法调用 function That button may wrap the icon.该按钮可能会包裹图标。

// function to write to clipboard
const copyAll = () => {...}

<IconButton onClick={copyAll}>
  <CopyAlIcon/>
</IconButton> 


  [1]: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Interact_with_the_clipboard#writing_to_the_clipboard

I got a solution我有一个解决方案

function CopyAllTexts()
{
    const list=[{
    id:1,
    name:'aaa'
    },
    {id:2,
    name:'bbb'},
    {id:3,
    name:'ccb'},
    {id:4,
    name:'babb'},
    ];
    const handleCopyALLText=()=>{
    var copiedTextValues = list.map(function(item) {
      return item.name;
    });
    navigator.clipboard.writeText(copiedTextValues);
    };
    return(
    <div>
    <CopyAllIcon onClick={handleCopyALLText}/>
    <Grid container rowSpacing={2}>
    {list.map((item)=>{
    <Grid xs={6}>
    {item.name})}}
    </Grid>
    </Grid>
    </div>
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM