Skip Navigation

[Resolved] Formatting a number before it's used in the filter

This support ticket is created 4 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by Simon Logan 4 years, 2 months ago.

Assisted by: Shane.

Author
Posts
#1497933

On our site we allow people to search for finance products so we have a filter form which takes input from the user in the form of a number (ie 30000) and this is then filtered against the matching finance products which works fine.

What the client has asked for, however, is that we format the number as the user types it to include thousand separators so we've added in some jQuery on keyup to do this which works fine at hidden link, the problem is that the thousand separator is then used in the filter which stops it working properly since it's not a strict number any more.

What I think we need to do is to be able to allow the jQuery to run on the inputs but then to have the thousand separators removed again when the filter is applied? Is there a way we can format those two specific fields back to strict numbers so the filtering works properly?

I'd looked around other posts such as https://toolset.com/forums/topic/transforming-content-in-number-field-as-price/ but they are more about merely displaying the field on the frontend whereas we need it to change for use in the filter.

#1498311

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simon,

These numbers are stored as simple integers on the backend ? This means they are stored without any formatting ?

The problem that we will run into is we cannot remove the formatting quick enough using jQuery because the form would already be submitted.

I think the best solution to this is to format the number itself when you're entering it on the backend so it stores with the correct formatting.

The problem we will have with this now is that because we won't be able to do min and max with string values and pure number fields can't have any formatting.

Thanks,
Shane

#1498367

Hi Shane

That's right, the numbers are stored as integers rather than strings in the backend (ie 35000 instead of 35,000) which is required in order to get the functionality of those numbers being checked they fall between a number range.

Is it not possible to use a function using the wpv_filter_query hook to re-format the numbers being sent to the filter query? So the person enters 35000, Jquery formats this as 35,000 on keyup, then when the user submits the form the actual value passed to the filter query is adjusted back to 35000 before being filtered on?

I thought the above method might be the cleanest but another idea I'd had was to add in two manual hidden fields and when someone enters a value 30000 then the JQuery script formats this as 30,000 but also copies that value (without formatting, so 30000) into the hidden field. I could then adjust the form's filtering to filter by the hidden field's value rather than the visible (formatted) field?

#1498463

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Simon,

I just thought of an Idea using your hidden fields.

When the user is typing in the field you can copy over their value with the keyup method for jquery into a hidden input field.

This field you can use to submit the actual value in the URL. The current field will only be there for display purposes but the actual field for the submit would be hidden.

This would should work as you are describing it.

Thanks,
Shane

#1498465

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

one suggestion is that you clear the hidden fields with each page load.

#1501447

The approach which worked for me was to hide the field from the post type (min loan) on which the filtering is performed, then to add a new manually-added field to the filter form "min-loan-formatted". It is this latter one which the user enters the number into and jQuery formats into thousands for us, ie 30000 becomes 30,000.

The same jQuery script also copies that value into the hidden "min loan" field but strips the commas this time, so its value is 30000.

This is therefore the value in which the filtering is performed and the manually created "min-loan-formatted" field is merely used for displaying the search amount to the user in a more user-friendly manner.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.