Primefaces Draggable Drop Position - jsf

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 :
<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 :
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.
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.
On the other side of the event onDrop(DragDropEvent dragDropEvent), you can find them as request parameters as I have mentioned.
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)
You can find a small example on github, and a working demo.

Related

p:autoComplete does not work with single char

I have a page with two inputtext (I'll name them "name" and "id"): one that compares the text entered with a string and another one that compares it with an int converted to string.
Now the problem is this: if I have an "id" == 1 and type 1 into the inputtext, the autocomplete shows only results with two or more digits/chars (so 11,31,117 etc but not 1)...
This is the html:
<p:autoComplete id="CustomerId" value="#{myBean.CustomerBean.id}"
completeMethod="#{myBean.autoCompleteId}"
maxResults="10">
<p:ajax event="itemSelect" listener="#{myBean.selectCustomerById}"
update="resultMessage name idComp table newAssociation" />
</p:autoComplete>
And this is the autocomplete method:
public List<String> autoCompleteId(String query) {
CustomerList = myService.selectByFilters(CustomerBean);
setAcList(new ArrayList<String>());
for (int i = 0; i < CustomerList.size(); i++) {
CustomerAnag tip = CustomerList .get(i);
if(String.valueOf(tip.getId()).contains(query) || String.valueOf(tip.getId()).equals(query)) {
acList.add(tip.getId().toString());
}
}
return acList;
}
What in the sweet heaven am I doing wrong ?!

Dynamic transition component VueJS 2

I would like to be able to switch between transition A and B dynamically
Parent component
<child></child>
Child component
Transition A
<transition name="fade">
<p v-if="show">hello</p>
</transition>
.fade-enter-active, .fade-leave-active {
transition: opacity .5s
}
.fade-enter, .fade-leave-to /* .fade-leave-active in <2.1.8 */ {
opacity: 0
}
Transition B (using animate.css library)
<transition
name="custom-classes-transition"
enter-active-class="animated tada"
leave-active-class="animated bounceOutRight"
>
<p v-if="show">hello</p>
</transition>
You can do the dynamic transition with properly assigning the enterClass and leaveClass. Here is the working demo: https://fiddle.jshell.net/j9h3Lmr5/1/
JS:
var vm = new Vue({
el: '#vue-instance',
data: {
show: true,
transitionType: "fade",
enterClass: "fade-enter",
leaveClass: "fade-enter-active"
},
methods: {
changeTransition() {
if (this.transitionType === "fade") {
this.transitionType = "custom-classes-transition"
this.enterClass = "animated slideInUp"
this.leaveClass = "animated slideOutDown"
} else {
this.transitionType = "fade"
this.enterClass = "fade-enter"
this.leaveClass = "fade-enter-active"
}
}
}
});

I need to click the button twice to make the calculation

I have a method that calculates a charge but you need to click the button twice for it to calculate
This is the method that fetches the data in the database and calculates
public Float findTarifaBandeira1() {
try {
//SQL
} catch (NoResultException ne) {
System.out.print("Tarifa não encontrada"); // Colocar messagesfaces
}
tarifaBand1 = (bandeira.getValorInicial() + ( bandeira.getTarifaCorrida().getValorKm() * km));
BigDecimal vt = new BigDecimal(tarifaBand1).setScale(2, RoundingMode.HALF_EVEN);
tarifaBand1 = vt.floatValue();
return tarifaBand1;
}
This method fires the click of the button calls other methods and the method above
I have a method that calculates a charge but you need to click the button twice for it to calculate
public void consultarBandeira() throws ParseException {
getData();
getHora();
convertDistance();
findTarifaBandeira1();
findTarifaBandeira2();
findTarifaVolumesDeMao ();
findTarifaVolumesNormais ();
findTarifaVolumesGrandes();
}
In my JSF page I use a remote command to call the above method
<p:commandButton value="#{msg['btn-search']}"
styleClass="btn btn-primary" onclick="findRoute()"/>
<p:remoteCommand actionListener="#{searchBean.consultarBandeira()}"
id="consultaValor" name="consultaValor"
update="bandeira1, bandeira2, nomeVPequenos,
descricaoVPequenos, valorVpequenos, nomeVNormais,
descricaoVNormais, valorVNormais, nomeVGrandes,
descricaoVGrandes, valorVGrandes"/>
findRoute() is a function that calls the JavaScript consultaValor component that triggers the method in my searchBean.
function findRoute(){
event.preventDefault();
var addressOrigin = origin;
var addressDestination = document.getElementById("addr").value +
document.getElementById("city").value;
var request = {
origin: addressOrigin,
destination: addressDestination,
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.metric
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
directionsDisplay.setPanel(document.getElementById("route"));
var route = response.routes[0];
for (var i = 0; i < route.legs.length; i++){
routeSegment = i + 1;
distance = route.legs[i].distance.value;
}
document.getElementById("distance").value = distance;
}
});
consultaValor();
}
When you click the commandButton, it does a postback to the server twice: once for the remoteCommand and once for the button itself. This is most likely the cause of your problem.
Since you're essentially using the button for client-side action only, instruct your commandButton to not postback via onclick.
Also, since you're setting form data in the findRoute() javascript method, you'll need to make sure it is processed on the backend. The default value of process for remoteCommand is #this.
<p:commandButton value="#{msg['btn-search']}"
styleClass="btn btn-primary" onclick="findRoute(); return false"/>
<p:remoteCommand actionListener="#{searchBean.consultarBandeira()}"
id="consultaValor" name="consultaValor"
process="#form"
update="bandeira1, bandeira2, nomeVPequenos,
descricaoVPequenos, valorVpequenos, nomeVNormais,
descricaoVNormais, valorVNormais, nomeVGrandes,
descricaoVGrandes, valorVGrandes"/>
But if it were me, I'd simplify this by combining the logic into the commandbutton itself:
<p:commandButton value="#{msg['btn-search']}"
styleClass="btn btn-primary" onstart="findRoute()"
action="#{searchBean.consultarBandeira()}"
process="#form"
update="bandeira1, bandeira2, nomeVPequenos,
descricaoVPequenos, valorVpequenos, nomeVNormais,
descricaoVNormais, valorVNormais, nomeVGrandes,
descricaoVGrandes, valorVGrandes" />

How to save only one item of select many check box?

when I an offer create an oferta object (offer) it saves all of the items in the select many check box (this are location as per company selected(empresa)), not only the checked ones:
Thank you very much for any help you can provide.
<h:outputLabel value="#{bundle.CreateOfertaLabel_empresaidEmpresa}" for="empresaidEmpresa" />
<h:selectOneMenu id="empresaidEmpresa"
value="#{ofertaController.selected.empresaidEmpresa}"
title="#{bundle.CreateOfertaTitle_empresaidEmpresa}"
required="true"
requiredMessage="#{bundle.CreateOfertaRequiredMessage_empresaidEmpresa}">
<f:ajax event="valueChange" execute="empresaidEmpresa" render="ubicacionCollection" />
<f:selectItems value="#{empresaController.itemsAvailableSelectOne}"/>
</h:selectOneMenu>
<h:outputLabel value="#{bundle.CreateOfertaLabel_ubicacionCollection}" for="ubicacionCollection" />
<h:selectManyCheckbox id="ubicacionCollection"
value="#{ubicacionXEmpresa}"
title="#{bundle.CreateOfertaTitle_ubicacionCollection}" >
<f:converter id="ubicacionConverter" converterId="ubicacionConverter"/>
<f:selectItems id="ubicacionCollectionItems"
value="#{ofertaController.selected.empresaidEmpresa.ubicacionCollection}"
var="ubicacionXEmpresa"
itemLabel="#{ubicacionXEmpresa.barrio}"
itemValue="#{ubicacionXEmpresa}"/>
</h:selectManyCheckbox>
Your correct the lines should be:
<h:outputLabel value="#{bundle.CreateOfertaLabel_ubicacionCollection}" for="ubicacionCollection" />
<h:selectManyCheckbox id="ubicacionCollection"
value="#{ofertaController.selected.ubicacionCollection}"
title="#{bundle.CreateOfertaTitle_ubicacionCollection}" >
<f:converter id="ubicacionConverter" converterId="ubicacionConverter"/>
<f:selectItems id="ubicacionCollectionItems"
value="#{ofertaController.selected.empresaidEmpresa.ubicacionCollection}"
var="ubicacionXEmpresa"
itemLabel="#{ubicacionXEmpresa.barrio}"
itemValue="#{ubicacionXEmpresa}"/>
</h:selectManyCheckbox>
But know faces show me this on creation of an offer
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '32-1' for key 'PRIMARY' Error Code: 1062 Call: INSERT INTO oferta_has_ubicacion (ubicacion_idUbicacion, oferta_idOferta) VALUES (?, ?) bind => [2 parameters bound] Query: DataModifyQuery(name="ubicacionCollection" sql="INSERT INTO oferta_has_ubicacion (ubicacion_idUbicacion, oferta_idOferta) VALUES (?, ?)")
Here is my create method:
public void create(Oferta oferta) {
if (oferta.getUbicacionCollection() == null) {
oferta.setUbicacionCollection(new ArrayList<Ubicacion>());
}
if (oferta.getEmpresaCollection() == null) {
oferta.setEmpresaCollection(new ArrayList<Empresa>());
}
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Empresa empresaidEmpresa = oferta.getEmpresaidEmpresa();
if (empresaidEmpresa != null) {
empresaidEmpresa = em.getReference(empresaidEmpresa.getClass(), empresaidEmpresa.getIdEmpresa());
oferta.setEmpresaidEmpresa(empresaidEmpresa);
}
Collection<Ubicacion> attachedUbicacionCollection = new ArrayList<Ubicacion>();
for (Ubicacion ubicacionCollectionUbicacionToAttach : oferta.getUbicacionCollection()) {
ubicacionCollectionUbicacionToAttach = em.getReference(ubicacionCollectionUbicacionToAttach.getClass(), ubicacionCollectionUbicacionToAttach.getIdUbicacion());
attachedUbicacionCollection.add(ubicacionCollectionUbicacionToAttach);
}
oferta.setUbicacionCollection(attachedUbicacionCollection);
Collection<Empresa> attachedEmpresaCollection = new ArrayList<Empresa>();
for (Empresa empresaCollectionEmpresaToAttach : oferta.getEmpresaCollection()) {
empresaCollectionEmpresaToAttach = em.getReference(empresaCollectionEmpresaToAttach.getClass(), empresaCollectionEmpresaToAttach.getIdEmpresa());
attachedEmpresaCollection.add(empresaCollectionEmpresaToAttach);
}
oferta.setEmpresaCollection(attachedEmpresaCollection);
em.persist(oferta);
if (empresaidEmpresa != null) {
empresaidEmpresa.getOfertaCollection().add(oferta);
empresaidEmpresa = em.merge(empresaidEmpresa);
}
for (Ubicacion ubicacionCollectionUbicacion : oferta.getUbicacionCollection()) {
ubicacionCollectionUbicacion.getOfertaCollection().add(oferta);
ubicacionCollectionUbicacion = em.merge(ubicacionCollectionUbicacion);
}
for (Empresa empresaCollectionEmpresa : oferta.getEmpresaCollection()) {
empresaCollectionEmpresa.getOfertaCollection().add(oferta);
empresaCollectionEmpresa = em.merge(empresaCollectionEmpresa);
}
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
value="#{ofertaController.selected.ubicacionCollection}"
The other was a many to many relationship problem jpa issue

h:commandButton/h:commandLink does not work on first click, works only on second click

We have an ajax navigation menu which updates a dynamic include. The include files have each their own forms.
<h:form>
<h:commandButton value="Add" action="#{navigator.setUrl('AddUser')}">
<f:ajax render=":propertiesArea" />
</h:commandButton>
</h:form>
<h:panelGroup id="propertiesArea" layout="block">
<ui:include src="#{navigator.selectedLevel.url}" />
</h:panelGroup>
It works correctly, but any command button in the include file doesn't work on first click. It works only on second click and forth.
I found this question commandButton/commandLink/ajax action/listener method not invoked or input value not updated and my problem is described in point 9.
I understand that I need to explicitly include the ID of the <h:form> in the include in the <f:ajax render> to solve it.
<f:ajax render=":propertiesArea :propertiesArea:someFormId" />
In my case, however, the form ID is not known beforehand. Also this form will not be available in the context initally.
Is there any solution to the above scenario?
You can use the following script to fix the Mojarra 2.0/2.1/2.2 bug (note: this doesn't manifest in MyFaces). This script will create the javax.faces.ViewState hidden field for forms which did not retrieve any view state after ajax update.
jsf.ajax.addOnEvent(function(data) {
if (data.status == "success") {
fixViewState(data.responseXML);
}
});
function fixViewState(responseXML) {
var viewState = getViewState(responseXML);
if (viewState) {
for (var i = 0; i < document.forms.length; i++) {
var form = document.forms[i];
if (form.method == "post") {
if (!hasViewState(form)) {
createViewState(form, viewState);
}
}
else { // PrimeFaces also adds them to GET forms!
removeViewState(form);
}
}
}
}
function getViewState(responseXML) {
var updates = responseXML.getElementsByTagName("update");
for (var i = 0; i < updates.length; i++) {
var update = updates[i];
if (update.getAttribute("id").match(/^([\w]+:)?javax\.faces\.ViewState(:[0-9]+)?$/)) {
return update.textContent || update.innerText;
}
}
return null;
}
function hasViewState(form) {
for (var i = 0; i < form.elements.length; i++) {
if (form.elements[i].name == "javax.faces.ViewState") {
return true;
}
}
return false;
}
function createViewState(form, viewState) {
var hidden;
try {
hidden = document.createElement("<input name='javax.faces.ViewState'>"); // IE6-8.
} catch(e) {
hidden = document.createElement("input");
hidden.setAttribute("name", "javax.faces.ViewState");
}
hidden.setAttribute("type", "hidden");
hidden.setAttribute("value", viewState);
hidden.setAttribute("autocomplete", "off");
form.appendChild(hidden);
}
function removeViewState(form) {
for (var i = 0; i < form.elements.length; i++) {
var element = form.elements[i];
if (element.name == "javax.faces.ViewState") {
element.parentNode.removeChild(element);
}
}
}
Just include it as <h:outputScript name="some.js" target="head"> inside the <h:body> of the error page. If you can't guarantee that the page in question uses JSF <f:ajax>, which would trigger auto-inclusion of jsf.js, then you might want to add an additional if (typeof jsf !== 'undefined') check before jsf.ajax.addOnEvent() call, or to explicitly include it by
<h:outputScript library="javax.faces" name="jsf.js" target="head" />
Note that jsf.ajax.addOnEvent only covers standard JSF <f:ajax> and not e.g. PrimeFaces <p:ajax> or <p:commandXxx> as they use under the covers jQuery for the job. To cover PrimeFaces ajax requests as well, add the following:
$(document).ajaxComplete(function(event, xhr, options) {
if (typeof xhr.responseXML != 'undefined') { // It's undefined when plain $.ajax(), $.get(), etc is used instead of PrimeFaces ajax.
fixViewState(xhr.responseXML);
}
}
Update if you're using JSF utility library OmniFaces, it's good to know that the above has since 1.7 become part of OmniFaces. It's just a matter of declaring the following script in the <h:body>. See also the showcase.
<h:body>
<h:outputScript library="omnifaces" name="fixviewstate.js" target="head" />
...
</h:body>
Thanks to BalusC since his answer is really great (as usual :) ). But I have to add that this approach does not work for ajax requests coming from RichFaces 4. They have several issues with ajax and one of them is that the JSF-ajax-handlers are not being invoked. Thus, when doing a rerender on some container holding a form using RichFaces-components, the fixViewState-function is not called and the ViewState is missing then.
In the RichFaces Component Reference, they state how to register callbacks for "their" ajax-requests (in fact they're utilizing jQuery to hook on all ajax-requests).
But using this, I was not able to get the ajax-response which is used by BalusC's script above to get the ViewState.
So based on BalusC's fix, i worked out a very similar one. My script saves all ViewState-values of all forms on the current page in a map before the ajax-request is being processed by the browser. After the update of the DOM, I try to restore all ViewStates which have been saved before (for all forms which are missing the ViewState now).
Move on:
jQuery(document).ready(function() {
jQuery(document).on("ajaxbeforedomupdate", function(args) {
// the callback will be triggered for each received JSF AJAX for the current page
// store the current view-states of all forms in a map
storeViewStates(args.currentTarget.forms);
});
jQuery(document).on("ajaxcomplete", function(args) {
// the callback will be triggered for each completed JSF AJAX for the current page
// restore all view-states of all forms which do not have one
restoreViewStates(args.currentTarget.forms);
});
});
var storedFormViewStates = {};
function storeViewStates(forms) {
storedFormViewStates = {};
for (var formIndex = 0; formIndex < forms.length; formIndex++) {
var form = forms[formIndex];
var formId = form.getAttribute("id");
for (var formChildIndex = 0; formChildIndex < form.children.length; formChildIndex++) {
var formChild = form.children[formChildIndex];
if ((formChild.hasAttribute("name")) && (formChild.getAttribute("name").match(/^([\w]+:)?javax\.faces\.ViewState(:[0-9]+)?$/))) {
storedFormViewStates[formId] = formChild.value;
break;
}
}
}
}
function restoreViewStates(forms) {
for (var formIndexd = 0; formIndexd < forms.length; formIndexd++) {
var form = forms[formIndexd];
var formId = form.getAttribute("id");
var viewStateFound = false;
for (var formChildIndex = 0; formChildIndex < form.children.length; formChildIndex++) {
var formChild = form.children[formChildIndex];
if ((formChild.hasAttribute("name")) && (formChild.getAttribute("name").match(/^([\w]+:)?javax\.faces\.ViewState(:[0-9]+)?$/))) {
viewStateFound = true;
break;
}
}
if ((!viewStateFound) && (storedFormViewStates.hasOwnProperty(formId))) {
createViewState(form, storedFormViewStates[formId]);
}
}
}
function createViewState(form, viewState) {
var hidden;
try {
hidden = document.createElement("<input name='javax.faces.ViewState'>"); // IE6-8.
} catch(e) {
hidden = document.createElement("input");
hidden.setAttribute("name", "javax.faces.ViewState");
}
hidden.setAttribute("type", "hidden");
hidden.setAttribute("value", viewState);
hidden.setAttribute("autocomplete", "off");
form.appendChild(hidden);
}
Since I am not an JavaScript-expert, I guess that this may be improved further. But it definitely works on FF 17, Chromium 24, Chrome 12 and IE 11.
Two additional questions to this approach:
Is it feasible to use the same ViewState-value again? I.e. is JSF assigning the same ViewState-value to each form for every request/response? My approach is based on this assumption (and I have not found any related information).
Does someone expect any problems with this JavaScript-code or already ran into some using any browser?

Resources