2013-10-31 24 views
0

我與此刻,在我目前的項目,我得從表1的行(其中一個按鈕被用戶點擊)追加到AngularJS工作第二張桌子。最重要的是,該按鈕還必須執行$ http.post(...)。AngularJS指令從一個表中的tablerow的追加到另一個表

所以我得到的是一個數據表由ng-repeat填充,並在其中一個td的我有一個按鈕與ng-click指令執行一個$ http.post函數。

HTML:

<table id="tableOne"> 
    <thead> 
     <th>ID</th> 
     <th>Name</th> 
     <th>Age</th> 
     <th></th> 
    </thead> 
    <tbody id="source"> 
    <tr ng-cloak ng-repeat="person in persons"> 
     <td>{{ person.id }}</td> 
     <td>{{ person.name }}</td> 
     <td>{{ person.age }}</td> 
     <td><button ng-click="movePerson(person.id)">Move to 2nd Table</button></td> 
    </tr> 
    </tbody> 
</table> 

當我按一下按鈕在我的控制器功能「movePerson(person.id)將被調用,並且工作正常

控制器:

$scope.movePerson = function (id) { 

    DataService.movePerson(id, { result: function (data, status) { 
     if(data.success === true) 
     { 
      $log.log("person has been moved"); 
     } 
    }, fault: function (data, status) { 
      $log.log("an error has occured"); 
    } }); 
}; 

'DataService'正在處理我的$ http請求。

現在我想移動與TBODY ID =「目標」裏的按鈕被點擊到另一個表(ID =「tableTwo」)的tablerow的。 我讀過你不應該在你的控制器使用jQuery ..所以有一個「角的方式」來做到這一點?因爲我想在我的項目中不包含jQuery。

我還以爲你會用一個指令對這類事情,所以,你可以只添加該指令的按鈕,標籤,如「NG單擊」是這樣的:

<td><button ng-click="movePerson(person.id)" move-tbl-row>Move to 2nd Table</button></td> 

但因爲我」對Angular來說,我不知道我的指令是怎麼樣的。那麼「ng-click」仍然會被執行嗎?

Geetings和感謝。

+0

你可以從你的指令邏輯函數內處理的整個過程,所以NG-點擊不會是必要再 – Sprottenwels

回答

1

你問如何移動的行....想着像jQuery ....時候,其實你想改變數組中的數據對象在。

這兒有你稍微修改。

<!-- pass whole object into scope function --> 
<button ng-click="movePerson(person)">Move to 2nd Table</button> 
$scope.movePerson = function (person) { 
    var id= person.id; 
    /* on ajax success, remove from persons array */ 
    var idx= $scope.persons.indexOf(person); 
    $scope.persons.splice(idx,1); 
    /* add person object to another scope array*/ 
    $scope.another_array.push(person); 


}) 
+0

感謝這個作品中,「..thinking如jQuery。」話幫一許多。不知道爲什麼我沒有想到這一點。還有的在你的答案有點筆誤,如果你要糾正它,它應該是'$ scope.persons.splice(IDX,1);' – Patrick

相關問題