2016-12-27 86 views
2

我使用的是twbs分頁插件,每次點擊頁碼時,ajax調用被觸發,每頁獲得10個結果。 在頁面上傳中,前10個結果被提取並分配給smarty變量。通過ajax使用twbs更新smarty變量分頁插件

這是tpl文件。我分配從PHP給變量batchesData所取得的陣列和使用foreach循環以表格的形式來顯示它現在

<table class="table table-striped projects tablesorter" 
    id="batchTable" style="font-size: 13px;"> 
<th> 
..... 
..... 
</th> 
<tbody> 
{if !empty($batchesData)} 
{foreach from = $batchesData key = filter item = value} 
<tr> 
...... 
...... 
</tr> 
{/foreach} 
    </tbody> 
</table> 

頁上點擊迭代它,這是我的js代碼,我可以根據獲取陣列頁面沒有來自PHP,但我怎麼更新它,因爲我猜想smarty執行是在ajax調用之前完成的。請建議

$(function() { 
     window.pagObj = $('#pagination').twbsPagination({ 
      totalPages: 35, 
      visiblePages: 10, 
      onPageClick: function (event, page) { 
       $.ajax({ 
        type : 'POST', 
        url : '/mbatch/batch/listBatch', 
        data : { 
         page: page, 
        }, 
        beforeSend : function() { 

        }, 
        success : function(response) { 
        /* Handling */ 
        }, 
        error : function(data) { 
         new PNotify({ 
          title: 'Notice', 
          text: JSON.parse(data.responseText).message, 
          type: 'error', 
          styling: 'bootstrap3' 
         }); 
        } 
       }); 
      } 
     }).on('page', function (event, page) { 
     }); 
    }); 

任何線索將不勝感激。這裏是插件的鏈接 http://www.jqueryscript.net/demo/Simple-Customizable-Pagination-Plugin-with-jQuery-Bootstrap-Twbs-Pagination/

回答

2

試試這個:

 $(function() { 
    window.pagObj = $('#pagination').twbsPagination({ 
     totalPages: 35, 
     visiblePages: 10, 
     onPageClick: function (event, page) { 
      $.ajax({ 
       type : 'POST', 
       url : '/mbatch/batch/listBatch', 
       data : { 
        page: page, 
       }, 
       beforeSend : function() { 

       }, 
       success : function(response) { 
       $("#batchTable").empty();//clearing the current table rows 
       var dataRes=JSON.parse(response); 
       var resultCount=dataRes.length; //Assuming result set in array form. 
       //now creating row for each result set and appending to table 
       $.each(dataRes,function(index,value){ 
       $("#batchTable").append("<tr> 
       <td>"+ 
       value.item1+" 
       </td> 
       <td>"+ 
       value.item2+" 
       </td> 
       </tr>") 
       }) 
       }, 
       error : function(data) { 
        new PNotify({ 
         title: 'Notice', 
         text: JSON.parse(data.responseText).message, 
         type: 'error', 
         styling: 'bootstrap3' 
        }); 
       } 
      }); 
     } 
    }).on('page', function (event, page) { 
    }); 
}); 

假設你的反應將是JSON格式。在success函數中相應地設置你的格式

+0

是的,這似乎是一個明顯的解決方案,但我希望能夠更新/刷新我的smarty變量。 由於在我的表中有很多條件語句,所以通過javascript附加HTML將是一個痛苦 –

+1

聰明的變量一旦渲染沒有任何轉換爲​​純Htmls。 請清除你的概念:smarty變量是服務器端變量,一旦服務器將所有數據轉儲到html頁面,javascript就會在客戶端運行。 JavaScript無法更新smarty變量。 –