標籤:blog status round more date 技術 echo cti color
功能實現:先輸出一頁的內容,然後點擊載入下一頁,
1.控制器中
1 /** 2 * 消費記錄:列出使用者購買章節的記錄 3 */ 4 public function actionMyPayHis() 5 { 6 //點擊載入更多 7 if( Yii::app()->request->isAjaxRequest ) { 8 //分頁 9 $pg = isset( $_GET[ "pg" ] ) ? $_GET[ "pg" ] : 1;10 $page = max( 0 , $pg );11 $offset = $page * 3;12 $accountLogs = B2cAccountLog::model()->findAll( array( ‘condition‘ => ‘user_id = :user_id and change_type = :change_type‘ ,13 ‘params‘ => array( ‘:user_id‘ => Yii::app()->user->getId() , ‘:change_type‘ => B2cAccountLog::NOVEL_USER_READ_PAY ) ,14 ‘order‘ => ‘change_time desc‘ ,15 ‘offset‘ => $offset ,16 ‘limit‘ => 317 ) );18 19 $tpl = ‘<tr>20 <td class="text-center">%s</td>21 <td class="text-center">%s 桃花幣</td>22 <td class="text-center">23 %s24 </td>25 </tr>‘;26 27 $data = array();28 $html = ‘‘;29 foreach ( $accountLogs as $accountLog ) {30 $html .= sprintf( $tpl ,31 $accountLog->change_desc ,32 DycUtil::toFloat( $accountLog->user_money ) ,33 DycUtil::getTim2DateTime( $accountLog->change_time , 1 )34 );35 }36 37 $data = array( ‘type‘ => ‘ok‘ , ‘info‘ => $html );38 39 echo CJSON::encode( $data );40 return;41 42 } else {43 44 $accountLogs = B2cAccountLog::model()->findAll( array( ‘condition‘ => ‘user_id = :user_id and change_type = :change_type‘ ,45 ‘params‘ => array( ‘:user_id‘ => Yii::app()->user->getId() , ‘:change_type‘ => B2cAccountLog::NOVEL_USER_READ_PAY ) ,46 ‘order‘ => ‘change_time desc‘ ,47 ‘offset‘ => 0 ,48 ‘limit‘ => 349 ) );50 51 $this->render( ‘myPayHis‘ , array( ‘accountLogs‘ => $accountLogs ) );52 }53 54 }View Code
先輸出第一頁的內容,再ajax請求輸出下一頁。
2.視圖中
1 <div id="czjl" style="padding: 0 20px;"> 2 <table class="am-table"> 3 <thead> 4 <tr> 5 <th class="text-center">圖書章節</th> 6 <th class="text-center">消費</th> 7 <th class="text-center">時間</th> 8 </tr> 9 </thead>10 <tbody id="data_tr">11 <?php12 foreach ($accountLogs as $accountLog) {13 ?>14 <tr>15 <td class="text-center"><?= $accountLog->change_desc ?></td>16 17 <td class="text-center"><?= DycUtil::toFloat( $accountLog->user_money ) ?>桃花幣</td>18 <td class="text-center">19 <?= DycUtil::getTim2DateTime( $accountLog->change_time , 1 ) ?>20 </td>21 22 </tr>23 <?php24 }25 ?>26 </tbody>27 </table>28 </div>29 <div style="display: flex;justify-content: space-around;">30 <div>31 <span class="btn btn-default" id="loadmore">載入更多</span>32 </div>33 </div>
載入更多,加一個 "id",綁定事件;"tbody"加一個"id",用來添加下一頁的內容。
3. JS實現
1 <script type="application/javascript"> 2 3 var url = ‘<?= $this->createUrl( ‘/Novel/Hyzx/MyPayHis‘ ) ?>‘; 4 var i = 1; 5 var load = $(‘#loadmore‘); 6 //根據頁數讀取資料 7 function getData(page) { 8 i++; //頁碼自動增加,保證下次調用時為新的一頁。 9 $.ajax({10 type: "get",11 url: url,12 data: {pg: page},13 dataType: "json",14 success: function (data) {15 if (data.info.length > 0) {16 $(‘#data_tr‘).append(data.info);17 } else {18 load.html(‘沒有更多的資料了....‘);19 load.unbind()20 }21 },22 error:function( request, textStatus ){23 alert(‘請求失敗,請稍候再試!‘ + textStatus);24 }25 });26 }27 28 29 //點擊載入下一頁30 $(function () {31 load.on(‘click‘, function(){32 getData(i);33 });34 });35 </script>
核心代碼,定義一個函數,然後點擊時,調用。點擊一次,"i"加1,保證最新一頁。
小記:小而美,工匠精神。
Yii 1開發日記 -- Ajax實現點擊載入下一頁