des1roer
@des1roer
ученье - свет, а неученье - приятный полумрак

Yii 2 ajax обновление?

хочу реализовать такой же функционал как здесь des1roer.blogspot.ru/2015/04/yii-renderpartial.html

первоначально имею
<?php
    use execut\widget\TreeView;
    use yii\widgets\Pjax;
    use yii\helpers\Html;
    ?>
    <div class="main-default-index">

    <?php
    $items = [
        [
            'text' => 'Parent 1',
            'nodes' => [
                [
                    'text' => 'Child 1',
                    'nodes' => [
                        [
                           'text' => 'Grandchild 1'
                        ],
                        [
                           'text' => 'Grandchild 2'
                        ]
                    ]
                ],
                [
                    'text' => 'Child 2',
                    'href' => 'http://asutpdb.ru/362_363.php',
                ]
            ],
        ],
        [
            'text' => 'Parent 2',
        ]
    ];

    $onSelect = new \yii\web\JsExpression(<<<JS
        function (undefined, item) {
            $.pjax({
                container: '#pjax-container',
                url: (typeof item.href != "undefined")? ""+item.href : '',
                timeout: null
            });
        }     
    JS
    );

    echo $groupsContent = TreeView::widget([
        'data' => $items,
        'size' => TreeView::SIZE_SMALL,
        'clientOptions' => [
            'onNodeSelected' => $onSelect,
            'selectedBackColor' => 'rgb(40, 153, 57)',
            'borderColor' => '#fff',
        ],
    ]);
    ?>
    <?php Pjax::begin(); ?>
    <div id="pjax-container">sdfds</div>
    <?php Pjax::end(); ?>
    </div>


но pjax выводит данные в url

есть желание сделать через ajax и контроллер.
что-то вроде
echo CHtml::ajaxLink(
$text = 'Click me', 
array('req'), 
$ajaxOptions=array (
    'type'=>'POST',        
    'update'=>'#content',
    'data' => array( 'id' => '44'),
    ), 
$htmlOptions=array (       
)
);

но конечно желательно функцией, чтобы сработывало по оннодселект
в текущем виде ajax возвращает нужный код, но не обновляет див
$onSelect = new \yii\web\JsExpression(<<<JS
    function (undefined, item) {
        $.ajax({
            type: 'POST',
            url  : 'default/lol/?id=' + item.href,
            update: '#pjax-container',
            container: '#pjax-container',
           // data: (typeof item.href != "undefined") ? item.href : '',
            timeout: null
        });
    }     
JS
);

public function actionLol($id)
    {
        $page = $id; 
        // $page = Page::model()->findByPk($page)->link;    
        echo '
          <iframe id="ifr" src="' . $page . '" align="center">    
          </iframe>';
        //return $this->render('index');
    }
  • Вопрос задан
  • 1756 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы