Datatable responsive in Bootstrap 4 Tab - datatable

I am develop a jsf application with a responsive datatable. It does not work fine in a real phone. When I touch and scroll up/down the table, it shows properly.
Here is the code for responsive:
<h:column a:data-priority="1">
<f:facet name="header">ID</f:facet>
#{_row.id}
</h:column>
<h:column a:data-priority="2">
<f:facet name="header">Số CMND</f:facet>
#{_row.code}
</h:column>
<h:column a:data-priority="2">
<f:facet name="header">Tên</f:facet>
#{_row.name}
</h:column>
<h:column a:data-priority="0">
<f:facet name="header">
<h:commandLink action="#{employeeEditBean.addDependent()}" value="Add" >
<f:ajax render="form#{tabname}" onevent="onEventAdd#{tabname}" />
</h:commandLink>
</f:facet>
<h:commandLink action="#{employeeEditBean.editEmployeeDependent(_row.id)}"
value="Edit" title="Hiệu chỉnh dòng"
>
<f:ajax render="form#{tabname}" onevent="onEventEdit#{tabname}" />
</h:commandLink>
<h:outputText value=" " escape="true" />
<h:commandLink action="#{employeeEditBean.editEmployeeDependent(_row.id)}"
styleClass="fa fa-trash" title="Xóa dòng" style="color: #e0a800 !important"
>
<f:ajax render="form#{tabname}Delete" onevent="onEventDelete#{tabname}" />
</h:commandLink>
</h:column>
Java script:
$('#tablexx').DataTable({
rowReorder: {
selector: 'td:nth-child(2)'
}, responsive: true
});
Here is the result:
I also put the app to my test server:
http://51.158.105.169:8080/hr
user:adam/pass:adam1
Please help me.
Thank you very much - VinhCC

I have the answer myself:
$(document).ready(function () {
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
e.target // activated tab
e.relatedTarget // previous tab
var table = $.fn.dataTable.fnTables(true);
if (table.length > 0) {
$(table).dataTable().fnAdjustColumnSizing();
}
});
});
Here is the result:

Related

In a Richfaces Datatable with clickable rows, I want one column to ignore the onClickEvent

In an application based on Seam 2.2.1, JSF 1.2, Richfaces 3.3.3, I have a datatable with clickable rows.
In the last column, I have an icon (trash). Clicking here should execute a different method (delete). The standard onRowClick event should not apply.
Is there any easy way to accomplish this?
<rich:panel headerClass="panelHeader" styleClass="panel">
<f:facet name="header">Header</f:facet>
<rich:dataTable id="myTable" value="#{myModel}" var="k">
<a4j:support event="onRowClick" action="#{myAction.selectItem(k, facesContext.viewRoot.viewId)}"
reRender="myTable"/>
<rich:column>
<f:facet name="header">
<h:outputText value="Normal Column - should be clickable"/>
</f:facet>
<h:outputText value="#{k.name}"/>
</rich:column>
<rich:column >
<f:facet name="header">
<h:outputText value="Only Click on Icon should react to Mouseclick"/>
</f:facet>
<s:graphicImage value="/img/bin_closed.gif">
<a4j:support event="onclick"
action="#{myAction.deleteItem(k)}"
reRender="myTable"
ajaxSingle="true" limitToList="true"
onsubmit="if(!confirm('Really?')) { return false; }" />/>
</s:graphicImage>
</rich:column>
</rich:dataTable>
</rich:panel>
I found the solution to my problem. For the column that contains the icon, I put the icon into another <a4j:outputPanel element like this: `
Now, only the action method is executed when clicking on the icon.
Here is the full solution:
<rich:panel headerClass="panelHeader" styleClass="panel">
<f:facet name="header">Header</f:facet>
<rich:dataTable id="myTable" value="#{myModel}" var="k">
<a4j:support event="onRowClick" action="#{myAction.selectItem(k, facesContext.viewRoot.viewId)}"
reRender="myTable"/>
<rich:column>
<f:facet name="header">
<h:outputText value="Normal Column - should be clickable"/>
</f:facet>
<h:outputText value="#{k.name}"/>
</rich:column>
<rich:column >
<f:facet name="header">
<h:outputText value="Only Click on Icon should react to Mouseclick"/>
</f:facet>
<a4j:outputPanel onclick="event.stopPropagation()">
<s:graphicImage value="/img/bin_closed.gif">
<a4j:support event="onclick"
action="#{myAction.deleteItem(k)}"
ajaxSingle="true" limitToList="true"
onsubmit="if(!confirm('Really?')) { return false; }" />/>
</s:graphicImage>
</a4j:outputPanel>
</rich:column>
</rich:dataTable>
</rich:panel>

primefaces commandButton "update" not working

I have this dialog:
<p:dialog
id="dlgComment"
widgetVar="dialogComentario"
modal="true"
header="#{messages.comentarios}"
width="600px"
resizable="false" showEffect="clip" hideEffect="clip">
<h:form id="formComentario">
<p:panelGrid columns="2" style="width: 100%">
<f:facet name="header">
<h:outputText value="#{monitorarEventoControlador.eventoSelecionado.titulo}"/>
</f:facet>
<h:outputText value="#{messages.inicio}" />
<h:outputText value="#{monitorarEventoControlador.eventoSelecionado.inicio}" />
<h:outputText value="#{messages.gravidade}" />
<h:outputText value="#{monitorarEventoControlador.eventoSelecionado.gravidade}" />
</p:panelGrid>
<p:dataTable id="tableComentario" var="comentario" value="#{monitorarEventoControlador.eventoSelecionado.getComentariosAsList()}" emptyMessage="#{messages.noRecordsFound}">
<f:facet name="header">
#{messages.comentarios}
</f:facet>
<p:column headerText="#{messages.dataHora}">
<h:outputText value="#{comentario.dataHora}">
<f:convertDateTime pattern="dd/MM/yyyy HH:mm" timeZone="#{monitorarEventoControlador.buscaTimeZone()}"/>
</h:outputText>
</p:column>
<p:column headerText="#{messages.usuario}">
<h:outputText value="#{comentario.usuario.orgao.sigla} - #{comentario.usuario.apelido}"/>
</p:column>
<p:column headerText="#{messages.texto}">
<h:outputText value="#{comentario.texto}" />
</p:column>
</p:dataTable>
<p:panelGrid columns="2" style="width: 100%">
<h:inputText value="#{monitorarEventoControlador.comentarioSelecionado.texto}" maxlength="80"/>
<p:commandButton value="#{messages.comentar}" actionListener="#{monitorarEventoControlador.adicionarComentario()}" icon="ui-icon-check" update="tableComentario" />
</p:panelGrid>
</h:form>
</p:dialog>
And the bean:
public void adicionarComentario() {
comentarioSelecionado.setDataHora(new Date());
comentarioSelecionado.setEvento(eventoSelecionado);
comentarioSelecionado.setUsuario(autenticador.getUsuarioCorrente());
todosOsComentarios.colocar(comentarioSelecionado);
notificarComentario(comentarioSelecionado);
eventoSelecionado.getComentarios().add(comentarioSelecionado);
todosOsEventos.colocar(eventoSelecionado);
iniciarComentario(eventoSelecionado);
}
private void notificarComentario(Comentario comentario) {
Notificacao n = new Notificacao();
n.setDataHora(comentario.getDataHora());
n.setDescricao(DateUtil.dataHoraFormatada(n.getDataHora()) + " - " + comentario.getUsuario().getOrgao().getSigla() + "(" + comentario.getUsuario().getApelido() + "): " + comentario.getTexto());
n.setComentario(true);
n.setInforme(comentario.getEvento().getInforme());
comentario.getEvento().getInforme().getNotificacoes().add(n);
}
public void iniciarComentario(Evento evento) {
comentarioSelecionado = new Comentario();
setEventoSelecionado(evento);
}
The commandButton should update a dataTable. I have just moved from Prime 3.5 to 5.1 and among other problems I managed to figure out, there is this one that's realy annoying. It was working on Prime 3.5.
To be more specific about what is going wrong here: The action is fired normaly, the data is inserted correctly, but the dataTable is no longer updated. It is the very same code I was using yesterday with Prime 3.5, but now, using Prime 5.1 the dataTable does not refresh anymore.
Any ideas ? Thanks
use : before id
Remember
if you make a dialog
Before dialog old form tag should be close
then dialog content should be placed into another form.
use :dialogid, :dialogFormId

p:commandLink in p:subTable header fires only for first p:subTable

I need to display a commandlink in the subTable header.The page renders correctly however only the first commandlink fires and action method gets invoked.All the subsequent commandlinks don't call the action method. I looked at the html source for the first two anchor elements. There is a slight difference in the generated ids. The first link's id has no digit after j_dt68 whereas the second link's id has j_dt68:0. Not sure if it is important or not. Please help!
First commandlink's html code:
<a id="center_content_form:equipment_tabview:equipment-table:0:j_idt68:equipment_details_link" href="#" class="ui-commandlink" onclick="PrimeFaces.ab({source:'center_content_form:equipment_tabview:equipment-table:0:j_idt68:equipment_details_link'});return false;">DGI5120_GI:SB-5120 | 06634CVJTKH5</a>
Second commandlink's html code:
<a id="center_content_form:equipment_tabview:equipment-table:1:j_idt68:0:equipment_details_link" href="#" class="ui-commandlink" onclick="PrimeFaces.ab({source:'center_content_form:equipment_tabview:equipment-table:1:j_idt68:0:equipment_details_link'});return false;">DAR502G_ARRIS:TM502G | 6A7BMU47P225361</a>
<p:dataTable id="equipment-table" styleClass="list-datatable"
var="equipment" value="#{equipmentBean.equipmentList}"
emptyMessage="#{equipment_msgs['equipment.table.emptyMessage']}">
<f:facet name="header">
<p:commandButton immediate="true" id="assign_equipment"
icon="add-sign" styleClass="table-header-button-class"
title="#{equipment_msgs['assign.equipment.tooltip.text']}"
action="#{equipmentBean.navigateToAssignEquipmentPage()}" />
<h:outputText
value="#{equipment_msgs['assign.equipment.button.label']}"
styleClass="table-header" />
</f:facet>
<p:column id="port_number"
headerText="#{equipment_msgs['port.number.column.label']}"
styleClass="col10 table_text" />
<p:column id="port_address"
headerText="#{equipment_msgs['address.column.label']}"
styleClass="co20 table_text" />
<p:column id="port_type"
headerText="#{equipment_msgs['type.column.label']}"
styleClass="col20 table_text" />
<p:column id="port_category"
headerText="#{equipment_msgs['port.category.column.label']}"
styleClass="col20 table_text" />
<p:column id="service_category"
headerText="#{equipment_msgs['service.category.column.label']}"
styleClass="col20 table_text" />
<p:column id="occurrence"
headerText="#{equipment_msgs['occurrence.column.label']}"
styleClass="col20 table_text" />
<p:column id="modem"
headerText="#{equipment_msgs['modem.column.label']}"
styleClass="col10 table_text" />
<p:subTable var="port" value="#{equipment.portList}">
<f:facet name="header">
<p:separator styleClass="header-separator" />
<p:commandLink id="equipment_details_link"
value="#{equipment.detailLinkValue}"
action="#{equipmentBean.navigateToEquipmentDetailsPage()}" >
<f:setPropertyActionListener value="#{equipment}"
target="#{equipmentBean.selectedEquipment}" />
</p:commandLink>
<p:separator styleClass="header-separator" />
</f:facet>
<p:column>
<p:commandLink immediate="true" id="equipment_port_details_link"
value="#{port.portNumber}"
action="#{equipmentBean.navigateToEquipmentPortDetailsPage(port.portNumber)}">
<f:setPropertyActionListener value="#{equipment}"
target="#{equipmentBean.selectedEquipment}" />
</p:commandLink>
</p:column>
<p:column>
<p:commandLink immediate="true" id="equipment_port_addressable_transaction_details_link1"
value="#{port.portAddress}"
action="#{equipmentBean.navigateToEquipmentAddressPortDetailsPage(port.portNumber)}">
<f:setPropertyActionListener value="#{equipment}"
target="#{equipmentBean.selectedEquipment}" />
</p:commandLink>
</p:column>
<p:column>#{port.portType}</p:column>
<p:column>#{port.portDetails.portCategory}</p:column>
<p:column>#{port.portDetails.serviceCategory}</p:column>
<p:column>#{port.portDetails.occurrence}</p:column>
<p:column>#{port.portDetails.modemText} </p:column>
</p:subTable>
</p:dataTable>
I made a workaround:
put a onclick attribute on your link/button (inside your subTable) to remove the additional digit from id and call PrimeFaces.ad(..) function
like this:
<script type="text/javascript">
function deleteMsg(id) {
var pattern = /(\w+:\w+:\d+:\w+)(:\d+)(:\w+)/g;
var newId = id;
if (pattern.test(id)) {
newId = id.replace(pattern, "$1$3");
}
PrimeFaces.ab({source:newId,update:'formTable'});
return false;
}
</script>
and on your button:
<p:commandButton onclick="return deleteMsg($(this).attr('id'))" actionListener="#{pSMsgController.removeMsg(_msg)}" icon="ui-icon-trash"/>
try this.
id replace js:
<script type="text/javascript">
function deleteMsg(link) {
var id = link.attr('id');
var pattern = /(\w+:\w+:\d+:\w+)(:\d+)(:\w+)/g;
var newId = id;
if (pattern.test(id)) {
newId = id.replace(pattern, "$1$3");
}
PrimeFaces.ab({source:newId,update:link.attr('id', newId)});
return false;
}
CommandLink in subtable header:
<p:commandLink onclick="return deleteMsg($(this));" ... >

Removing a single row from DataTable using Ajax

I have a JSF view that lists items in a collection in a Primefaces DataTable. The rightmost columns contain remove buttons. When a remove button is clicked, it is supposed to make an Ajax call, remove the corresponding item from the session variable Cart and update the view in-place. I would like the request and the view change to be as minimal as possible.
Here is what I have for this purpose:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Register user</title>
</h:head>
<h:body>
<f:view>
<h:form id="itemsForm">
<p:outputPanel id="items">
<p:dataTable value="#{cart.itemList}" var="item">
<p:column>
<f:facet name="header">
<h:outputText value="name" />
</f:facet>
<h:outputText value="#{item.product.description}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="quantity" />
</f:facet>
<h:outputText value="#{item.quantity}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="" />
</f:facet>
<p:commandButton icon="ui-icon-close" title="remove from cart">
<p:ajax listener="#{cart.removeItem}"
update="form:itemsForm"
process="#this" />
</p:commandButton>
</p:column>
<f:facet name="footer">
Total amount: ${cart.totalAmount}
</f:facet>
</p:dataTable>
</p:outputPanel>
</h:form>
</f:view>
</h:body>
</html>
Accordingly, I have the following method in Cart.java
public void removeItem() {
System.out.println("REMOVE REQUEST ARRIVED");
}
However, the removeItem method isn't even executing when I click a remove button.
So my questions are:
1) What is wrong with my Ajax call? What changes should I make to my XHTML?
2) How do I handle the request in the removeItem method and return a response?
3) How do I update the footer, which displays the totalAmount?
You can pass #{item} as a parameter of your method call in the actionListener.
Your .xhtml page should look like this:
<p:dataTable id="cartTable" value="#{cart.itemList}" var="item">
<p:column>
<f:facet name="header">
<h:outputText value="" />
</f:facet>
<p:commandButton icon="ui-icon-close" title="remove from cart"
actionListener="#{cart.removeItem(item)}" update="cartTable" />
</p:column>
</p:dataTable>
And this is the method removeItem of your ManagedBean:
#ManagedBean
#ViewScoped
public class Cart {
private List<Item> itemList;
public void removeItem(Item item) {
itemList.remove(item);
}
}
1) <p:commandButton uses ajax by default , so instead placing the p:ajax use the action or actionListener of the <p:commandButton
2) I would use the action of the button and return null
3) update="#form" should update the entire form and this will update the entire table
here an example of a working button (link) from my page , i used the f:setPropertyActionListener to "pass" some data to the delete method
<p:commandButton action="#{cart.removeItem}" icon="ui-icon-close" title="remove from cart" update="#form" process="#this" >
<f:setPropertyActionListener
target="#{cart.selectedItem}"
value="#{item}" />
</p:commandButton>
in your class add this
private Item selectedItem;
public Item getSelectedItem() {
return selectedItem;
}
public void setSelectedItem(Item selectedItem) {
this.selectedItem = selectedItem;
}

JSF- <f:ajax> can't render datatable

I have a datatable which has a "delete" button at each row,
and I want to re-render the table when the data at the row is deleted.
But I can not make f:ajax work properly.
Here is the datatable at facelet page:
<h:form>
<h:dataTable value="#{adminPanelBean.activitiesList}" var="activityItem" id="allActivities"
styleClass="datatable" cellpadding="2" cellspacing="4"
columnClasses="ColName,colType,colTime,colPlace,colSummary" >
<h:column>
<f:facet name="header">
<h:outputText value="Etkinlik"/>
</f:facet>
<h:outputText value="#{activityItem.name} "/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Türü"/>
</f:facet>
<h:outputText value="#{activityItem.type}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Tarih "/>
</f:facet>
<h:outputText value="#{activityItem.time}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Yer "/>
</f:facet>
<h:outputText value="#{activityItem.place}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Açıklama "/>
</f:facet>
<h:outputText value="#{activityItem.summary}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value=" "/>
</f:facet>
**<h:commandButton value="Delete activity" onclick="if(confirm(' Delete #{activityItem.name} ?'))return true; else return false;" action="#{adminPanelBean.deleteActivity(activityItem.id)}">
<f:ajax event="click" render="allActivities" execute="#all" immediate="true"/>
<f:param name="activityId" value ="#{activityItem.id}" />
</h:commandButton>**
</h:column>
</h:dataTable>
</h:form>
When I refresh the page, I see the row is deleted. But I want it to be done dynamically.
In the action method you need to remove the item from the list as well (and thus not only from the DB). I'd suggest to pass the whole item instead of only the ID so that you can utilize List#remove() method.
action="#{adminPanelBean.deleteActivity(activityItem)}">
So that you can do
public void deleteActivity(Activity activity) {
activitiesList.remove(activity);
// ...
}
Unrelated to the concrete problem, they way how you used confirm() is pretty clumsy.
onclick="if(confirm(' Delete #{activityItem.name} ?'))return true; else return false;"
That function returns a boolean already. You do not need to test the obtained boolean within an if in order to return another boolean. Just simplify it as follows:
onclick="return confirm('Delete #{activityItem.name}?')"

Resources