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

0 evaluates to false in a logical context, allow a zero to be returned for totalRows #324

Merged
merged 1 commit into from
Sep 29, 2013

Conversation

camallen
Copy link
Contributor

@camallen camallen commented Jun 4, 2013

avoid 0 being treated as false in the || statement.

@Mottie
Copy link
Owner

Mottie commented Jun 4, 2013

The way the code is set up now:

result = c.ajaxProcessing(data, table) || [ 0, [] ],
// allow [ total, rows, headers ]  or [ rows, total, headers ]
t = isNaN(result[0]) && !isNaN(result[1]);
// ...
c.totalRows = result[t ? 1 : 0] || c.totalRows || 0;

if no result is returned from ajax, or the result is zero, it will then return the current c.totalRows, and if that is zero, it will then return zero. Is this a problem with the c.totalRows not being zero?

Maybe I'm not seeing how the above is different from this:

rr_count = result[t ? 1 : 0];
c.totalRows = isNaN(rr_count) ? c.totalRows || 0 : rr_count;

rr_count should always return a number (or maybe undefined) since it is coming from the server. If it isn't then the server side code should be modified?

@camallen
Copy link
Contributor Author

camallen commented Jun 5, 2013

@Mottie, perhaps i should have set up some context for my pull request.

Let's say i am using server side filtering and the ajax pager with 100 records on the server. On initial load of the table i get 10 records for the first page, 1-10 / 100 and c.totalRows = 100.

Lets say that i then run some restrictive filtering that returns no rows but the total rows are the same (ajaxProcessing result = [ 100, [ [] ] ]). The table will have no rows but the pager links will be active trying to page into nothing.

Now i want the pager links to be correct so i return 0 total rows (ajaxProcessing result = [ 0, [ [] ] ]) but total rows doesn't update as the code uses the existing count of c.totalRows.

Looking at the code:
t = isNaN(result[0]) && !isNaN(result[1]);
=> t = false

Before the pull request:
c.totalRows = result[t ? 1 : 0] || c.totalRows || 0;
=> 100
As the result[0] is 0, which when used in 0 || c.totalRows will return the c.totalRows value which is 100.

My pull request will not use the result[0] in a logical operation for detecting it's value:
rr_count = result[t ? 1 : 0];
=> 0
//check that the result is a number, if not use the previous c.totalRows || 0 //otherwise use the latest returned count. c.totalRows = isNaN(rr_count) ? c.totalRows || 0 : rr_count;
=> 0

I think my use case is not the most common as most users won't have a changing totalRows count on the pager. However this change should be backwards compatible with any previous use cases and my filtering use case.

I hope i've explained the reasoning for the pull request.

Cheers,
Cam

@sbine
Copy link

sbine commented Aug 27, 2013

@camallen Thank you, I was looking for a solution to this issue.

@camallen
Copy link
Contributor Author

Hey @sbine, glad I could help.

Hopefully @Mottie accepts this pull request when he gets back from his sabbatical.

Mottie added a commit that referenced this pull request Sep 29, 2013
0 evaluates to false in a logical context, allow a zero to be returned for totalRows
@Mottie Mottie merged commit cb5efc0 into Mottie:master Sep 29, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants