使用 ng 重复从可排序列表中获取每个项目的位置
Get position of each item from a sortable list with ng-repeat
我有一个用ng-repeat
创建的jquery-ui可排序<ul>
。当我点击我的提交按钮时,我想id
每个<li>
及其在列表中的位置。我知道有jquery index()
方法,但我不知道如何编写它。
<ul id="sortable">
<li class="" ng-repeat="ex in exercises">
<div class="row zone " id="ex.id">
<div class="zone-header"> {{ex.title_exercise}}
</div>
<div class="col-sm-12 zone-body">
Something
</div>
</div>
</li>
</ul>
<button type="submit" ng-click="generate()" class="btn blue-background">Generate</button>
您可以使用
可排序的toArray
和序列化方法,如下所示:
angular.module("app", [])
.controller("ctrl", function($scope) {
$scope.exercises = [{
"id": "ex_1",
"title_exercise": "one"
}, {
"id": "ex_2",
"title_exercise": "two"
}, {
"id": "ex_3",
"title_exercise": "three"
}];
$scope.generate = function() {
console.log($scope.toArray());
console.log($scope.serialize());
}
}).directive('sortable', function() {
return {
link: function(scope, element) {
element.sortable();
scope.toArray = function() {
return element.sortable("toArray");
}
scope.serialize = function() {
return element.sortable("serialize",{ key: "ex" });
}
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0-beta.1/jquery-ui.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<ul id="sortable" sortable>
<li class="" ng-repeat="ex in exercises" id="{{ex.id}}">
<div class="zone-header">{{ex.title_exercise}}</div>
</li>
</ul>
<button type="submit" ng-click="generate()" class="btn blue-background">Generate</button>
</div>
还要检查 ui 可排序模块,这样您就不必编写自己的可排序指令。
相关文章:
- Lodash 从与值数组不匹配的数组中获取项目
- 获取项目的顺序 jQuery 可排序并设置它们的顺序
- 拉力赛:有没有一种简单的方法来获取项目组合项目下的所有用户故事
- 如何在Sonarqube Javascript扩展中获取项目路径
- 下划线.js - OO:在索引处获取项目?有没有更好的JS库来做到这一点
- 使用 javascript 从本地存储获取项目
- ExtJS - 获取项目的完整路径
- 如何在HTML Angular JS中获取项目内部的项
- 如何从数组中的数组中获取项目
- 示例角度应用程序如何知道如何获取“项目”
- 从页面顶部获取项目偏移量
- 从会话属性获取项目
- 根据第一个字母获取项目
- extendscript:获取项目文件夹
- 获取项目名称
- 在javascript中使用过滤器时获取项目索引
- Jquery asp.net复选框列表获取项目值
- 使用jquery从多个具有相同名称的输入中获取项目
- JSON按值获取项目
- JavaScript数组如何在索引处获取项目