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

SortableStackInline breaks with Select2 #216

Open
mcat opened this issue Apr 3, 2014 · 3 comments
Open

SortableStackInline breaks with Select2 #216

mcat opened this issue Apr 3, 2014 · 3 comments

Comments

@mcat
Copy link

mcat commented Apr 3, 2014

Line 135 in sortables.js:
https://github.com/darklow/django-suit/blob/master/suit/static/suit/js/sortables.js#L135

|| $set_block.find(":input[value!='']").filter(filter_hidden).filter(filter_unchanged).serialize()){

Overly aggressively selects all hidden fields with a value. This Causes problems with the Select2 django integration because Selec2 uses a hidden input field to store the value of it's faux selectbox.

Specifically filtering for Select2 classes solves the problem but is too specific to Select2.

        function filter_hidden(i, input){
            return $(input).hasClass('select2-offscreen') || input.type !== 'hidden';
        }

        // Update input count right before submit
        if ($inputs && $inputs.length) {
            var $last_input = $inputs.last();
            var selector = $(this).selector;
            $($last_input[0].form).submit(function (e) {
                var i = 0, value;
                $(selector).each(function () {
                    var $input = $(this);
                    var fieldset_id = $input.attr('name').split('-')[0];
                    // Check if any of new dynamic block values has been added
                    var $set_block = $input.closest('.dynamic-' + fieldset_id);
                    if (!$set_block.length
                        || $set_block.hasClass('has_original')
                        || $set_block.find(":input[value!='']").filter(filter_hidden).filter(filter_unchanged).serialize()) {
                        value = i++;
                        $input.val(value);
                    }
                });
            });
        }

Fields that should be excluded should have a special class rather then relying on type="hidden"

@darklow
Copy link
Owner

darklow commented Apr 4, 2014

What this checking does is it verifies if any of new fieldset inputs has changed value, so that if you click multiple times to Add inline and enter none of fields it wouldn't add order value, otherwise on form save, empty inlines will be saved (because order field will have value).

I'll try adding select2 to demo inline and see what i can do to fix the problem.

@darklow
Copy link
Owner

darklow commented Apr 14, 2014

I tried replicate problem, but everything seemed fine (except select2 won't work on new inlines, but that's another problem.

Overly aggressively selects all hidden fields with a value. This Causes problems with the Select2 django integration because Selec2 uses a hidden input field to store the value of it's faux selectbox.

Could you please be more specific, what problems exactly it causes?
Code example would be even bettter.

@mcat
Copy link
Author

mcat commented Jul 18, 2014

The issue I ran into was that the order was not maintained. Did your test properly maintain the order of items in the sortable?

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

2 participants