noganno
9/4/2013 - 2:02 PM

http://www.eha.ee/labs/yiiplay/index.php/en/site/extension?view=select2 ajax init selection, ajax suggest options, change event load data wi

http://www.eha.ee/labs/yiiplay/index.php/en/site/extension?view=select2 ajax init selection, ajax suggest options, change event load data with ajax

$this->widget('ext.widgets.select2.XSelect2', array(
    'model'=>$model,
    'attribute'=>'id',
    'options'=>array(
        'minimumInputLength'=>2,
        'ajax' => array(
            'url'=>$this->createUrl('/request/suggestPerson'),
            'dataType'=>'json',
            'data' => "js: function(term,page) {
                return {q: term};
            }",
            'results' => "js: function(data,page){
                return {results: data};
            }",
        ),
        'initSelection' => "js:function (element, callback) {
            var id=$(element).val();
            if (id!=='') {
                $.ajax('".$this->createUrl('/request/initPerson')."', {
                    dataType: 'json',
                    data: {
                        id: id
                    }
                }).done(function(data) {callback(data);});
            }
        }",
    ),
    'events'=>array(
        'change'=>"js:function (element) {
            var id=element.val;
            if (id!='') {
                $.ajax('".$this->createUrl('/request/listPersonsWithSameFirstname')."', {
                    data: {
                        id: id
                    }
                }).done(function(data) {
                    $('#name-list').text(data);
                });
            }
        }"
    ),
    'htmlOptions'=>array(
        'style'=>'width:300px'
    ),
));