如何做一个选择二小部件被填充使用AJAX? [英] How does a select2 widget gets populated with AJAX?
问题描述
当我在我的窗口小部件输入几个字符,我得到这样的从阿贾克斯结果:
When I enter a few characters in my widget, I get results like these from AJAX:
[{"id":550,"campName":"IB Resi Showtime Rebate Website"},{"id":60,"campName":"OB ACGBK EB"}]
不过,我错过了如何填充这些结果的插件的部分。
However, I'm missing the part on how to populate the widget with those results.
下面是code我使用的小工具:
Here is the code I use for the widget:
$this->widget
(
'ext.ESelect2-master.ESelect2',
[
'selector'=>'#campaigns',
'options'=>
[
'tags'=>$tags,
'allowClear'=>true,
'minimumInputLength' => 2,
'width'=>'300px',
'multiple'=>true,
'placeholder'=>'Campaigns',
'ajax'=>
[
'url'=>Yii::app()->createUrl('CoxReports/autoCompleteCampaigns'),
'dataType'=>'json',
'data'=>'js:function(term,page){return{q: term, page_limit: 10, page: page};}',
'results'=>'js:function(data,page){ return {results:data};}'
]
]
]
);
我在想什么?
What am I missing?
推荐答案
你错过了一个 formatResult
和 formatSelection
在你的小部件定义。
请参阅<一href="https://github.com/schmunk42/p3media/blob/4e53edac11364e66da7ee83903e7f8395db2b7d8/components/P3MediaSelect.php"相对=nofollow>这code为例。
You're missing a formatResult
and a formatSelection
definition in your widget.
See this code for an example.
结果通过自定义标记的 formatResult
函数处理,结构类似于你。
The results are handled by the formatResult
function with custom markup, the structure is similar to yours.
这篇关于如何做一个选择二小部件被填充使用AJAX?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!