gpt4 book ai didi

javascript - ng-repeat 随机显示数据

转载 作者:搜寻专家 更新时间:2023-10-31 21:07:04 24 4
gpt4 key购买 nike

我有从数据库返回的 json 对象数据。我的 json 对象的演示如下:

 $scope.info = [
{
"num": 1,
"col1": "demo1",
"col2": "demoX1"
},
{
"num": 2,
"col1": "demo2",
"col2": "demoX2",
},
................................
................................
{
"num": 15,
"col1": "demo15",
"col2": "demoX15",
}
.................................
................................
];

php::

 <?php
// sorted by num
$sql = "SET @num:=0; SELECT @num:=@num+1 AS num, t.* FROM table as t";
//convert array data to json data
$getData = json_encode(GetAllDataFromServer($sql));
?>

html::

 <table>
<tr ng-repeat="dbItem in info | slice: itemNum: currentItem">
<td>{{dbItem.num}}</td>
<td>{{others}}</td>
</tr>
</table>

<pagination total-items="info.length" ng-model="currentItem" max-size="7" class="pagination" boundary-links="true" items-per-page="15"></pagination>

<script>
angular.module("App")
.controller("NamController", function($scope) {
// json data from database
$scope.info = <?=$getData ?>;

$scope.currentItem = 1;
$scope.itemNum = 15;
});
</script>

当我在 ng-repeat 中显示 $scope.info json 数据时,它会随机显示数据,而不是基于 num

示例输出::

num  -  col1   -  col2
---------------------------
1 - demo1 - demoX1
15 - demo15 - demoX15
9 - demo9 - demoX9
............................
............................

我做了一些测试,从slice过滤器返回数据后,数据随机显示。

js::(切片过滤器)

myApp.filter('slice', function() {
return function(arr, itemNum, current) {
var lt = ((current-1)*itemNum);
var gt = +lt + +itemNum;
var data = {};
i = 0;
angular.forEach(arr, function(val, index) {
if ( index >= lt && index < gt ) {
data[i++] = val;
}
});
return data;
};
});

我该如何解决这个问题?提前致谢。

编辑::

js::(切片过滤器编辑)

myApp.filter('slice', function() {
return function(arr, itemNum, current) {
var begin = ((current-1)*itemNum);
var end = +begin + +itemNum;
return arr.slice(begin, end);
};
});

还是没有解决

最佳答案

您可以使用以下代码:

html::

 <table>
<tr ng-repeat="dbItem in info | startFrom:(currentItem-1)*itemNum | limitTo:itemNum">
<td>{{dbItem.num}}</td>
<td>{{others}}</td>
</tr>
</table>

<pagination total-items="info.length" ng-model="currentItem" max-size="7" class="pagination" boundary-links="true" items-per-page="{{itemNum}}"></pagination>

js:

 myApp.filter('startFrom', function() {
return function(input, start) {
start = +start; //parse to int
return input.slice(start);
}
});

进一步阅读 https://gist.github.com/kmaida/06d01f6b878777e2ea34

关于javascript - ng-repeat 随机显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30702664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com