2015-12-22 36 views
1

我在做一個表。定位和編輯新創建的行?

我做了我行可編輯:ng-click="todo.editing = !todo.editing"

的index.html

<tr ng-repeat="todo in todos> 
    <td> 
     <div ng-hide="todo.editing">{{ todo.id }} </div> 
     <div ng-show="todo.editing"><input type="id" ng-model="todo.id" /></div> 
    </td> 
    <td>     
     <div ng-hide="todo.editing">{{ todo.text }}</div> 
     <div ng-show="todo.editing"><input type="text" ng-model="todo.text" /></div> 
    </td> 
</tr> 

<button class="btn btn-warning btn-sm ng-scope" ng-click="todo.editing = !todo.editing"><span class="glyphicon glyphicon-pencil"></span></button> 

我做了一個按鈕,將新行添加到表:

的index.html

<button type="button" class="btn btn-default btn-block " ng-click="addRow($storage.todos)">New record</button> 

script.js

$scope.addRow = function (arr) { 
    console.log(arr); 
    arr.push({'id':$scope.id, 'text': $scope.text}); 
}; 

現在我想擴展我的addRow()函數,這樣我就可以添加一個新行並同時將自己定位在該行,並將我置於編輯模式。

位置的問題是我正在使用分頁。假設我在分頁第一頁,新排在第四頁。我想自動到達那裏。

有人可以給我一個提示嗎?謝謝你的時間。

+0

你的文本字段在哪裏? – messerbill

+1

要將新行置於可編輯模式,'arr.push({id:$ scope.id,text:$ scope.text,editing:true})'? – Komo

+1

您將需要存儲對正在編輯的行的引用。之後,您需要創建某種類型的「編輯模式」。你可以通過在ng-if中包含ng-repeat的內容來做到這一點。如果當前ID是正在編輯的ID,則會顯示帶有輸入字段而不是純文本的表單。 – Marie

回答

1

只需在推送過程中使editing屬性值true

像這樣

arr.push({'id':$scope.id, 'text': $scope.text,editing:true}); 
1

我創建了一個琴:

https://jsfiddle.net/0t1trq6m/

addRow()方法現在看起來像:

$scope.addRow = function() { 
console.log("test"); 
var e = new Entry(); 
e.id = $scope.todos.length; 
e.text = $scope.content; 
$scope.todos.push(e); 
} 

聖誕快樂;)

+0

聖誕快樂! –