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

textattribute with filter and math widget #1601

Open
nitmpez opened this issue Nov 20, 2018 · 16 comments
Open

textattribute with filter and math widget #1601

nitmpez opened this issue Nov 20, 2018 · 16 comments

Comments

@nitmpez
Copy link

nitmpez commented Nov 20, 2018

I apologize if this isn't an actual issue, but I'm using the filter and math widget. When using the textAttribute to pass in seconds to the data-text attribute, the filter now filters off of that instead of the displayed value. I had to pass in seconds to avoid rounding issues with the data, when using the math widget.

IE:

<td data-text='${seconds}'>${hours}</td>

<td data-text="468">0.13</td>

textAttribute: 'data-text'

Doing a filter of >1 filters on the data-text instead of the ${hours}

Not sure if there is a way to make the filter widget ignore the textAttribute or to have the math widget use a custom textAttribute.

Thanks

@Mottie
Copy link
Owner

Mottie commented Nov 20, 2018

Hi @nitmpez!

The filter should target the cell text by default. It should only use the parsed values if the parser sets the flag or the filter-parsed class name is included in the header.

I haven't messed with or looked at that code in a while, so it would help me if you would modify this demo with an example of your use case.

@nitmpez
Copy link
Author

nitmpez commented Nov 20, 2018

I simplified quite a bit, but this should give the gist of what I'm trying to do. So if i filter >1, it doesn't work, but if I filter >3001, it will remove "route2" which is 0.87 hours.
https://jsfiddle.net/9w7k4d2x/

@TheSin-
Copy link
Collaborator

TheSin- commented Nov 20, 2018

your filter is based on data-number which is set to 3000 on that row hence why you need 3001 to filter it out. Comment out JS line 8 and it'll work with > 1

@nitmpez
Copy link
Author

nitmpez commented Nov 20, 2018

Agreed, that will fix the filter, but will break the math widget. You will get rounding errors, because the math widget will then use ${hours} instead of the data-number value.

@TheSin-
Copy link
Collaborator

TheSin- commented Nov 20, 2018

Oh sorry I have to change line 14 too, makes seconds into seconds1 or something that doesn't match.

Or change that function to iterate through and total up data-number before using value. I'm not sure which cause I assume that function has value in your production version in this version it doesn't but I'd assume if you pull your table from a db, add a data-total to the total field and use that value instead then you won't need to iterate through which is expensive.

@nitmpez
Copy link
Author

nitmpez commented Nov 20, 2018

main point is, the math widget and the filter widget both rely on the textAttribute setting.

I could do what you say, and provide a data-total, but unless you used an ajax filter to update, your data-total would be static. So if you filtered a row, your result would stay the same.

I thought about maybe a custom math function and to grab the cell attribute manually, but I'm not sure that is possible. The custom functions I've done, just pass in the arry, which comes from the value shown if no textAttribute is specified. I'd have to have the array of $cell, and pull the value from each cell and sum them.

@TheSin-
Copy link
Collaborator

TheSin- commented Nov 20, 2018

right, so try something like

      filter_functions : {
      	1: function(e, n, f, i, $r, c, data) {
        	return (e / 3600) > f; 
        }
      },

obviously I'd locked it to > but you could so string match to check for a compare and if not make it default to something. If you add that and put 1 in the filter it works.

@Mottie
Copy link
Owner

Mottie commented Nov 20, 2018

Do you need the second data to maintain accuracy? If not, then maybe try this (demo):

<th data-math="col-hours" col_desc="seconds">col-hours</th>
  $.tablesorter.equations["hours"] = function(arry, config) {
  return arry.reduce(function(total, val) {
    return (total += val * 3600);
  }, 0);
};

And remove the textAttribute setting.

It might be better to add a math setting to allow manipulating or targeting a different attribute.

@TheSin-
Copy link
Collaborator

TheSin- commented Nov 20, 2018

I agree with the last statement but I'm just trying to show there are way to make it work as is jus need to think outside the box ;). but like you I'm not sure which value is required versus just display curtesy ;)

@nitmpez
Copy link
Author

nitmpez commented Nov 20, 2018

Yeah, I'm trying to maintain accuracy. I think adding the custom filter which TheSin recommended might work out, and do what I need it to do.

On a side note, I guess I don't understand why you would ever filter on something that wasn't displayed. But there might be examples I'm not thinking of, but I can't think of any off the top of my head.

@TheSin-
Copy link
Collaborator

TheSin- commented Nov 20, 2018

the easiest and most common is formatted text like money, filtering on 1023.23 is better than $1023.23 USD for example.

@nitmpez
Copy link
Author

nitmpez commented Nov 20, 2018

Yeah, I just thought of that, or if you did some custom html in the td, so the display looked the same, but it would mess up the filter.

But yeah, I think a custom math attribute for a number would be great, but like TheSin said there are multiple ways to get around something, and I think the custom filter would do it.

Mottie added a commit that referenced this issue Nov 20, 2018
Mottie added a commit that referenced this issue Nov 20, 2018
@Mottie
Copy link
Owner

Mottie commented Nov 20, 2018

I just updated to v2.31.1... new math_textAttr docs here.

@TheSin-
Copy link
Collaborator

TheSin- commented Nov 20, 2018

sure just render my thinking useless ;). But efficient support as always here :D

@Mottie
Copy link
Owner

Mottie commented Nov 20, 2018

render my thinking useless

I still <3 you!

@nitmpez
Copy link
Author

nitmpez commented Nov 21, 2018

<3 Thanks Mottie and TheSin

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

3 participants