【问题标题】:Saving Drag and Drop position primefaces保存拖放位置primefaces
【发布时间】:2014-03-16 00:33:09
【问题描述】:

美好的一天。我在这里有一个使用 primefaces 的拖放实现。这是用于可拖动的。

<p:dataGrid id="availableComputers" value="#{coltsysLab.computer}" var="computer" columns="3" emptyMessage="No Computers Added Yet">   
<p:column>  

    <p:graphicImage id="computer" value="http://localhost:8080/COLTSysResources/resources/images/#{computer.pic}" styleClass="computerImage" rendered="#{computer.status=='AVAILABLE'}"/>  

    <p:draggable for="computer" revert="true" scope="#{computer.status}" stack=".computerImage"/>

    <p:contextMenu for="computer">
        <p:menuitem value="Add Information" icon="ui-icon-plusthick" onclick="addInfoDlg.show();" action="#{coltsysLab.passComputerId(computer)}"/>
        <p:menuitem process="@this" value="Computer Information" icon="ui-icon-search" oncomplete="viewInfoDlg.show()" action="#{coltsysLab.passComputerId(computer)}"/>
    </p:contextMenu>

</p:column> 

这是用于可放置插槽的。

<p:dataGrid id="slots" value="#{coltsysLab.op}" var="com1" columns="#{coltsysLab.column}" >  
<p:outputPanel id="drop" styleClass="slot">
    <p:droppable for="drop" datasource=":organizeForm:availableComputers" tolerance="fit" scope="AVAILABLE">
        <p:ajax process="slots" listener="#{coltsysLab.onDrop}" update=":organizeForm:growl :organizeForm:selectedComputers"/>
    </p:droppable>
</p:outputPanel>

我能够通过支持 bean 上的 DragDropEvent 获取 dragId 和 dropId。但是我将如何保存每个包含可拖动的插槽的位置?我想把它保存到数据库中,这样当我打开页面时,它就会显示每个可拖动的保存位置。

--

我从kolossus的建议中添加了一个代码,我想获取被拖动对象的位置,但是这里输出的是null

String dropId = event.getDropId();
UIComponent theContainer = FacesContext.getCurrentInstance().getViewRoot().findComponent(":organizeForm:slots"); 
UIComponent theComponent = FacesContext.getCurrentInstance().getViewRoot().findComponent(":organizeForm:availableComputers:computer");
List<UIComponent> childComponents = theContainer.getChildren();

    for (UIComponent child : childComponents) {

        System.out.println(child.getClientId());

        if (dropId.equals(child.getClientId())) {

            GraphicImage gImage = (GraphicImage) theComponent;
            String currentPosition = gImage.getStyle();
            System.out.println(currentPosition);
        }
    }

当我使用浏览器的inspect元素时,我可以看到它的样式,我将如何获得值?

【问题讨论】:

    标签: java jsf primefaces


    【解决方案1】:

    页面元素的位置由style 属性表示。因此,在您的情况下,您只需要获取已删除组件的当前值。为此,您需要通过其 clientId 检索视图中的组件。不幸的是,JSF 没有提供一种简单的方法来获取这些信息(尽管通过 id 获取组件非常容易);我只知道蛮力方法:

    String dropId = dde.getDropId();  //get the currently dropped component
    UIComponent theContainer =  FacesContext.getCurrentInstance().getViewRoot().findComponent("slots"); //you need a reference to the drop container
    List<UIComponent> childComponents =  theContainer.getChildren(); //all dropped components are children of the datagrid
    
    for(UIComponent child: childComponents){
       if(dropId.equals(child.getClientId()));{
          GraphicImage gImage = (GraphicImage) theComponent;
          String currentPosition = gImage.getStyle();
       }
    }
    

    【讨论】:

    • 我猜组件是这个? UIComponent theComponent = FacesContext.getCurrentInstance().getViewRoot().findComponent("availableComputers:computer"); 但它在 getStyle 上返回 null
    • @Francis - 您可能应该在 drop ajax 侦听器的 process 属性中包含 slots 组件。如果您检查正在查看的页面的元素源,您会注意到样式属性已更新,但它仅发生在客户端; JSF 仍然需要了解样式的变化
    • 我添加了process="slots",但仍然为空
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-23
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多