/**
* Sets the current sort order using the fluid Sort API. Read the
* documentation for {@link Sort} for more information.
+ * <p>
+ * <em>Note:</em> Sorting by a property that has no column in Grid will hide
+ * all possible sorting indicators.
*
* @param s
* a sort instance
+ *
+ * @throws IllegalStateException
+ * if container is not sortable (does not implement
+ * Container.Sortable)
+ * @throws IllegalArgumentException
+ * if trying to sort by non-existing property
*/
public void sort(Sort s) {
setSortOrder(s.build());
/**
* Sort this Grid in ascending order by a specified property.
+ * <p>
+ * <em>Note:</em> Sorting by a property that has no column in Grid will hide
+ * all possible sorting indicators.
*
* @param propertyId
* a property ID
+ *
+ * @throws IllegalStateException
+ * if container is not sortable (does not implement
+ * Container.Sortable)
+ * @throws IllegalArgumentException
+ * if trying to sort by non-existing property
*/
public void sort(Object propertyId) {
sort(propertyId, SortDirection.ASCENDING);
/**
* Sort this Grid in user-specified {@link SortOrder} by a property.
+ * <p>
+ * <em>Note:</em> Sorting by a property that has no column in Grid will hide
+ * all possible sorting indicators.
*
* @param propertyId
* a property ID
* @param direction
* a sort order value (ascending/descending)
+ *
+ * @throws IllegalStateException
+ * if container is not sortable (does not implement
+ * Container.Sortable)
+ * @throws IllegalArgumentException
+ * if trying to sort by non-existing property
*/
public void sort(Object propertyId, SortDirection direction) {
sort(Sort.by(propertyId, direction));
}
/**
- * Sets the sort order to use. This method throws
- * {@link IllegalStateException} if the attached container is not a
- * {@link Container.Sortable}, and {@link IllegalArgumentException} if a
- * property in the list is not recognized by the container, or if the
- * 'order' parameter is null.
+ * Sets the sort order to use.
+ * <p>
+ * <em>Note:</em> Sorting by a property that has no column in Grid will hide
+ * all possible sorting indicators.
*
* @param order
* a sort order list.
+ *
+ * @throws IllegalStateException
+ * if container is not sortable (does not implement
+ * Container.Sortable)
+ * @throws IllegalArgumentException
+ * if order is null or trying to sort by non-existing property
*/
public void setSortOrder(List<SortOrder> order) {
setSortOrder(order, false);
}
- private void setSortOrder(List<SortOrder> order, boolean userOriginated) {
+ private void setSortOrder(List<SortOrder> order, boolean userOriginated)
+ throws IllegalStateException, IllegalArgumentException {
if (!(getContainerDataSource() instanceof Container.Sortable)) {
throw new IllegalStateException(
"Attached container is not sortable (does not implement Container.Sortable)");
Object[] propertyIds = new Object[items];
boolean[] directions = new boolean[items];
- String[] columnKeys = new String[items];
SortDirection[] stateDirs = new SortDirection[items];
for (int i = 0; i < items; ++i) {
SortOrder order = sortOrder.get(i);
- columnKeys[i] = this.columnKeys.key(order.getPropertyId());
stateDirs[i] = order.getDirection();
-
propertyIds[i] = order.getPropertyId();
switch (order.getDirection()) {
case ASCENDING:
fireEvent(new SortEvent(this, new ArrayList<SortOrder>(sortOrder),
userOriginated));
- getState().sortColumns = columnKeys;
- getState(false).sortDirs = stateDirs;
+ if (columns.keySet().containsAll(Arrays.asList(propertyIds))) {
+ String[] columnKeys = new String[items];
+ for (int i = 0; i < items; ++i) {
+ columnKeys[i] = this.columnKeys.key(propertyIds[i]);
+ }
+ getState().sortColumns = columnKeys;
+ getState(false).sortDirs = stateDirs;
+ } else {
+ // Not all sorted properties are in Grid. Remove any indicators.
+ getState().sortColumns = new String[] {};
+ getState(false).sortDirs = new SortDirection[] {};
+ }
} else {
throw new IllegalStateException(
"Container is not sortable (does not implement Container.Sortable)");
import com.vaadin.testbench.elements.GridElement;
import com.vaadin.testbench.elements.GridElement.GridCellElement;
+import com.vaadin.testbench.elements.NotificationElement;
import com.vaadin.tests.annotations.TestCategory;
import com.vaadin.tests.tb3.MultiBrowserTest;
assertTrue("Column baz was not sorted descending", bazHeader
.getAttribute("class").contains("sort-desc"));
}
+
+ @Test
+ public void testInitialSorting() {
+ // Grid is sorted in this case by one visible and one nonexistent
+ // column. There should be no sort indicator.
+ setDebug(true);
+ openTestURL();
+
+ GridElement grid = $(GridElement.class).first();
+
+ GridCellElement kmHeader = grid.getHeaderCell(0, 1);
+ assertFalse("Column km was unexpectedly sorted",
+ kmHeader.getAttribute("class").contains("sort-asc")
+ || kmHeader.getAttribute("class").contains("sort-desc"));
+ assertFalse("Unexpected client-side exception was visible",
+ isElementPresent(NotificationElement.class));
+ }
}