A
A
AlexSer2018-07-04 10:32:30
Yii
AlexSer, 2018-07-04 10:32:30

How to pass parameter to Select2 Ajax by Kartik?

Hello! I have a table with products and Select2 from a card with product loading. Tired of entering the name of the product every time. Is it possible and how to pass the product id parameter by highlighting the (string) string in select2. So that he immediately selects the product. js gets the post id from the row that the item is selected. How to transfer?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2018-07-04
@AlexSer

Tired of entering the name of the product every time

Where to enter? Select2 is a dropdown, there is a list from which the selection occurs, you write only the beginning of the word in order to get a selection at this beginning. The number of characters before the list is regulated by the settings.
Yes, it is intended for this, I can’t imagine how else you use it. Actually, like a regular select, in value you have id in option you have names.
select and works, clicked, what was clicked got out. You either describe it strangely or made some kind of hell non-standard.
Where, ajax-th in some controller? Read value after click and pass. There are a bunch of events in the docks :
pluginEvents = [
    "change" => "function() { log('change'); }",
    "select2:opening" => "function() { log('select2:opening'); }",
    "select2:open" => "function() { log('open'); }",
    "select2:closing" => "function() { log('close'); }",
    "select2:close" => "function() { log('close'); }",
    "select2:selecting" => "function() { log('selecting'); }",
    "select2:select" => "function() { log('select'); }",
    "select2:unselecting" => "function() { log('unselecting'); }",
    "select2:unselect" => "function() { log('unselect'); }"
];

As I understand it, you are interested in the select2:select event.
And there are even examples in the docks of the plugin itself :
$('#mySelect2').on('select2:select', function (e) {
    var data = e.params.data;
    console.log(data);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question