Quantcast
Channel: Live News for Yii Framework
Viewing all articles
Browse latest Browse all 3361

[Wiki] How to send POST data from CActiveForm with a CListView inside

$
0
0

myview.php

We need to send POST data on clicking any pagination buttons. To achieve this, we need to 1) insert a hidden field and 2) write some jQuery.

<?php
$script ='
    $(\'.page\').click(function(){
        submitForm(this);
        return false;
    });
    $(\'.first\').click(function(){
        submitForm(this);
        return false;
    });
    $(\'.previous\').click(function(){
        submitForm(this);
        return false;
    });
    $(\'.next\').click(function(){
        submitForm(this);
        return false;
    });
    $(\'.last\').click(function(){
        submitForm(this);
        return false;
    });         
    function submitForm(obj)
    {
        var href = $(obj).find(\'a\').attr(\'href\');
        var pos = href.indexOf(\'page/\');
        var page = 1;
        if(pos > 0)
            page = href.substring(pos + 5);
        $(\'#page\').val(page);
        $(\'#myform\').submit();
    }
';
Yii::app()->clientScript->registerScript('script', $script);
?>
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'myform',
    'enableClientValidation'=>false,
    'clientOptions'=>array(
        'validateOnSubmit'=>true,
    ),
)); ?>
        <?= CHtml::hiddenField('page', ''); ?>
        <?php $this->widget('zii.widgets.CListView', array(
            'ajaxUpdate'=>false,
            'dataProvider'=>$dataProvider,
            'itemView'=>'_view',
            'template'=>"{items}\n{pager}",
        )); ?>
<?php $this->endWidget(); ?>

MyController.php

$page = 1;  
    if(isset($_POST['page']))
    {
        $page = $_POST['page'];
    }
 
    $dataProvider = new CActiveDataProvider('Post',array(
            'pagination'=>array(
                'pageSize'=>5,
                'currentPage'=> $page - 1,
            ),
            ));
 
    $this->render('myview', array(
        'dataProvider' => $dataProvider,
    ));

Viewing all articles
Browse latest Browse all 3361

Trending Articles