select2 box with multiple selections

select2 box with multiple selections

mguinnessmguinness Posts: 85Questions: 12Answers: 1

I added the following reply to select2 box with dynamic url but it really deserves it's own question. Can this be fixed?

I've hit another issue when using ajax and multiple: true with the select2 plugin. The code in beforeSend passes the array value as a delimited string.

// Add an initial data request to the server, but don't
// override `data` since the dev might be using that
var initData = 'initialValue=true&value='+
    JSON.stringify(val);

That produces a url like http://localhost/api/Lookup?initialValue=true&value=["1","12"] which isn't ideal.

A better way would be to use the jQuery.param() function instead.

// Add an initial data request to the server, but don't
// override `data` since the dev might be using that
var initData = $.param({ initialValue: true, value: val }, true);

That produces a url like http://localhost/api/Lookup?initialValue=true&value=1&value=12 which is easier to work with.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin
    Answer ✓

    Yup - fair point! You could make that change directly in your copy of the plug-in.

    I'll need to have a bit of a think about this as if I change the plug-in and someone upgrades, it would break their existing install. I think a local patch would be the thing to do for the moment.

    Allan

  • mguinnessmguinness Posts: 85Questions: 12Answers: 1

    Thanks for the reply Allan. To maintain backwards compatibility an option like
    jsonParam: true|false (with default as true) could work.

    For the other question are you still planning to allow the ajax url option as a function in the next release?

    if (typeof(conf.opts.ajax.url) === 'function')
        settings.url = conf.opts.ajax.url();
    
  • allanallan Posts: 63,471Questions: 1Answers: 10,467 Site admin

    Yes. I think that would be a good move since DataTables and Editor allow that elsewhere.

    Allan

This discussion has been closed.