2012-01-07 50 views
0

我有一些Zend框架和AJAX的麻煩。ZF阿賈克斯一般如何

在視圖中,我有一個帶有製表符的div。當我點擊UnitTab「#tabUnits」時,單元視圖將顯示一個項目列表。還有一個鏈接來添加新的單位,所以添加視圖加載到選項卡。

的鏈接添加一個單位是:

<a id="addUnit" href="idBuilding/<?= $this->idBuilding ?>" href="#"><img width="16" height="16" src="/images/icons/fugue/plus-circle-blue.png"></a> 

和Ajax調用是:

$(document).ready(function(){ 

    var url = '/unit/add/'+$("#addUnit").attr('href'); 

    // ADD 
    $("#addUnit").click(function(event){ 
     event.preventDefault(); // link is not executed 

     $.ajax({ 
      url: url, 
      type: 'GET', 
      success: function(data) { 
       $("#tabUnits").html(data); 
      }, 
      error: function(){ 
       alert('An error occurred, please check before continue...'); 
      } 
     }); 

    }); 

}); 

當沒有發生錯誤添加單位,並再次顯示列表。

當我想編輯一個單元時,如何知道該id正在改變?

+0

不能得到你正在嘗試做的 – emaillenin 2012-01-07 19:57:15

回答

1

它與ZF沒什麼共同之處,它更多的僅僅是jQuery。如果您想要爲更多元素使用相同的AJAX調用,只需更改url變量即可。喜歡的東西(未測試):

<a class="link" data-url="add" id="addUnit" href="idBuilding/<?= $this->idBuilding ?>" href="#"><img width="16" height="16" src="/images/icons/fugue/plus-circle-blue.png"></a> 
<a class="link" data-url="edit" id=editUnit" href="idBuilding/<?= $this->idBuilding ?>" href="#"><img width="16" height="16" src="/images/icons/fugue/plus-circle-blue.png"></a> 


$(document).ready(function() { 

    var url; 

    // ADD 
    $(".link").click(function(event){ 
     event.preventDefault(); // link is not executed 

     url = '/unit/' + $(this).data('url') + '/' + $(this).attr('href'); 

     $.ajax({ 
      url: url, 
      type: 'GET', 
      success: function(data) { 
       $("#tabUnits").html(data); 
      }, 
      error: function(){ 
       alert('An error occurred, please check before continue...'); 
      } 
     }); 

    }); 

}); 

更多數據參數:http://api.jquery.com/data/#data-html5

+0

謝謝你很多,你救了我的一天;) – cwhisperer 2012-01-08 11:07:32