optgroup in select2 plugin of datatables editor

optgroup in select2 plugin of datatables editor

JerryhXuJerryhXu Posts: 7Questions: 3Answers: 0

Hi,

I just bought DataTables Editor license and I would like to use select2 plugin for multi-select dropdown box. I can't figure out a way to make <optgroup> to work with the plugin. Does anyone know? If so, any sample?

Thank you very much.

Jerry

Answers

  • allanallan Posts: 54,909Questions: 1Answers: 8,606 Site admin

    Hi Jerry,

    Thanks for your question. The key here is the data format that Select2 expects. As their documentation notes, you can create an optgroup by nesting the data under a children property.

    What might also be of interest for you here is that Editor uses a default of value and label for the value and label properties of the select dropdown (to match its built in select, etc field types). That is controlled by the options property which is documented here.

    Let me know how you get on with it.

    Regards,
    Allan

  • loukinglouking Posts: 242Questions: 48Answers: 0

    Is there any example on how to use select2 with optgroups?

    This thread gives ideas but no clear example. I don't quite understand how to "connect" what select2 expects with how the editor must be configured. (BTW, seems the select2.org website has been down for last couple of days so I found the relevant documentation at https://github.com/select2/select2/blob/develop/docs/pages/06.data-sources/01.formats/docs.md)

    I see https://datatables.net/forums/discussion/comment/133479/#Comment_133479 which makes me think there was a solution/example provided, but that it was sent by email.

  • colincolin Posts: 12,939Questions: 0Answers: 2,204

    No, there's no select2 examples yet, I'm afraid. It's on my list of things to do, but it's an ever-expanding list. It would be worth looking at other forum threads for guidance. For the reply you quoted, that would've been discussing logins to private pages, otherwise the chat is always in the forum.

    If you're having trouble, please post a link to your page or create a test case, and we're be happy to take a look,

    Colin

  • loukinglouking Posts: 242Questions: 48Answers: 0
    edited March 2020

    Continued review of the forums found https://datatables.net/forums/discussion/comment/141409/#Comment_141409 which seems to indicate that optgroup is not possible yet with editor, but is on the list for future enhancements.

    So I suppose trying to do a test case wouldn't be fruitful.

    As this post was 1.5 years ago, can you tell me if this is coming "soon"? :)

  • allanallan Posts: 54,909Questions: 1Answers: 8,606 Site admin

    Its not on our immediate work list I'm afraid. The only thing that really needs to be figured out is the format for the JSON and how to make that configurable.

    That said, there should be no reason why Select'2 optgroups won't work within the plug-in for it. This is their documentation for their grouped data format. Returning that from the server for the JSON options should allow it to be populated in.

    Regards,
    Allan

  • loukinglouking Posts: 242Questions: 48Answers: 0
    edited March 2020

    I've gotten something to sort of work using opts. The issue I see is that the group is repeated for some reason.

    http://live.datatables.net/liveluqu/1/

    Any ideas why? I've tried without and with options:[], getting the same result. I can't debug because it's a minimized verison of editor on the server. I suppose I could use another full version from somewhere, though, if you don't have time to look.

  • allanallan Posts: 54,909Questions: 1Answers: 8,606 Site admin
    edited April 2020

    Here is a little tweak to fix that: http://live.datatables.net/yipohadu/1/edit .

    I've commented out the first of these two lines in the create plug-in function:

    conf._input.select2( options );
    

    It looks like Select2 adds the data twice when called with the data property multiple times.

    Allan

  • loukinglouking Posts: 242Questions: 48Answers: 0

    Perfect -- thanks! Hmm, I guess because I didn't create the thread I can't mark it as "answered". Hopefully @jerryxgh sees this.

Sign In or Register to comment.