Select2 add new option

Select2 add new option

accenturecfaccenturecf Posts: 4Questions: 1Answers: 0

Hi guys,

i'm stuck into a problem using select2.

That's the situation:
1. I already have my table working
2. There is a column fieldtype select2 that show results and it works with autocomplete.

Now i need to add a new option when enter is pressed and if the item is not found.

Look at the image

Can someone help me?

This question has an accepted answers - jump to answer

Answers

  • accenturecfaccenturecf Posts: 4Questions: 1Answers: 0
    edited February 12

    Maybe this informations could help:

    in the table definition there is this column:
    { data: 'group.name', editField: 'agent.group', className: 'editable'},

    in the editor definition there is this field:
    {label: "Agent Group:", name: "agent.group", type: "select2"},

    options are retrived by ajax call:
    var group = [];
    $.getJSON("/url",
    function(data) {
    var option = {};
    $.each(data, function(i, e) {
    option.label = e.name;
    option.value = e.idGroup;
    group.push(option);
    option = {};
    });
    }
    ).done(function() {
    editor.field('agent.group').update(group);
    });

    and submit is on blur:
    $('#agentsTable').on( 'click', 'tbody td.editable', function (e) { editor.inline( table.cell( this ).index(), { submit: 'allIfChanged', onBlur: 'submit' }); });

  • allanallan Posts: 48,908Questions: 1Answers: 7,132 Site admin
    Answer ✓

    Hi,

    In Select2 dynamic option creation is called tagging and their documentation has more information about it. The key is to enable the tags: true option for the Select2 configuration.

    Allan

Sign In or Register to comment.