角排序依据排序号在NG-重复文本 [英] Angular orderBy number sorting as text in ng-repeat

查看:119
本文介绍了角排序依据排序号在NG-重复文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数据:

[{"id":"42","firstname":"Sarah","lastname":"Dilby","age":"40","cars":"Yaris"},
{"firstname":"Jason","lastname":"Diry","age":"5","id":"5"},
{"id":"6","firstname":"Bilson","lastname":"Berby","age":"1","cars":"Tipo"}]

当我在NG-重复排序依据ID或按年龄,其排序为文本的数量。因为我找不到它写,这是任何地方的问题,我猜有一个与我的code的一个问题。我创造了这个小提琴: http://jsfiddle.net/vsbGH/1/ 很抱歉的模板,但没有按的jsfiddle ŧ允许在HTML框。无论如何,这是code加载并排序数据:

When I orderBy id or by age in an ng-repeat, it sorts the number as text. Since I can't find it written that this is an issue anywhere, I'm guessing there's a problem with my code. I have created this fiddle: http://jsfiddle.net/vsbGH/1/ Sorry about the template, but jsfiddle doesn't allow in the html box. Anyway, this is the code which loads and sorts the data:

//user data
app.service('People', function() {
var People = {};
People.details = [{"id":"42","firstname":"Sarah","lastname":"Dilby","age":"40","cars":"Yaris"},
                  {"firstname":"Jason","lastname":"Diry","age":"5","id":"5"},
                  {"id":"6","firstname":"Bilson","lastname":"Berby","age":"1","cars":"Tipo"}]
return People;
});

//list ctrl
controllers.listCtrl = function ($scope,People) {
 $scope.people = People.details;

 $scope.sortList = function(sortname) {
    $scope.sorter = sortname;
 }
}

这是模板的NG-重复的部分:

And this is the ng-repeat part of the template:

<tr ng-repeat="person in people | orderBy:sorter ">
        <td>{{person.id | number}}</td>
        <td>{{person.firstname}} </td>
        <td>{{person.lastname}} </td>
        <td>{{person.age | number}}</td>
        <td>{{person.cars}} </td>
 </tr>

非常感谢,如果你能帮助我理解为什么一些数据不排序的数字,为什么它的排序为文本。

Many thanks if you can help me understand why the number data isn't sorting as numbers, and why it's sorting as text.

推荐答案

我认为最合适的解决方案是在我的JSON对象正确地格式化我有号码,即不换行他们的报价。所以:

I think the most appropriate solution is to format the numbers I have on my JSON objects correctly, ie not to wrap them in quotes. So:

 [{"id":"42","firstname":"Sarah","lastname":"Dilby","age":"40","cars":"Yaris"},
  {"firstname":"Jason","lastname":"Diry","age":"5","id":"5"},
  {"id":"6","firstname":"Bilson","lastname":"Berby","age":"1","cars":"Tipo"}]

变成了:

[{"id":42,"firstname":"Sarah","lastname":"Dilby","age":40,"cars":"Yaris"},
 {"firstname":"Jason","lastname":"Diry","age":5,"id":5},
 {"id":6,"firstname":"Bilson","lastname":"Berby","age":1,"cars":"Tipo"}]

我猜SergL的解决方案是好的,如果它不可能纠正JSON数据的格式。

I'm guessing SergL's solution is good if it's not possible to correct the format of the JSON data.

要加入到这一点,在我的具体情况下,这个问题是在服务器端PHP的json_en code函数来做。默认情况下,它把数字作为字符串。要解决我不得不在 JSON_NUMERIC_CHECK 选项添加到PHP脚本的连接code方法:

To add to this, the issue in my specific case is to do with PHP's json_encode function on the server side. By default, it treats numbers as strings. To fix I had to add the JSON_NUMERIC_CHECK option to the encode method in the PHP script:

json_encode($assoc_array,JSON_NUMERIC_CHECK);

这篇关于角排序依据排序号在NG-重复文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆