Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jchafik opened this issue Aug 3, 2015 · 5 comments
Open

Comments

@jchafik
Copy link

jchafik commented Aug 3, 2015

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
Copy link
Member

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.

@tduchateau tduchateau self-assigned this Aug 3, 2015
@jchafik
Copy link
Author

jchafik commented Aug 3, 2015

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
Copy link
Member

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
Copy link
Author

jchafik commented Aug 3, 2015

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

@tduchateau
Copy link
Member

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!

tduchateau added a commit that referenced this issue Aug 4, 2015
Fixes #294: DatatablesCriterias should return with single column defined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants