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

new tag position #270

Closed
bcail opened this issue Apr 7, 2016 · 9 comments
Closed

new tag position #270

bcail opened this issue Apr 7, 2016 · 9 comments

Comments

@bcail
Copy link
Contributor

bcail commented Apr 7, 2016

I would like to move the position of the new tag that the user's typing to the bottom of the list, instead of the top. That seems to be newly supported in select2 4.0.2 (select2/select2#4008). What's the best way to do this in django-select2?

@bcail
Copy link
Contributor Author

bcail commented Apr 8, 2016

Update: I got it working by doing the following:

  • copy django_select2.js to local app, and add the following:
    $('.django-select2').djangoSelect2({
    insertTag: function (data, tag) {
    data.push(tag);
    }
    });

  • in my local widget, I copied the following lines as below (to use the local version of django_select2.js instead of the default):

    def _get_media(self):
    return forms.Media(
    js=(settings.SELECT2_JS, 'local_app/django_select2.js'),
    css={'screen': (settings.SELECT2_CSS,)}
    )
    media = property(_get_media)

This worked, but is there a better way?

@codingjoe
Copy link
Collaborator

You don't need to copy the JS file. It's possible to overwrite the default JS options.
Check out how we do it for the heavy widget here.
Let me know if you have any further questions.

@bcail
Copy link
Contributor Author

bcail commented Apr 8, 2016

Thanks for replying. Are you saying that in my template I should override the initHeavy function, with the settings already listed in the django_select2.js file and adding the insertTag option?

Or is there a way to pass in the insertTag code as an option to the initHeavy function?

Sorry if I'm missing something obvious here...

@codingjoe
Copy link
Collaborator

No you should overwrite attrs['class'] and add your own class. After that you can write your own init method that calls the init method provided by django-select2. Im I right @anneFly ?

@bcail
Copy link
Contributor Author

bcail commented Apr 8, 2016

Hmm, I tried overriding attrs['class'] when I instantiate my widget, but the normal django-select2 classes are still added to my custom class, and the regular init functions are still called. Would I need to override the widget build_attrs method as well?

@codingjoe
Copy link
Collaborator

Yes, I just append the class in the build_attrs method.
Let me know if you have any good ideas or learnings on how to improve extensibility of this package.

@bcail
Copy link
Contributor Author

bcail commented Apr 11, 2016

Well, it seems like the user needs to be able to pass options into the javascript pretty easily, since there's this insertTag feature from select2 that has to be done in javascript.

I see this line in django_select2.js, that's called automatically:
$('.django-select2').djangoSelect2();

It seems like if this line could have some options that default to empty, but that the user could pass things in as needed (without overriding that file completely), that would be nice. But I'm not exactly sure how to do this.

One option would be to take out the $('.django-select2').djangoSelect2(); line and have the user put it in their template. That would add another step that everyone would need to do to use django-select2, but it would make it a lot easier to add custom javascript code, because you could just pass options through that call (without needing to override that file completely).

Even better would be if you could call djangoSelect2() conditionally in django_select2.js - call it automatically if the user hasn't overridden it, but use the user's version if they have one in their template.

Hopefully there's something helpful in these comments.

@codingjoe
Copy link
Collaborator

Hm... good point. Regular options you can base as widget attrs but callbacks wont work here. I'll try to figure something out.

@codingjoe codingjoe reopened this Apr 12, 2016
@codingjoe
Copy link
Collaborator

Closed: This issue has been inactive for an extended period of time. Please reopen the issue if it is still relevant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants