我有一个问题,我找不到一种方法,如何在json中从php获取分页,以下是我的代码:
$type_name = substr(trim($_POST['type']),1);
$criteria = new CDbCriteria();
$count=ImageLibrary::model()->count($criteria);
$pages=new CPagination($count);
$pages->pageSize=10;
$pages->applyLimit($criteria);
$criteria->select = 'name,type';
$criteria->condition = 'type=:type';
$criteria->params = array('type' => $type_name);
$model = ImageLibrary::model()->findAll($criteria);
foreach( $model as $key => $data){
$json_data[$key]['name'] = $data->name;
$json_data[$key]['type'] = $data->type;
}
echo CJSON::encode($json_data);感谢您的帮助!
发布于 2013-07-19 21:16:18
不一定非得这样做。您可以呈现包含分页小部件的整个HTML结果。
来自我的一个项目的示例:
$count = AdStavke::model()->count($criteria);
$pages = new CPagination($count);
$pages->pageSize = 20;
$pages->applyLimit($criteria);
$stavke = AdStavke::model()->findAll($criteria);
if (sizeof($stavke)) {
$response = array(
'success' => true,
'html' => $this->renderPartial($types[$type]['view_file'], array(
'model' => $stavke,
'pages' => $pages,
'nezaduzene' => true,
), true),
);
}在视图中:
<div id="appTable">
<table class="tablesorter stavke zaduzene table table-striped table-hover table-condensed table-bordered">
<thead>
<tr class="descriptioner">
<th colspan="9">
<div class="pull-left">
Lista <span class="label label-warning" style="font-size: 14px;">zaduženih</span> stavki u: <?= $model[0]->stavka->vrsta->naziv; ?>
</div>
<div class="pull-right">
<?php
$this->widget('CLinkPager', array(
'htmlOptions' => array(
'class' => 'yiiPager yiiPager_2',
),
'pages' => $pages,
'header' => false,
'firstPageLabel' => 'Prva',
'lastPageLabel' => 'Zadnja',
'prevPageLabel' => 'Prethodna',
'nextPageLabel' => 'Sljedeća',
));
?>
</div>
<div class="clearfix"></div>
</th>
</tr>
</thead>
</table>
</div>您可以发送一个简单的AJAX请求来使用Updated-Pagination更改入口视图
$.post('/aplikacije/ajax/fetch', {
submit: true,
type: 'stanja',
seek: mapped.data('stanje'),
vrsta_id: $('.vrstex.active').first().data('vid'),
q: _mapped.val()
}, function(data) {
if(data.success) {
$('#appTable').html( data.html );
}
}, 'json');注意:这段代码是从我的一个实时项目中派生出来的,从中你可以得到如何使用AJAX进行Yii分页的想法。
https://stackoverflow.com/questions/17746913
复制相似问题