select2 小部件如何使用 AJAX 填充? [英] How does a select2 widget gets populated with AJAX?

查看:33
本文介绍了select2 小部件如何使用 AJAX 填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在小部件中输入几个字符时,我会从 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.

这是我用于小部件的代码:

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};}'
                ]
            ]
        ]
);

我错过了什么?

推荐答案

您的小部件中缺少 formatResultformatSelection 定义.请参阅此代码示例.

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.

这篇关于select2 小部件如何使用 AJAX 填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆