dandelion / dandelion-datatables

Dandelion component for DataTables
http://dandelion.github.io/components/datatables/
Other
110 stars 49 forks source link

Datatables does not work with a single column and server-side processing #294

Open jchafik opened 9 years ago

jchafik commented 9 years ago

This is probably an unlikely case, but I noticed it while updating a test case for 1.1.0. DatatablesCriterias.getColumnNumber will return 0 (as in zero columns) if 1 column is defined.

Perhaps the columnNumber counter should begin at -1 (for no results), and take the Math.max(columnNumber, col). This way, the if (columnNumber != 0) check may be removed, and a valid count will always be returned (0 if no request params were matched, # of columns otherwise).

Specifically, I attempted sorting the datatable with a single column.

tduchateau commented 9 years ago

Hi Jacob,

I've tested with this sample: https://github.com/dandelion/dandelion-datatables-samples/blob/1.1.0/datatables-thymeleaf-ajax/src/main/webapp/WEB-INF/views/server-side/enabling-server-side-processing.html I've just removed all columns except the first one and I can't reproduce the above issue. Would you have more details to share?

Regarding the column number, actually it has to begin at 0, just because the field is used both as a counter and as a column indexer, since DataTables requires the index to begin at 0.

jchafik commented 9 years ago

You can test with the following:

        // Test ascending sort
        final MockHttpServletRequest request = new MockHttpServletRequest();

        // Column properties
        request.addParameter("columns[0][data]", "email");
        request.addParameter("columns[0][orderable]", String.valueOf(true));

        // **** Remove the following two lines and the test will fail. ****
        request.addParameter("columns[0][data]", "type");
        request.addParameter("columns[0][orderable]", String.valueOf(false));

        // Sorting properties
        request.addParameter("order[0][column]", 0);
        request.addParameter("order[0][dir]", SortDirection.ASC.toString());

        DatatablesCriterias criteria = DatatablesCriterias.getFromRequest(request);

        Assert.assertTrue(criteria.hasOneSortedColumn());

int com.github.dandelion.datatables.core.ajax.DatatablesCriterias.getColumnNumber(HttpServletRequest request) is actually 1-based. As long as there are multiple columns, it increments the counter at the end (line 269). Since it's returning the number of columns (not the last index), it should return 0+ (0 for no definitions). If you initialize the counter at -1 and increment it by one at the end, you'll be guaranteed to have 0+. To verify, you can check line 189 of DatatablesCriterias. for (int i = 0; i < columnNumber; i++) { will iterate from [0,columnNumber)

tduchateau commented 9 years ago

My bad. You're right from the very beginning! I've reproduced the issue. And your suggestion fits perfectly. Would you like to make a PR? :-)

jchafik commented 9 years ago

Sure, I'll get it fixed and open a PR in a few hours (after work)

tduchateau commented 9 years ago

Great. 2 notes: 1) please read and sign https://docs.google.com/forms/d/1-lciVTM3cqkcOVmDWH4wEzqE36BNlldtwMaoRUOhHTc/viewform before I can merge the PR 2) please submit the PR against the wip/1.1.1 branch

Thx!