dongxie9448 2015-04-07 10:30
浏览 57
已采纳

使用YII Framework选择2 ajax选项

I am using Yii framework on a project and i am using an extension which uses select2 jquery. I am unable to grasp how the implementation for ajax works with this extension or the select2.

My ajax call returns the following json.

    [
    {"id":"1", "text" : "Option one"},
    {"id":"1", "text" : "Option one"},
    {"id":"1", "text" : "Option one"}
    ]

The yii extension enfolds the select2 extension as below

$this->widget('ext.select2.ESelect2', array(
                    'name' => 'selectInput',
                    'ajax' => array(
                       'url'=>Yii::app()->createUrl('controller/ajaxAction'),
                        'dataType' => 'json',
                        'type' => 'GET',
                        'results' => 'js:function(data,page) { 
  var more = (page * 10) < data.total; return {results: data, more:more };
                             }',
                        'formatResult' => 'js:function(data){
                                 return data.name;
                              }',
                        'formatSelection' => 'js: function(data) {
                                return data.name;
                              }',
                    ),
                ));

I found a related question from this Question! The link to the extension am using is YII select2 Extention!

  • 写回答

1条回答 默认 最新

  • donglu1881 2015-04-14 12:41
    关注

    So a week later i merged with the answer to this question.

    First let me highlight how the select2 ajax or in my case the Yii ESelect Extension.

    The ajax options for jquery are the same as for the Eselect Extention i.e. url,type and datatype altho there is a slight difference on the format returned after successfully querying.

    As for the result set for Eselect/select2 expects two parameters to be returned. that is

    id : data.myOptionsValue;
    text : data.myOptionText;
    

    Reference :: https://select2.github.io/options.html#ajax

    if we want to customize the format for the result set that is retured we can go a head and extend the plugin by using

     'formatResult' => 'js:function(data){
                                         return data.name;
                                      }',
                                'formatSelection' => 'js: function(data) {
                                        return data.name;
                                      }',
    

    I also had an issue getting my head around how the extention was quering. A look around and i realised that we have two datatype jsonp and json these two datatypes will handle data differently.

    Jsonp (json padding) allows sending query parameters when querying. As for my case i am not passing any other parameters e.g an authkey e.t.c. In my case i changed the datatype to json and returning a json with id and text as results. See below my working snippet.

     echo CHtml::textField('myElementName', '', array('class' => 'form-control col-lg-12'));
    $this->widget('ext.select2.ESelect2', array(
                    'selector' => '#myElementName',
                    'options' => array(
                        'placeholder' => 'Search ..',
                        'ajax' => array(
                            'url' => Yii::app()->createUrl('controller/ajaxAction'),
                            'dataType' => 'json',
                            'delay' => 250,
                            'data' => 'js: function(term) {
                        return {
                            q: term,
                        };
                    }',
                            'results' => 'js: function(data){
    
    
                    return {results: data }
                }',
                        ),
                    ),
                ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败