Skip Navigation

[Resolved] Split: Sortable columns for the view table

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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 5 replies, has 2 voices.

Last updated by Waqar 10 months, 3 weeks ago.

Assisted by: Waqar.

Author
Posts
#2671347

I'm seeing that the Order ID and Order Date field columns are sorting correctly, but the other columns are not. Here's how the other headers are coded in the View:

<th style="text-align: center;">[wpv-heading name="_billing_first_name"]First Name[/wpv-heading]</th>
<th style="text-align: center;">[wpv-heading name="_billing_last_name"]Last Name[/wpv-heading]</th>
<th style="text-align: center;">[wpv-heading name="_order_total"]Order Total[/wpv-heading]</th>
<th style="text-align: center;">[wpv-heading name="post-field"]Ordered products[/wpv-heading]</th>
<th style="text-align: center;">[wpv-heading name="post-field"]Product attributes[/wpv-heading]</th>

Previously I had them all as name="post-field". I tried changing to _billing_first_name, _billing_last_name, and _order_total on those three, but that didn't fix it. I'm not sure what I'd use for the other two fields getting the product titles and attributes. I suppose it is possible that I can't sort on those with those being pulled from shortcodes.

#2671363

Hi,

For the non-Toolset Types custom fields, the prefix 'field-' needs to be included in the 'wpv-heading' shortcode:


[wpv-heading name="field-_billing_first_name"]First Name[/wpv-heading]
[wpv-heading name="field-_billing_last_name"]Last Name[/wpv-heading]
[wpv-heading name="field-_order_total"]Order Total[/wpv-heading]

You'll note that the sorting is working correctly for the columns for the 'billing first name' and the 'billing last name'.

It is not working for the 'Order Total' column because the 'orderby_as' attribute in that query needs to be set to 'numeric', whereas, by default, all non-Toolset custom fields are treated as a 'string'.

But, even if some workaround is used to make this column's sorting work, the main challenge remains for the other columns where custom shortcodes are used. Their columns can't be sorted through this view's query method, because they're not coming directly from a single custom field or taxonomy source.

For sorting a table like this, a better alternative would be dropping the built-in query-based table column sorting and adding it through the front-end script. This would mean that you'll create the table through the view, but without using the 'wpv-heading' shortcode. For sorting you can include a third-party script library that can sort the table columns, without having to make any database-side queries.

Here is an example of such a script that you can use:
hidden link

regards,
Waqar

#2671897

Confirmed that First Name and Last Name columns are now sorting correctly. Thanks on that.

Order Total column: How do I set that to numeric? All I'm seeing in the view is the header for the column is set up as

<th style="text-align: center;">[wpv-heading name="field-_order_total"]Order Total[/wpv-heading]</th>

And the column output loop is set up as

<td style="text-align: center;">[wpv-post-field name="_order_total"]</td>

I'm not seeing anything defined as 'orderby_as'. Let's get this one working before worrying about the last two columns further.

As for the other two columns, your suggested approach is definitely beyond my skill level. But realize the main reason we are seeking sortability on those columns is because the desired front-end filter on product names wasn't doable from this ticket:

https://toolset.com/forums/topic/query-filter-in-view-based-on-all-woocommerce-product-titles-in-store/

The idea of sorting by the product name is just so we can at least see all orders for each product and each product attribute together within the table. I was just hoping to get that as a minimum in case we can't get contractor help to achieve this, or in case the contractor help will cost more than the client is wanting to pay.

#2672615

Thanks for writing back and glad that the sorting is working for main name fields/columns.

For the Order Total column, you can include the following custom script in the view's JS editor under the 'Search and Pagination' section:


jQuery( document ).on( 'ready js_event_wpv_pagination_completed js_event_wpv_parametric_search_form_updated js_event_wpv_parametric_search_results_updated', function( event, data ) {
	jQuery('a.js-wpv-sort-trigger.js-wpv-column-header-click[data-orderby=field-_order_total]').attr('data-orderbyas','numeric');
});

For the remaining columns where the data is coming from multiple fields/sources, you can remove the 'wpv-heading' shortcode from around their text labels, so that it is not clickable.
( since the sorting doesn't work for them )

#2673937

Sorry for taking a bit to get back to you on this. OK, both these suggestions worked. Thanks.

Regrettably here, the sorting on the product title and the product attribute was one of the main goals of this page moreso than the other fields. It's still good to have it working to the extent that it is and thanks for the assistance there. I've gotten a quote from Codeable on our needs from two support tickets here where I'm trying to set up filters on both the front end and the back end on product titles and product attributes, but it's unfortunately going to be unaffordable for us at their rates. The sorting idea was a fallback "solution" I thought would be helpful to see all the like products and like attributes together on the page, but that's proving too difficult too.

I guess we can mark this as resolved and I'll do so after your last reply on the chance that you'll have anything more you can add for filtering on product titles and product attributes from orders.

#2674189

Thanks for the update and glad that I was able to assist.

I think we've covered all the points that can be added to this discussion. If there are no follow-up questions, you're welcome to mark this ticket as resolved.

Feel free to start a new ticket for a new question or concern; we're here to help!