JSF: Clear the DataTable when no records found? - jsf

I have a strange issue. I have a search criteria and i have a datatable which displays the results.
The search functionality works fine. Below is the issue.
I enter some search criteria i click on search button the records get displayed. Fine.
Again i enter some search criteria for which there are no records the datatable should get cleared.But the issue is the old record itself is getting displayed. What can be the problem
Below is my java code:
public void results(TestItem[] TestItemTOArray, TestForm TestForm, boolean isNewSearch, CarService carService) throws Exception {
if (viewBean instanceof TestViewBean) {
TestViewBean = (TestViewBean) viewBean;
}
TestRowForm[] TestRowFormArray = this.convertToTestRowForm(TestItemTOArray, TestForm, isNewSearch, carService);
if(TestRowFormArray.length > 0)
TestForm.setTestRowForm(TestRowFormArray);
if(TestRowFormArray!=null){
TestForm.setTestRowForm(TestRowFormArray);
TestForm.setTotalRows(TestRowFormArray.length);
}
}
JSF:
<rich:dataTable
value="#{testController.testForm.testRowForm}"
rows="#{testController.noOfRecordsToShow}" var="resultRow" noDataLabel="No Result Found"
headerClass="ColumnHeader" styleClass="DataTable" rowKeyVar="itc"
style="width:980px;" id="dtTable">

You should clean up data source for data table in case when result set is empty too.

Related

TestFx - How to test validation dialogs with no ids

I have an application with grid of records and button insert. After clicking insert, there is a form, where you fill in data and click Ok for adding new record to the grid. After clicking Ok, there is validation which fires dialog with error informations, if any of the text fields do not match validation rules. Is there any posible way to test text on the dialog with textFx, if the dialog has no id?
This is an example for Alert based dialog:
In your test:
alert_dialog_has_header_and_content(
"Removing 'Almaty' location", "Are you sure to remove this record?");
In you helper test class:
public void alert_dialog_has_header_and_content(final String expectedHeader, final String expectedContent) {
final javafx.stage.Stage actualAlertDialog = getTopModalStage();
assertNotNull(actualAlertDialog);
final DialogPane dialogPane = (DialogPane) actualAlertDialog.getScene().getRoot();
assertEquals(expectedHeader, dialogPane.getHeaderText());
assertEquals(expectedContent, dialogPane.getContentText());
}
private javafx.stage.Stage getTopModalStage() {
// Get a list of windows but ordered from top[0] to bottom[n] ones.
// It is needed to get the first found modal window.
final List<Window> allWindows = new ArrayList<>(robot.robotContext().getWindowFinder().listWindows());
Collections.reverse(allWindows);
return (javafx.stage.Stage) allWindows
.stream()
.filter(window -> window instanceof javafx.stage.Stage)
.filter(window -> ((javafx.stage.Stage) window).getModality() == Modality.APPLICATION_MODAL)
.findFirst()
.orElse(null);
}
I know this issue is a little old and probably got fixed, but for documentation purpose in case someone else look for a fix for an issue alike, I see dialog.getDialogPane() in Dialog documentation, which would help lookup for specific controls inside the pane. So further on #plaidshirt query, we could retrieve buttons and input fields with:
dialog.getDialogPane().lookupAll()
Then narrow that down to buttons and input fields for example.

Custom selector challenges

I have a custom screen with a multiple custom selectors, which change what they select based on dropdown lists.
The solution I implemented is shown in a previous case:
Dynamically changing PXSelector in Acumatica (thanks).
My challenge is twofold:
1.) If the dropdown selection is "No Lookup", then I want the PXSelector Attribute to essentially be removed - leaving just a text entry. Not sure if this is even possible...
2.) If one of the selectors (let's say Projects) is selected, I'd like the selection of the following selector (let's say Tasks) to filter based on the Project selected.
Thanks much...
1) I think the only way to do this is to create your own attribute.
Something like that:
public class PXSelectorTextEditAttribute : PXSelectorAttribute
{
bool selectorMode;
public PXSelectorTextEditAttribute(Type type, bool selectorOn):base(type)
{
selectorMode = selectorOn;
}
public override void FieldVerifying(PXCache sender, PXFieldVerifyingEventArgs e)
{
if(selectorMode)
base.FieldVerifying(sender, e);
}
public static void SwitchSelectorMode(PXSelectorTextEditAttribute attribute, bool onOff)
{
attribute.selectorMode = onOff;
}
}
You will be able to turn on and off the 'selector' part of the attribute. With the field verifying turned off you will be able to put any value to the field just like in simple TextEdit field. However, the lookup button in the right end of the field still will be visible. I have no idea how to hide it.
2) This behavior can be implemented easily. You will need something like that(example based on cashaccount):
[PXSelector(typeof(Search<CABankTran.tranID, Where<CABankTran.cashAccountID, Equal<Current<Filter.cashAccountID>>>>))]
If you want to see all records when the cashaccount is not defined then you just modify the where clause by adding Or<Current<Filter.cashAccountID>, isNull>
Also don't forget to add AutoRefresh="true" to the PXSelector in the aspx. Without it your selector will keep the list of the records untill you press refresh inside of it.

Adding p:column programatically with filterBy and sortBy attributes

I'm trying to create datatable column pragmatically ,using primeface 5.2 ,wildfly ,Mojarra 2.2.6
I have inherited from DataTable, and creating my own Columns,i want to set the sortBy,filterBy attributes using value expressions using the below code
public void AddCoulumn(String colName, String dbFieldName,
boolean Searchable) {
ValueExpression valExpr = null;
Column column = new Column();
valExpr = createValueExpression("#{obj." + dbFieldName + "}");
column.setValueExpression("filterBy", valExpr);
column.setValueExpression("sortBy", valExpr);
column.setWidth("100px");
column.setHeaderText(colName);
HtmlOutputText out = new HtmlOutputText();
out.setValueExpression("value", valExpr);
out.setStyle("color:red");
column.setField(dbFieldName);
this.getChildren().add(column);
}
Unfortunately when trying to filter results nothing occur and where trying to sort using any column i got this exception
Caused by: java.lang.NullPointerException
at org.primefaces.component.datatable.DataTable.findColumnInGroup(DataTable.java:905) [primefaces-5.2.jar:5.2]
at org.primefaces.component.datatable.DataTable.findColumn(DataTable.java:896) [primefaces-5.2.jar:5.2]
at org.primefaces.component.datatable.feature.SortFeature.decode(SortFeature.java:86) [primefaces-5.2.jar:5.2]
at org.primefaces.component.datatable.DataTableRenderer.decode(DataTableRenderer.java:62) [primefaces-5.2.jar:5.2]
at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:831) [jboss-jsf-api_2.2_spec-2.2.6.jar:2.2.6]
thanks in advance
I fixed this problem after debugging the PrimeFaces datatable.
I found that if you try to sort or filter using specific column
the datatable calls a findcolumn function which take string client id as a paramter: findcolumn(string client id). Inside this function a function called getcolumns() retrieves the table columns... If the client id is found in the list it returns the column but in this case it never finds a column because PrimeFaces/JSF changes the columns id on the postback since the whole view is restored. So the column will not be found anymore
To solve this problem explicitly set the column id
column.setId(colName);
It works fine now

NullPointerException when executing same action multiple times

In my xhtml page, I have a search button and a p:datatable to display the search results. I added a c:if condition to hide and display the datatable.
<c:if test="#{!search_bean.isResultList}">
In my managedBean, I created the flag isResultEmpty and set it to true in my doInit(). In the action of my search button (actSearchForData), I've set it to true if my List is not empty.
private String actSearchForData() throws Exception {
if(resultList.size > 0) {
isResultEmpty = false;
}
}
Now this works without any errors the first time I execute actSearchForData and my resultList is displayed. But when I try to run actSearchForData the second time, I encounter nullpointer exception. I've tried debugging by returning isResultEmpty = true after getting the resultList but this only causes my flag to always return isResultEmpty = true.
How can I execute my search function multiple times and display the results in the datatable without getting any nullpointer exceptions?
UPDATE: I've tried using render again this time for the flag like rendered="!#{search_bean.isResultEmpty}". I no longer get nullpointerexception and my result list count displays the correct number of results but my data table does not show.
The JSF way to control conditional rendering is to use the rendered attribute like
<p:dataTable id="..."
value="#{search_bean.resultList}"
var="..."
rendered="#{not empty search_bean.resultList}" />
This will not render the datatable if resultList is null or resultList.size() is 0.
you must be getting NullPointerException (maybe) - because your "resultList" is null and still you are executing "resultList.size" inside "if(..)" statement
try something like This...
private String actSearchForData() {
if(resultList==null || resultList.size>0) {
isResultEmpty=false;
}
}
This code executes "isResultEmpty=false" - if(resultList is null)
Hope this works for you...

NullPointerException while displaying the same page after some taks in jsf

First I want to tell :
I saw How to use JSF's h:selectBooleanCheckbox with h:dataTable to create one object per row?
and my problem is like same but not solved problem.
Following is my code :
*.xhtml
<rich:column>
<h:selectBooleanCheckbox id="resolveTxn" value="#{verifyTransactionBean.checked[verifyTxnList.id]}"/>
</rich:column>
.
.//Some code here
.
<h:commandButton value="Resolve" action="#{verifyTransactionBean.resolveToTxn}" styleClass="btn" />
and following is my code
private Map<Long, Boolean> checked = new HashMap<Long, Boolean>();
public void resolveToTxn() {
List<ToVerifyTxnDTO> list = new ArrayList<ToVerifyTxnDTO>();
for (ToVerifyTxnDTO dTO : toVerifyTxnDTOList) {
if(checked.get(dTO.getId())){//I got null pointer exception here
Long id=dTO.getId();
verifyTransactionSessionBeanLocal.updateResolvedflag(id);
}
}
}
What I want to do ?
I want to pass checked row id as parameter on following :
verifyTransactionSessionBeanLocal.updateResolvedflag(id);
And after clicking the button Resolve some operation should be done and refresh dataTable and display same page. Operation is done but getting null pointer exception while displaying (Reddering) the same page
Thanks
The if () test on a Boolean will throw NullPointerException if the Boolean itself is actually null instead of true or false. In other words, the checked.get(dTO.getId()) has unexpectedly returned null for some reason.
That can have several causes:
The JSF implementation hasn't properly updated the checked map with the value from <h:selectBooleanCheckbox> for some reason.
The EL implementation was using Boolean instead of boolean for some reason.
The toVerifyTxnDTOList has incompatibly changed during the process which caused that the currently iterated dTO isn't the one which was been displayed in the table at the moment checkboxes were clicked (perhaps because the bean is request scoped instead of view scoped).
The toVerifyTxnDTOList contains more items than present in the checked map because you're using pagination and displaying only a subset at once.
In any case, to fix the NullPointerException, just add an additional nullcheck:
Boolean dtoChecked = checked.get(dTO.getId());
if (dtoChecked != null && dtoChecked) {
// ...
}
This however doesn't fix the underlying cause of your problem. My best guess would be that the toVerifyTxnDTOList contains more items than actually being displayed in the table. Thus, it's one of the last mentioned two causes mentioned in the list. The first mentioned two causes are theoretically possible, but in practice I haven't seen this before.

Resources