大家好。这里有一个使用素面的拖放实现。
<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元素,我可以看到它的样式,我将如何能够得到的值?
页面元素的位置由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();
}
}