2014-01-06 55 views
0

1更新模板數據:index.html的細枝:從Ajax調用

<!-- begin filter --> 
<input type="radio" id="" name="top10" value="top10">Top 10 
<input type="radio" id="" name="top20" value="top20">Top 20 
<input type="radio" id="" checked="checked" name="all" value="all">All 
<!-- end filters --> 

{% for user in users %} 
    {% include 'userslist.html' %}  
{% endfor %} 

在 '的index.html' 的默認操作是列表中的所有用戶。當我更改過濾器時,這會在'user.js'中調用ajax函數。

2:user.js的

$.ajax({ 


         url: 'user/find/'+params, 
         type: 'get', 
         data: null, 
         async: false, 
         dataType: 'html', 
         success: function(dataJson){ 
          ????? 
         }, 
         error: function(jqXHR, textStatus, errorThrown){}, 
         complete: function(jqXHR, textStatus){} 
        }); 

        return jsonContratos; 
       } 

此AJAX函數調用類user.php的

3:user.php的

class User extends AppRequest 
{  

     public function __construct(){} 

     public function index_action() 
     { 
     $this->template('index.html', Array('users' => $this->find())); 
     } 

     public function find() 
     { 
     $arrayUsers = dataBase->findUsersByFilters($_GET['params']); 

     if($this->isAjaxRequest()){ 

      $array = Array('users' => $arrayUsers) 

      $this->template('index.html', $array); 
     }else{ 
      return $arrayUsers; 
     } 

     } 
} 

問題: - 我可以如何發送新用戶lis t到模板,使用ajax回調? - 或者做這個的最好方法是什麼? - Json可以做到這一點嗎?

+0

歡迎來到Stackoverflow!第一句話:'async:false' - > no。 – moonwave99

回答

0

如前所述,請勿使用async: false。根據定義,AJAX是異步的。關於您的問題:只需在成功回調中更新您的視圖即可。您返回HTML,所以只需注入它:

$.ajax({ 
    url: 'user/find/'+params, 
    type: 'get', 
    data: null, 
    async: false, 
    dataType: 'html', 
    success: function(data, textStatus, jqXHR){ 
     // for example 
     $("#some-id-or-class-of-your-main-element").html(data); 
    }, 
    error: function(jqXHR, textStatus, errorThrown){}, 
    complete: function(jqXHR, textStatus){} 
}); 
+0

你救我.... – user2236305

+0

有沒有辦法用json做到這一點?我有樹枝模板,頁面上的每個報價都由100多行格式的html代碼組成,無空格對於一個幾乎爲零。但是,如果我想顯示這些優惠中的15個,這是非常大的html響應。所以我問是否是一些可能的方式,用JS和json數據處理樹枝模板。謝謝 –

+0

@ErikKubica當然可能,但你最好發表一個新問題... – nietonfir