D
D
des1roer2015-07-30 14:13:53
JavaScript
des1roer, 2015-07-30 14:13:53

Yii 2 ajax update?

I want to implement the same functionality as here des1roer.blogspot.ru/2015/04/yii-renderpartial.html

originally I have

<?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>


but pjax displays data in the url

, there is a desire to do it through ajax and a controller.
something like
echo CHtml::ajaxLink(
$text = 'Click me', 
array('req'), 
$ajaxOptions=array (
    'type'=>'POST',        
    'update'=>'#content',
    'data' => array( 'id' => '44'),
    ), 
$htmlOptions=array (       
)
);

but of course it is desirable for the function to work on onnoselect
in the current form ajax returns the desired code, but does not update the div
$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');
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question