简体   繁体   English

Primefaces Draggable Drop Position

[英]Primefaces Draggable Drop Position

I need to get the drop position of a draggable panel. 我需要获得可拖动面板的下降位置。 But i cannot figure out how to do it. 但我无法弄清楚如何做到这一点。 I've tried to get the style but I've got unrelated information. 我试图得到这种风格,但我得到了无关的信息。

Here is my xhtml : 这是我的xhtml:

<h:form id="dnd">

   <p:panel id="draggable1" 
            style="z-index:1; width: 60px; height: 60px;">
       <h:outputText value="CAM-1" />
       <p:draggable for="draggable1" 
                    revert ="false"/>
   </p:panel>

   <p:panel id="droppable" 
            style="z-index:1; width: 600px; height: 600px;">
      <p:droppable for="droppable"> 
          <p:ajax listener="#{myBean.onDrop}" />
      </p:droppable>
   </p:panel>

</h:form>

Here is my backing bean : 这是我的支持bean:

public void onDrop(DragDropEvent dragDropEvent) {
    String dragId = dragDropEvent.getDragId();

    UIComponent draggedItem = FacesContext.getCurrentInstance().getViewRoot().findComponent(dragId);

    System.out.println(draggedItem.getClientId());
    Panel draggedPanel = (Panel) draggedItem;

    String style = draggedPanel.getStyle();
    System.out.println(style);

    String styleClass = draggedPanel.getStyleClass();
    System.out.println(styleClass);
}

Any help will be appreciated. 任何帮助将不胜感激。 Thanks in advance. 提前致谢。

PrimeFaces is using jQuery UI .droppable() , to get the position you should alter the event binding in PrimeFaces.widget.Droppable , this is done in bindDropListener . PrimeFaces使用jQuery UI .droppable()来获取你应该改变PrimeFaces.widget.Droppable事件绑定的位置,这是在bindDropListener完成的。

Adding a couple of request params to the original request would do it, here's an example: 在原始请求中添加几个请求参数就可以了,这是一个例子:

PrimeFaces.widget.Droppable.prototype.bindDropListener = function() {
   var _self = this;

   this.cfg.drop = function(event, ui) {
       if (_self.cfg.onDrop) {
           _self.cfg.onDrop.call(_self, event, ui);
       }
       if (_self.cfg.behaviors) {
           var dropBehavior = _self.cfg.behaviors['drop'];

           if (dropBehavior) {
               var ext = {
                    params: [
                       {name: _self.id + '_dragId', value: ui.draggable.attr('id')},
                       {name: _self.id + '_dropId', value: _self.cfg.target},
                       {name: ui.draggable.attr('id') + '_left', value: ui.position.left},
                       {name: ui.draggable.attr('id') + '_top', value: ui.position.top}
                    ]
                };

                dropBehavior.call(_self, ext);
           }
       }
    };
}

The change here is adding two more parameters to the ext params of the request, where the positions of the draggable item (left, top) are present. 此处的更改是向请求的ext参数添加另外两个参数,其中存在可拖动项目(左侧,顶部)的位置。

On the other side of the event onDrop(DragDropEvent dragDropEvent) , you can find them as request parameters as I have mentioned. onDrop(DragDropEvent dragDropEvent)事件的另一端,你可以找到它们作为我提到的请求参数。

public void onDrop(DragDropEvent dragDropEvent) {
    String dargId = dragDropEvent.getDragId();
    Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
    String left = params.get(dargId + "_left");
    String top = params.get(dargId + "_top");
}

Note: the ajax event should be present in p:droppable (as in the question) 注意:ajax事件应该出现在p:droppable (如问题所示)

You can find a small example on github , and a working demo . 你可以在github上找到一个小例子和一个工作演示

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

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