简体   繁体   中英

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. My grid has 2 columns and 2 rows. 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.

CopyAllIcon like all MaterialIcon icons is nothing but a dumb SVG-based image/icon. 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. 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>
    );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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