HTML代碼
<div ng-app="fiddle" columnly>
<button ng-click="addRow()">Add Row</button>
<button ng-click="move()">Move Column 1</button>
<table>
<tr class="row">
<th class="column move">Row 0 Head 1</th>
<th class="column">Row 0 Head 2</th>
<th class="column">Row 0 Head 3</th>
</tr>
<tr class="row" ng-repeat="item in items">
<td class="column first move">Row {{item}} Column 1</td>
<td class="column second">Row {{item}} Column 2</td>
<td class="column">Row {{item}} Column 3</td>
</tr>
</table>
</div>
Controller.js
var app = angular.module('fiddle', []);
app.directive('columnly', function() {
return {
restrict: 'A',
link: function(scope, element, attr, $scope) {
var count = 6;
scope.items = [1, 2, 3, 4, 5];
scope.firstClick = true;
scope.addRow = function() {
scope.items.push(count++);
if ($("tr th:nth-child(2)").hasClass('move')) {
setTimeout(function() {
var abc = $("tr:nth-child(" + count + ")").children().eq(0).html();
var def = $("tr:nth-child(" + count + ")").children().eq(1).html()
$("tr:nth-child(" + count + ")").children().eq(1).html(abc);
$("tr:nth-child(" + count + ")").children().eq(0).html(def);
$("tr:nth-child(" + count + ")").children().eq(1).addClass('move')
$("tr:nth-child(" + count + ")").children().eq(0).removeClass('move')
});
}
};
scope.move = function() {
jQuery.each($("table tr"), function() {
$(this).children().eq(1).after($(this).children().eq(0));
});
};
}
}
});
小提琴鏈接
[https://jsfiddle.net/Anvesh2727/jn1y44s6/10/][1]
聽起來像是你需要一個嵌套的NG-重複,其中列在自己的對象,基於作用域(每行)進行修改。 – isherwood
你面臨的問題是因爲你在混合使用jQuery DOM操作和AngularJS。理想情況下,而不是通過jQuery更改DOM,爲什麼不嘗試以編程方式更改數據? – nikjohn
我有一個想法 - 將使用'行'子指令''觀看'列順序數組並設置列順序類。將發佈我的答案。 – remarsh