Primefaces Datatable header width:auto - jsf

I am developing a dynamic Datatable with Primefaces 5. I have set the table width to auto. This is really nice. My Column uses only the needed width, which is perfect. But I also use paginator and a Table header. So my table looks like this:
<p:dataTable var="row"
editingRow="#{row.status == CommonConstant.ROWSTATUS_EDIT}"
tableStyle="width:auto"
value="#{componentBean.componentData[componentId].lazyModel}"
id="#{componentId}"
editable="true" scrollable="false" scrollHeight="100%"
resizableColumns="true"
paginator="true"
paginatorPosition="top"
rowsPerPageTemplate="10 25 50 100"
lazy="true">
<f:facet name="header">
<p:outputPanel style="text-align:right">
<h:commandLink rendered="#{fn:indexOf(componentBean.componentData[componentId].loadedData.actions, CommonConstant.ACTIONTYPE_EXCEL) >= 0}" >
<img border="0" src="img/excel.png" width="24"/>
<p:dataExporter type="xls" target="#{componentId}" fileName="#{appDialogId}" />
</h:commandLink>
<h:commandLink rendered="#{fn:indexOf(componentBean.componentData[componentId].loadedData.actions, CommonConstant.ACTIONTYPE_PDF) >= 0}">
<img border="0" src="img/pdf.png" width="24"/>
<p:dataExporter type="pdf" target="#{componentId}" fileName="#{appDialogId}"/>
</h:commandLink>
</p:outputPanel>
<p:outputPanel>
<h:outputText value="#{componentBean.componentData[componentId].loadedData.appdialogid}" />
</p:outputPanel>
</f:facet>
...
</p:dataTable>
How can I force the Header and the paginator to have the same width than the table?

For plain data table try this:
.ui-datatable {
display: table;
}
.ui-datatable table {
table-layout: auto !important;
}
I haven't tried resizable or anything else yet.

I am assuming you are setting the auto width on the datatable as I can recreate your problem.
Try this:
set the style to min-width: $px on the div with the ui-datatable class to a value that makes your table happy
remove the auto width from the table and add table-layout: auto ! important to the table element so the colums still auto size somewhat but relative to the header.
Note: The width on the table element needs to remain as 100%, its default, so it fills the header.
The below is not really relevant but still 'good to know'.
All datatables in Primefaces 5 use table-layout: fixed. You can override to match the older Primefaces model using css.
.ui-datatable table, .ui-datatable-resizable table {
table-layout: auto !important;
}
.ui-datatable-scrollable table {
table-layout: fixed !important;
}
Scrollable data-tables need to remain as fixed.
It may help if you remove the outputPanels and use the data exporter facet for the links. If you prefer the links in the header put them in a span.
Priemfaces example
<f:facet name="{Exporters}">
<h:commandLink> ...
</h:commandLink> ...
</f:facet>

Enclose your datatable in a panelGrid
<p:panelGrid id="forautowidth" columns="1">
--- Data Table Here ---
</p:panelGrid>
This has a somewhat annoying box around the table, but that is a good deal better then the overshooting paginator. Plus it works well as the browser page size changes

Related

p:datatable - row editing - cell menu items not visible when selecting row to edit

I am new to JSF. I have a p:datatable in which I set to edit rows, but just editing one of the cells. I have the row selection icon, the one looking like a pencil, and then the icons for confirming the editing and for cancelling the editing. When I select the row to edit, the menu of the involved cell does not show the item values (in my case booleans - true/false). Even if, when I put the mouse over an item, this shows.
I don't get any warning/error/exception with that.
<p:column headerText="Available">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{device.available}" />
</f:facet>
<f:facet name="input">
<h:selectOneMenu value="#{device.available}" style="width:100%">
<f:selectItems value="#{dtDeviceView.availableValues}" var="bool_value" itemLabel="#{bool_value}" itemValue="#{bool_value}" />
</h:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
I'm Using JSF 2.2 (Mojarra 2.3.5) And PrimeFaces 6.2.
How can I solve it?
[SOLVED]
I just needed
<style type="text/css">
.black-font {
color: black;
}
</style>
in the head of my xhtml file and styleClass="#{'black-font'}" in my h:selectOneMenu tag:
<h:selectOneMenu value="#{device.available}" style="width:100%" styleClass="#{'black-font'}">

How can I change column width of panel grid in PrimeFaces

I am working with Java EE and PrimeFaces. How can I change the column width of a panel grid in PrimeFaces?
Is there an example ?
You can qualify your columns inside the panelGrid using columnClasses. The following code sets different width and stick the cells content aligned to the top-side.
<h:panelGrid columns="2" style="width: 100%" columnClasses="forty-percent top-alignment, sixty-percent top-alignment">
.forty-percent {
width: 40%;
}
.sixty-percent {
width: 60%;
}
.top-alignment {
vertical-align: top;
}
I had a similar problem and here is my solution:
<p:panelGrid style="width:100%"> # notice that I do not define columns attribute in p:panelGrid!!
<f:facet name="header"> # header
<p:row> # p:row and p:column are obligatory to use since we do not define columns attribute!
<p:column colspan="2"> # here I use colspan=2, coz I have 2 columns in the body
Title
</p:column>
</p:row>
</f:facet>
<p:row>
<p:column style="width:150px"> # define width of a column
Column 1 content
</p:column>
<p:column> # column 2 will fill the rest of the space
Column 2 content
</p:column>
</p:row>
<f:facet name="footer"> # similar as header
<p:row>
<p:column colspan="2">
Footer content
</p:column>
</p:row>
</f:facet>
</p:panelGrid>
So as you can see, the main difference is that you do not define columns attribute in p:panelGrid. In header and footer you have to use p:row and p:column, and in my case I also have to use colspan=2 since in body we have 2 columns.
Hope this helps ;)
Have you considered the style attribute ?
Example :
<p:panelGrid columns="2" style="width: 50px">
Otherwise, for columns :
<p:column style="width:50px">
Refer to this thread : how can I adjust width of <p:column> in <p:panelGrid>?

How do I conditionally color the background in a table cell?

I am rendering a table with p:dataTable (PrimeFaces) and what I want to do is color the background of cells depending on the value of their content. This is different from coloring a row or a column -- it is the individual cell.
First a CSS problem. If I do this:
<p:column headerText="xyzzy">
<div style="background-color: green">
<h:outputText value="#{rowVar.anumber}" >
<f:convertNumber groupingUsed="true" />
</h:outputText>
</div>
</p:column>
the background color just of the content gets set, not the whole cell. In other words the padding is still the default.
Second, I want to make the style string a variable expression. I can add a function to the backing bean, but how do I access the table content in the method? Will this work?
<div style="#{bean.computeCSS(rowVar.number}">
EDIT:
I figured out a way to do the conditional part, but I still need help with the CSS part. My solution looks like:
<p:column headerText="xyzzy">
<div class="#{rowVar.anumber gt 0 ? 'colored' : ''}">
<h:outputText value="#{rowVar.anumber}">
<f:convertNumber groupingUsed="true" />
</h:outputText>
</div>
</p:column>
Although I dislike getting to fancy in EL, this has the advantage of not needed a backing bean method.
However I still only get the background color set, not the whole cell.
You can add a css class to the row and to the column too, that identifies a cell.
Use the dataTable's rowStyleClass attribute (example).
If you want to color multiple rows:
<p:dataTable value="#{bean.rows}" var="rowVar"
rowStyleClass="#{rowVar.firstCol gt 0 ? 'firstColColored' : ''}
#{rowVar.secondCol gt 0 ? 'secondColColored' : ''}">
<p:column styleClass="firstCol">...
<p:column styleClass="secondCol">
css:
.firstColColored .firstCol {
background: pink;
}
how about adding padding to your class , with px or percents...
something like this
.colored{
background-color:yellow;
padding-top:25px;
padding-bottom:25px;
padding-right:50px;
padding-left:50px;
}

Text & Image Layout in JSF

i am trying to make image and text display at same line by using JSF tag. is there any way to do that? oringinal code is like following but image and text always displays in 2 lines.
<rich:modalPanel id="Busy" autosized="true" zindex="2000">
<h:outputText value="Submitting..."></h:outputText>
<h:graphicImage value="images/loading.gif" />
</rich:modalPanel>
You can try this
<rich:modalPanel id="Busy" autosized="true" zindex="2000">
<h:panelGrid columns="2">
<h:outputText value="Submitting..."></h:outputText>
<h:graphicImage value="images/loading.gif" />
</h:panelGrid>
</rich:modalPanel>
The will arrange all its child elements in a table, containing the specified number of columns (2 in this case).
h:panelGrid
The best solution is to define the line directly in the CSS properties:
<h:outputText value="Submitting..." styleClass="loading"/>
and in your CSS:
.loading {
background-image: url('images/loading.gif');
}
then, you will have to adapt your CSS class to display correctly your image.

How to make a label display in a single line in jsf

Can any one help me to display a label in a single line?
In my UI there is a field called check funding period
but it is getting displayed in 3 lines like:
check
funding
period
what can i do so that it will display like
check funding period (in single line)
in jsf?
Not really a JSF question, just a CSS question.
Ensure that the label gets the CSS style white-space:nowrap; either via the style or styleClass attributes.
Looks to me like you have your output in a column of a table and have not made sure the column is wide enough. Use the columnClasses attribute of the dataTable to specify a css column style and make sure it is wide enough for your output. ie:
<rich:dataTable id="curfDataTable"
columnClasses="column40percent,column20percent,column40percent"
rowClasses="rowFirst,rowSecond" value="#{accessCurfMBean.unowned}"
var="curf" styleClass="tableInfo">
Much the same for when using the panelGrid layout.
<h:panelGrid id="panel" columns="2" border="1" columnClasses="column40percent,column60percent">
and in your css:
.column20percent {
width: 20%;
}
.column40percent {
width: 40%;
}
.column60percent {
width: 60%;
}
What I usually do is I wrap them in a h:panelGrid tag like this:
<h:panelGrid columns="3">
<h:outputText value="check" />
<h:outputText value="funding" />
<h:outputText value="period" />
</h:panelGrid>
If the html output from this is too messy for you, then I would consider doing like this:
<h:outputText value="#{Messages.check Messages.funding Messages.period}" />
Or simply:
<h:outputText value="Check funding period" />
As I said in the comment tho it really depends on what tags your using and what your wrapping them in. This may just be a simple CSS problem.
Another possible easy solution for this problem is the <nobr> tag.
Put <nobr> before your label area, and </nobr> at the end of it..
<nobr>
<h:outputText value="check" />
<h:outputText value="funding" />
<h:outputText value="period" />
</nobr>
Your problem solved!! :-)

Resources