Angular orderBy number sorting as text in ng-repeat

前端 未结 7 1450
天命终不由人
天命终不由人 2020-11-29 08:10

I have this data:

[{\"id\":\"42\",\"firstname\":\"Sarah\",\"lastname\":\"Dilby\",\"age\":\"40\",\"cars\":\"Yaris\"},
{\"firstname\":\"Jason\",\"lastname\":\"         


        
相关标签:
7条回答
  • 2020-11-29 08:52

    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"}]
    

    becomes:

    [{"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"}]
    

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

    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);
    
    0 讨论(0)
提交回复
热议问题