Skip to content

Commit

Permalink
fix(ajax, sorting): properly set the sorted attribute of ColumnDef
Browse files Browse the repository at this point in the history
Also fixed wording issues

Closes #285
  • Loading branch information
tduchateau committed Jul 2, 2015
1 parent 6de5c5e commit 29a6061
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class ColumnDef implements Serializable {
private static final long serialVersionUID = 6349611254914115218L;
private String name;
private boolean sortable;
private boolean sorted;
private boolean sorted = false;
private boolean searchable;
private boolean filtered;
private String regex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ public class DatatablesCriterias implements Serializable {
private final Integer start;
private final Integer length;
private final List<ColumnDef> columnDefs;
private final List<ColumnDef> sortingColumnDefs;
private final List<ColumnDef> sortedColumnDefs;
private final Integer draw;

private DatatablesCriterias(String search, Integer displayStart, Integer displaySize, List<ColumnDef> columnDefs,
List<ColumnDef> sortingColumnDefs, Integer draw) {
List<ColumnDef> sortedColumnDefs, Integer draw) {
this.search = search;
this.start = displayStart;
this.length = displaySize;
this.columnDefs = columnDefs;
this.sortingColumnDefs = sortingColumnDefs;
this.sortedColumnDefs = sortedColumnDefs;
this.draw = draw;
}

Expand All @@ -101,8 +101,19 @@ public List<ColumnDef> getColumnDefs() {
return columnDefs;
}

/**
* @return all sorted columns.
* @deprecated Use {@link #getSortedColumnDefs()} instead.
*/
public List<ColumnDef> getSortingColumnDefs() {
return sortingColumnDefs;
return sortedColumnDefs;
}

/**
* @return all sorted columns.
*/
public List<ColumnDef> getSortedColumnDefs() {
return sortedColumnDefs;
}

/**
Expand Down Expand Up @@ -144,7 +155,7 @@ public Boolean hasOneFilteredColumn() {
* @return true if a column is being sorted, false otherwise.
*/
public Boolean hasOneSortedColumn() {
return !sortingColumnDefs.isEmpty();
return !sortedColumnDefs.isEmpty();
}

/**
Expand Down Expand Up @@ -207,11 +218,19 @@ else if (searchTerm.contains("~")) {
}
}

for (int j = 0; j < columnNumber; j++) {
String ordered = request.getParameter("order[" + j + "][column]");
if (ordered != null && ordered.equals(String.valueOf(i))) {
columnDef.setSorted(true);
break;
}
}

columnDefs.add(columnDef);
}

// Sorted column definitions
List<ColumnDef> sortingColumnDefs = new LinkedList<ColumnDef>();
List<ColumnDef> sortedColumnDefs = new LinkedList<ColumnDef>();

for (int i = 0; i < columnNumber; i++) {
String paramSortedCol = request.getParameter("order[" + i + "][column]");
Expand All @@ -225,11 +244,11 @@ else if (searchTerm.contains("~")) {
sortedColumnDef.setSortDirection(SortDirection.valueOf(sortedColDirection.toUpperCase()));
}

sortingColumnDefs.add(sortedColumnDef);
sortedColumnDefs.add(sortedColumnDef);
}
}

return new DatatablesCriterias(paramSearch, start, length, columnDefs, sortingColumnDefs, draw);
return new DatatablesCriterias(paramSearch, start, length, columnDefs, sortedColumnDefs, draw);
}

private static int getColumnNumber(HttpServletRequest request) {
Expand All @@ -255,6 +274,6 @@ private static int getColumnNumber(HttpServletRequest request) {
@Override
public String toString() {
return "DatatablesCriterias [search=" + search + ", start=" + start + ", length=" + length + ", columnDefs="
+ columnDefs + ", sortingColumnDefs=" + sortingColumnDefs + ", draw=" + draw + "]";
+ columnDefs + ", sortingColumnDefs=" + sortedColumnDefs + ", draw=" + draw + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void should_return_non_null_criterias_when_request_is_not_null() {
assertThat(criterias.getLength()).isEqualTo(-1);
assertThat(criterias.getDraw()).isEqualTo(-1);
assertThat(criterias.getColumnDefs()).isEmpty();
assertThat(criterias.getSortingColumnDefs()).isEmpty();
assertThat(criterias.getSortedColumnDefs()).isEmpty();
}

@Test
Expand All @@ -104,16 +104,16 @@ public void should_return_criterias_when_sorting_disabled_and_filtering_disabled
assertThat(criterias.getColumnDefs().get(0).getName()).isEqualTo("prop1");
assertThat(criterias.getColumnDefs().get(0).isSortable()).isFalse();
assertThat(criterias.getColumnDefs().get(0).isSorted()).isFalse();
assertThat(criterias.getColumnDefs().get(0).isFilterable()).isFalse();
assertThat(criterias.getColumnDefs().get(0).isSearchable()).isFalse();
assertThat(criterias.getColumnDefs().get(0).isFiltered()).isFalse();

assertThat(criterias.getColumnDefs().get(1).getName()).isEqualTo("prop2");
assertThat(criterias.getColumnDefs().get(1).isSortable()).isFalse();
assertThat(criterias.getColumnDefs().get(1).isSorted()).isFalse();
assertThat(criterias.getColumnDefs().get(1).isFilterable()).isFalse();
assertThat(criterias.getColumnDefs().get(1).isSearchable()).isFalse();
assertThat(criterias.getColumnDefs().get(1).isFiltered()).isFalse();

assertThat(criterias.getSortingColumnDefs()).isEmpty();
assertThat(criterias.getSortedColumnDefs()).isEmpty();
}

@Test
Expand All @@ -140,22 +140,22 @@ public void should_return_criterias_when_sorting_enabled_and_filtering_disabled(
assertThat(criterias.getColumnDefs()).hasSize(2);
assertThat(criterias.getColumnDefs().get(0).getName()).isEqualTo("prop1");
assertThat(criterias.getColumnDefs().get(0).isSortable()).isTrue();
assertThat(criterias.getColumnDefs().get(0).isSorted()).isFalse();
assertThat(criterias.getColumnDefs().get(0).isFilterable()).isFalse();
assertThat(criterias.getColumnDefs().get(0).isSorted()).isTrue();
assertThat(criterias.getColumnDefs().get(0).isSearchable()).isFalse();
assertThat(criterias.getColumnDefs().get(0).isFiltered()).isFalse();

assertThat(criterias.getColumnDefs().get(1).getName()).isEqualTo("prop2");
assertThat(criterias.getColumnDefs().get(1).isSortable()).isTrue();
assertThat(criterias.getColumnDefs().get(1).isSorted()).isFalse();
assertThat(criterias.getColumnDefs().get(1).isFilterable()).isFalse();
assertThat(criterias.getColumnDefs().get(1).isSearchable()).isFalse();
assertThat(criterias.getColumnDefs().get(1).isFiltered()).isFalse();

assertThat(criterias.getSortingColumnDefs()).hasSize(1);
assertThat(criterias.getColumnDefs().get(0).getName()).isEqualTo("prop1");
assertThat(criterias.getColumnDefs().get(0).isSortable()).isTrue();
assertThat(criterias.getColumnDefs().get(0).isSorted()).isFalse();
assertThat(criterias.getColumnDefs().get(0).isFilterable()).isFalse();
assertThat(criterias.getColumnDefs().get(0).isFiltered()).isFalse();
assertThat(criterias.getSortedColumnDefs()).hasSize(1);
assertThat(criterias.getSortedColumnDefs().get(0).getName()).isEqualTo("prop1");
assertThat(criterias.getSortedColumnDefs().get(0).isSortable()).isTrue();
assertThat(criterias.getSortedColumnDefs().get(0).isSorted()).isTrue();
assertThat(criterias.getSortedColumnDefs().get(0).isSearchable()).isFalse();
assertThat(criterias.getSortedColumnDefs().get(0).isFiltered()).isFalse();
}

@Test
Expand All @@ -178,7 +178,7 @@ public void should_have_one_filterable_column() {

DatatablesCriterias criterias = DatatablesCriterias.getFromRequest(request);

assertThat(criterias.hasOneFilterableColumn()).isTrue();
assertThat(criterias.hasOneSearchableColumn()).isTrue();
}

@Test
Expand Down

0 comments on commit 29a6061

Please sign in to comment.