Copy text to clipboard from jsf / primefaces

2019-08-08 18:54发布

问题:

I am using https://github.com/patricklodder/jquery-zclip to implement copy to clipboard function. it should copy some text, but for now i want it to be anything really... but its not working at all.

I included in head:

<h:outputScript name="js/jquery.zclip.min.js" library="test" />

        <script>
            $(document).ready(function(){
                $('a#copy').zclip({
                    path:'#{resource['/test/js/ZeroClipboard.swf']}',
                    copy:$('div#content').text()
                });
            });                
        </script>

and in JSF page i have an ordinary :

<a href="#" id="copy">COPY</a> 

there are no errors in Chrome console but clicking the link doesn't do anything too. I really have no idea why it's not working. Big thanks for any suggestion.

---------- EDIT

The datagrid:

<p:dataGrid var="item" columns="3" rows="9" value="#{pictureManagementBean.pictures}" id="gallery" paginator="true">  

<p:column>
    <p:panel header="#{item.pictureName}" style="text-align:center; width:230px;">  
         <h:panelGrid styleClass="shortLink"> 
             <p:graphicImage value="#{item.thumbnailDir}" width="200px" />                             
             <a href="#" id="copy#{item.idpicture}">COPY</a>   
             <p id="copy#{item.idpicture}">LOREM IPSUM</p>
             <p:commandLink value="Delete" action="#pictureManagementBean.removePicture(item.idpicture)}" update="@form"/>

</h:panelGrid></p:panel></p:column></p:dataGrid> 

回答1:

This works for me:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
   <h:head>
   </h:head>
   <h:body>
         <h:outputScript library="default" name="js/jquery-1.8.1.min.js"/>
         <h:outputScript library="default" name="js/jquery.zclip.min.js"/>
         <script type="text/javascript">
         $(document).ready(function(){

                $('a#copy-description').zclip({
                    path:"#{resource['default:js/ZeroClipboard.swf']}",
                    copy:$('p#description').text()
                });
         });
         </script>
         <a id="copy-description" href="#">Copy Description</a>
         <p id="description">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque volutpat venenatis erat 
            eu convallis. Phasellus ut purus dui, in tristique ligula. Fusce a sodales ipsum.
            Proin et commodo lacus. Morbi eget lectus ante, sed interdum orci.
            Nunc rutrum, enim in mattis bibendum, sapien ligula semper nisi, sed ullamcorper justo sem quis felis.
            Duis vehicula arcu non felis convallis eleifend.
         </p>
   </h:body>
</html>

where the folder structure is like this:

-->resources/default/js/jquery.zclip.min.js
-->resources/default/js/ZeroClipboard.swf