Skip to content

Commit

Permalink
Fix primefaces#7391: Datatable encode selected row keys
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware committed Jun 10, 2021
1 parent f13e4a1 commit 942b411
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
Expand Up @@ -61,12 +61,17 @@ public void onRowUnselect(UnselectEvent<ProgrammingLanguage> event) {
TestUtils.addMessage("ProgrammingLanguage Unselected", event.getObject().getId() + " - " + event.getObject().getName());
}

public void unselectRow() {
selectedProgLanguage = null;
TestUtils.addMessage("NO ProgrammingLanguage selected", "");
}

public void submit() {
if (selectedProgLanguage != null) {
TestUtils.addMessage("Selected ProgrammingLanguage", selectedProgLanguage.getId() + " - " + selectedProgLanguage.getName());
}
else {
TestUtils.addMessage("no ProgrammingLanguage selected", "");
TestUtils.addMessage("NO ProgrammingLanguage selected", "");
}
}
}
Expand Up @@ -39,6 +39,7 @@

<p:commandButton id="button" value="Submit" update="@form" action="#{dataTable004.submit}"/>
<p:commandButton id="buttonMsgOnly" value="Submit - update card only" process="form:datatable" update="form:card"/>
<p:commandButton id="buttonUnselect" value="Unselect" update="@form" action="#{dataTable004.unselectRow}"/>

<p:card id="card" style="width: 25rem; margin-top: 2em">
<f:facet name="title">
Expand All @@ -48,7 +49,7 @@
#{dataTable004.selectedProgLanguage.name}
</p:outputPanel>
<p:outputPanel rendered="#{empty dataTable004.selectedProgLanguage}">
no ProgrammingLanguage selected
NO ProgrammingLanguage selected
</p:outputPanel>
</p:card>
</h:form>
Expand Down
Expand Up @@ -82,8 +82,8 @@ public void testSelectionSingle(Page page) {
page.button.click();

// Assert (no row selected)
dataTable.getRows().forEach(r -> Assertions.assertEquals("false", r.getWebElement().getAttribute("aria-selected")));
assertMessage(page, "no ProgrammingLanguage selected", "");
dataTable.getRows().forEach(r -> Assertions.assertEquals("false", r.getWebElement().getAttribute("aria-selected"), "Found a selected row!"));
assertMessage(page, "NO ProgrammingLanguage selected", "");
assertConfiguration(dataTable.getWidgetConfiguration());
}

Expand All @@ -100,7 +100,7 @@ public void testSelectionSingleUnselect(Page page) {
page.buttonMsgOnly.click();

// Assert
WebElement card = page.getWebDriver().findElement(By.id("form:card"));
WebElement card = page.card;
Assertions.assertTrue(card.getText().contains(languages.get(2).getName()));

// Act - unselect row
Expand All @@ -109,9 +109,34 @@ public void testSelectionSingleUnselect(Page page) {
page.buttonMsgOnly.click();

// Assert (no row selected)
dataTable.getRows().forEach(r -> Assertions.assertEquals("false", r.getWebElement().getAttribute("aria-selected")));
card = page.getWebDriver().findElement(By.id("form:card"));
Assertions.assertTrue(card.getText().contains("no ProgrammingLanguage selected"));
dataTable.getRows().forEach(r -> Assertions.assertEquals("false", r.getWebElement().getAttribute("aria-selected"), "Found a selected row!"));
Assertions.assertTrue(card.getText().contains("NO ProgrammingLanguage selected"));
assertConfiguration(dataTable.getWidgetConfiguration());
}

@Test
@Order(3)
@DisplayName("DataTable: selection - single - unselect programmatically; https://github.com/primefaces/primefaces/issues/7391")
public void testSelectionSingleUnselectProgrammatically(Page page) {
// Arrange
DataTable dataTable = page.dataTable;
Assertions.assertNotNull(dataTable);

// Act
dataTable.getCell(2, 0).getWebElement().click();
page.buttonMsgOnly.click();

// Assert
WebElement card = page.card;
Assertions.assertTrue(card.getText().contains(languages.get(2).getName()));

// Act - unselect row programmatically
page.buttonUnselect.click();

// Assert (no row selected)
assertMessage(page, "NO ProgrammingLanguage selected", "");
dataTable.getRows().forEach(r -> Assertions.assertEquals("false", r.getWebElement().getAttribute("aria-selected"), "Found a selected row!"));
Assertions.assertTrue(card.getText().contains("NO ProgrammingLanguage selected"));
assertConfiguration(dataTable.getWidgetConfiguration());
}

Expand Down Expand Up @@ -139,6 +164,12 @@ public static class Page extends AbstractPrimePage {
@FindBy(id = "form:buttonMsgOnly")
CommandButton buttonMsgOnly;

@FindBy(id = "form:buttonUnselect")
CommandButton buttonUnselect;

@FindBy(id = "form:card")
WebElement card;

@Override
public String getLocation() {
return "datatable/dataTable004.xhtml";
Expand Down
Expand Up @@ -82,12 +82,13 @@ public void decodeSelection(FacesContext context, DataTable table, Set<String> r
}

public void decodeSelectionRowKeys(FacesContext context, DataTable table) {
Set<String> rowKeys = null;
ValueExpression selectionByVE = table.getValueExpression(DataTableBase.PropertyKeys.selection.name());
if (selectionByVE != null) {
Object selection = selectionByVE.getValue(context.getELContext());

if (selection != null) {
Set<String> rowKeys = new HashSet<>();
rowKeys = new HashSet<>();

if (table.isSingleSelectionMode()) {
rowKeys.add(table.getRowKey(selection));
Expand All @@ -106,10 +107,9 @@ public void decodeSelectionRowKeys(FacesContext context, DataTable table) {
rowKeys.add(table.getRowKey(o));
}
}

table.setSelectedRowKeys(rowKeys);
}
}
table.setSelectedRowKeys(rowKeys);
}

protected void decodeSingleSelection(FacesContext context, DataTable table, Set<String> rowKeys) {
Expand Down

0 comments on commit 942b411

Please sign in to comment.