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

Do not work with Turbolinks 5 well #31

Open
ledsun opened this issue Mar 8, 2018 · 13 comments
Open

Do not work with Turbolinks 5 well #31

ledsun opened this issue Mar 8, 2018 · 13 comments

Comments

@ledsun
Copy link

ledsun commented Mar 8, 2018

Problem

bootstrap-select depends on the load events of the window.

https://github.com/silviomoreto/bootstrap-select/blob/master/js/bootstrap-select.js#L1840

  $(window).on('load.bs.select.data-api', function () {
    $('.selectpicker').each(function () {
      var $selectpicker = $(this);
      Plugin.call($selectpicker, $selectpicker.data());
    })
  });

But the load events of the window is not fired in page transitions with Turbolinks 5.

Reference: snapappointments/bootstrap-select#1413

Resolve

We relay the 'turbolinks:load' events of the document to the load events of the window.

For example:

$(document).on('turbolinks:load', function() {
   $(window).trigger('load.bs.select.data-api');
});

Suggestion

I think it is a good idea to append above code into the vendor/assets/javascripts/bootstrap-select.js in this gem.
Because this gem is to use bootstrap-select with the Ruby on Rails.

@CodyFitzpatrick
Copy link

CodyFitzpatrick commented Feb 8, 2019

While I'm not using this gem in my Rails 5 app, Bootstrap 4 <select>s break when turbolinks loads pages from cache. @ledsun's mentioned fix work in these situations, without duplicating dropdowns like $('select').selectpicker(); happened to do.

Hopefully this can help someone else.

@cigano
Copy link

cigano commented Apr 7, 2019

This solution doesn't work for version 1.13.8. Had to disable Turbolinks completely.

At any <div> from you layout, add data-turbolinks="false".

@ledsun
Copy link
Author

ledsun commented May 17, 2019

I made a sample application that uses version 1.13.8.
This solution is still working well.
https://github.com/ledsun/sample-application-for-verifying-the-solution-for-bootstrap-select-rails-to-work-with-Turbolinks-5.

@cesc1989
Copy link

cesc1989 commented May 27, 2019

Also didn't work in my case.

  • bootstrap-select-rails (1.13.8)
  • rails (5.2.3)

@pam81
Copy link

pam81 commented Jun 26, 2019

same issue
I'm using
"bootstrap": "4.3.1",
"bootstrap-select": "1.13.10",
"turbolinks": "5.2.0"

@eredi93
Copy link

eredi93 commented Oct 3, 2019

@Slashek are you planning to support turbolinks?
I saw you closed #32 without offering a workaround. should we assume turbolinks is not supported?
If so should you update the README?

@xxEoD2242
Copy link

xxEoD2242 commented Dec 20, 2019

@ledsun's solution is working for:

  • Turoblinks (5.2.1)
  • Rails (6.0.0)
  • Bootstrap (4.3.1)
  • bootstrap-select (1.13.8)

Also, try adding 'ready' for the $(document).on() selection. Example below:

$(document).on('ready turbolinks:load', function() {
    $(window).trigger('load.bs.select.data-api');
});

Having both ready and turbolinks:load has helped me get past all of the JS/Turbolinks issues I have had.

@romanbsd
Copy link

It breaks when doing "back" in the browser.

@romanbsd
Copy link

romanbsd commented May 1, 2020

This is the workaround for going "back":

$(document).on('turbolinks:before-cache', function() {
    const $picker = $('.selectpicker');
    const picker = $picker.data('selectpicker');
    if (picker) {
        picker.destroy();
        $picker.addClass('selectpicker');
    }
});

@wulman16
Copy link

wulman16 commented Jul 16, 2020

@romanbsd Your solution worked for me to an extent, but when navigating my application using Turbolinks, the selectpicker element would appear noticeably later than the rest of the page.

My solution ended up using the data-turbolinks-permanent attribute from the Turbolinks API (Persisting Elements Across Page Loads):

$(document).on('ready turbolinks:load', function() {
  $(window).trigger('load.bs.select.data-api');
}); 
<div id="selectpicker-parent" data-turbolinks-permanent></div>

@OpakAlex
Copy link

This is the workaround for going "back":

$(document).on('turbolinks:before-cache', function() {
    const $picker = $('.selectpicker');
    const picker = $picker.data('selectpicker');
    if (picker) {
        picker.destroy();
        $picker.addClass('selectpicker');
    }
});

Thanks man!

@tkuisma
Copy link

tkuisma commented Mar 19, 2021

This is the workaround for going "back":

That solution works, but only for one selectpicker. When there are multiple pickers, it is better to handle them all.

$(document).on('turbolinks:before-cache', function() {
    $('.selectpicker').each(function (i, el) {
        const picker = $(el).data('selectpicker');
        if (picker) {
            picker.destroy();
            $(el).addClass('selectpicker');
        }
    });
});

But with this solution the problem is that the selected values are lost when going "back".
So I'm trying to find a way to first store selected values and then restore them after the destroy call.

@adomigold
Copy link

This solution helped me out

function jsReload() { $('.selectpicker').selectpicker(); $('.flatpickr').flatpickr(); } $(document).ready(jsReload); $(document).on('page:load', jsReload);

Hope it will help you to.

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

No branches or pull requests