2016-03-16 23 views
1

我正在開發一個Joomla組件和我使用AJAX對我的看法通過發送GET請求來獲取控制器數據庫表中的內容,然後通過創建模型的數據對象,我打電話給顯示PHP對象我的模型中的一個函數又返回一個JSON對象。
現在我的問題是通過jQuery在html表中顯示該對象的內容。如何使用jQuery

這裏是我的代碼:

jQuery("#papertitle").change(function(){ 
      console.log('eneterd jquery function'); 
      var title =jQuery('#papertitle').val(); 

      jQuery.ajax({ 
      type: 'GET', 
      url: "http://localhost/joomladev/index.php?option=com_journals&task=payments&format=raw&title="+title, 
      data: {title : title}, 
      success: function(data){ 
       console.log('data',data); 
       jQuery('#data-table').html(data); 
      } 
     }); 

    }); 

在數據表是我的股利。而且它顯示JSON對象作爲輸出是這樣的:

array (size=1) 
    0 => 
    object(stdClass)[165] 
     public 'paperid' => string '4' (length=1) 
     public 'journalid' => string '7' (length=1) 
     public 'papertitle' => string 'Big Data' (length=8) 
     public 'classification' => string 'computer science' (length=16) 
     public 'status' => string 'complete' (length=8) 
     public 'comments' => string 'none' (length=4) 

但我想以表格的形式來顯示物體的細節。 關於如何使用jQuery來做任何建議!

回答

2

我會在編碼控制器(http://localhost/joomladev/index.php)對象爲JSON並返回JSON。

然後你可以使用javascript解析該json。目前你得到的php對象轉儲不能/不應該在JS內部解析並且創建/填充表格。

+0

它worked..thanks試試這個! :) –

0

在你的Ajax成功功能

success: function(data){ 
    jQuery.each(data,function(key,row){ 
     jQuery('#data-table').append('<tr>'); 
     jQuery.each(row,function(key,column){ 
      jQuery('#data-table').append('<td>'+column+'</td>'); 
     }); 
     jQuery('#data-table').append('</tr>'); 
    }); 
}