Skip Navigation

[Resolved] Adding Flickity to a Blocks View

This support ticket is created 3 years, 11 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 – 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: Africa/Casablanca (GMT+01:00)

This topic contains 2 replies, has 2 voices.

Last updated by JoelK2744 3 years, 11 months ago.

Assisted by: Jamal.

Author
Posts
#1924469

Hello, I'm trying to follow Jamal's suggestion in this thread to add Flickity to a Block View - https://toolset.com/forums/topic/create-slideshow-with-content-from-custom-post-type/#post-1618051

I'm testing on a local site, I have installed Flickity JS and CSS from here - hidden link - and tested that it loads by creating a simple slider with HTML successfully.

I'm having difficulty getting it to work with a View created with Toolset Blocks. Jamal suggests wrapping the content of the loop inside a container block and giving that block a class to initialise Flickity. This is the part I'm struggling with, which part of the View block needs to go inside a container? Is it the View output? View loop? Neither seem to work for me.

The view seems to create another Div - js-wpv-loop-wrapper - which I think might be better to target but I'm not sure how this relates to the View Block breakdown (View>ViewOutput>View Loop>etc).

Also, how should I be setting up the View in terms of Pagination (off?) and Loop Style (unformatted?)

Thanks

#1924781

Hello and thank you for contacting the Toolset support.

I previously suggested putting what is inside View Loop in a Container to have (View>ViewOutput>View Loop>Container>etc) so we can have something like the HTML in this example hidden link
But this is not required anymore because the view now generates a wrapper for the loop's content. Check this screenshot hidden link
Now the view generates the following:

<div class="js-wpv-loop-wrapper">
	<div class="wp-block-toolset-views-view-template-block wpv-block-loop-item php-to-be-replaced-with-shortcode" data-toolset-views-view-template-block="dc93e5c9d995759b1b0bc802e00be842">...</div>
	<div class="wp-block-toolset-views-view-template-block wpv-block-loop-item php-to-be-replaced-with-shortcode" data-toolset-views-view-template-block="dc93e5c9d995759b1b0bc802e00be842">...</div>
	<div class="wp-block-toolset-views-view-template-block wpv-block-loop-item php-to-be-replaced-with-shortcode" data-toolset-views-view-template-block="dc93e5c9d995759b1b0bc802e00be842">...</div>
</div>

And you can initialize the slider with code like:

$('#wpv-view-layout-1912 .js-wpv-loop-wrapper').flickity({
  // options
  cellAlign: 'left',
  contain: true
});

Or more precisely, if you have multiple views on the page, by targeting the view's ID attribute that uses the view's ID(1912 in the following example) hidden link

$('#wpv-view-layout-1912 .js-wpv-loop-wrapper').flickity({
  // options
  cellAlign: 'left',
  contain: true
});

You are right, you need to use unformatted loop style.

And regarding pagination, you can choose, between generating all the pages in the first load, or relying upon Toolset pagination. In the latter case, and if you use AJAX pagination, you can rely upon Toolset View's events:

jQuery( document ).on( 'js_event_wpv_pagination_completed', function( event, data ) {
	/**
	* data.view_unique_id (string) The View unique ID hash
	* data.effect (string) The View AJAX pagination effect
	* data.speed (integer) The View AJAX pagination speed in miliseconds
	* data.layout (object) The jQuery object for the View layout wrapper
	*/
	
});

View's Javascript events are not currently available in the blocks editor interface, even if they do work on the frontend. To see all the available events, you can create a view on the legacy editor, just, to copy/paste the events from it. Check this screenshot hidden link
https://toolset.com/documentation/programmer-reference/adding-custom-javascript-code-to-views-that-use-ajax/

So, as a conclusion:
- No more need to use a container block. The loop, now, generates a wrapper for each of the loop instances.
- Unformatted style for the loop.
- Use frontend events to reinitialize the slider if you are using AJAX.

I hope this answers your questions. Let me know if you have any further questions.

#1925463

Thanks Jamal...that's a great reply. While I wasn't able to initialise using jQuery, I've managed to get it working using vanilla JS with your guidance which is great! I need to spend a bit more time to understand the pagination options but it's working great without pagination for now.

Thanks again