排序不适用于下拉菜单

时间:2022-11-25 19:41:01

I have a dropdown in my angular.js webapp like:

我在angular.js webapp中有一个下拉列表,如:

<select ng-model="script"
        ng-options="s as s[1].shortDescription for s in objs | orderBy:'toString()'" 
        ng-change="updateParam()">
</select>

where the objects in the array are type of

数组中的对象的类型

["test1", {shortDescription: "first test", longDescription: "AAAAAAAA"}]

I want to sort by shortDescription property of each entry. The problem is that the sort doesn't work.

我想按每个条目的shortDescription属性排序。问题是排序不起作用。

UPDATE1

UPDATE1

Sample data

样本数据

["test1", {shortDescription: "first test", longDescription: "AAAAAAAA"}]
["test2", {shortDescription: "2nd test", longDescription: "BBBB"}]
["test3", {shortDescription: "3rd test", longDescription: "CCCC"}]

2 个解决方案

#1


0  

It looks like you want the following

看起来你想要以下内容

| orderBy: 'shortDescription'

Where you are sorting by property shortDescription. See the orderBy docs for more information. Here is a simplistic example demonstrating this...

您按属性shortDescription排序的位置。有关更多信息,请参阅orderBy文档。这是一个简单的例子来证明这一点......

<select ng-model="script"
        ng-options="s as s.shortDescription for s in objs | orderBy: 'shortDescription'">
</select>

$scope.objs = [{shortDescription: "first test", longDescription: "AAAAAAAA"},
               {shortDescription: "aaa short", longDescription: "BBBBBB"}] 

JSFiddle Link - working example

JSFiddle Link - 工作示例

#2


0  

Just as @sal niro is demonstrating, the orderBy filter works on objects (order by a property of an object) your sample data shows that you are iterating over an array of array, which on the first position there's a string and in the second there's an object, forget about the object, you have an array of array, which it does not have properties, just elements, theres no way you can tell the filter to order by a property of one on the elements of an array, as far as i know :).

正如@sal niro所展示的那样,orderBy过滤器处理对象(按对象的属性排序),你的样本数据显示你正在迭代一个数组数组,在第一个位置有一个字符串,在第二个位置有一个字符串一个对象,忘记了对象,你有一个数组数组,它​​没有属性,只有元素,你无法告诉过滤器按一个数组元素的属性来命令,只要我知道 :)。

The best possible solution is to convert that array of arrays to an array of objects, in which each object have a title, shortDescription and a longDescription.

最好的解决方案是将该数组数组转换为对象数组,其中每个对象都有一个标题,shortDescription和一个longDescription。

{
    title: 'test1',
    shortDescription: 'a',
    longDescription: 'aaaaaaaa'
}

#1


0  

It looks like you want the following

看起来你想要以下内容

| orderBy: 'shortDescription'

Where you are sorting by property shortDescription. See the orderBy docs for more information. Here is a simplistic example demonstrating this...

您按属性shortDescription排序的位置。有关更多信息,请参阅orderBy文档。这是一个简单的例子来证明这一点......

<select ng-model="script"
        ng-options="s as s.shortDescription for s in objs | orderBy: 'shortDescription'">
</select>

$scope.objs = [{shortDescription: "first test", longDescription: "AAAAAAAA"},
               {shortDescription: "aaa short", longDescription: "BBBBBB"}] 

JSFiddle Link - working example

JSFiddle Link - 工作示例

#2


0  

Just as @sal niro is demonstrating, the orderBy filter works on objects (order by a property of an object) your sample data shows that you are iterating over an array of array, which on the first position there's a string and in the second there's an object, forget about the object, you have an array of array, which it does not have properties, just elements, theres no way you can tell the filter to order by a property of one on the elements of an array, as far as i know :).

正如@sal niro所展示的那样,orderBy过滤器处理对象(按对象的属性排序),你的样本数据显示你正在迭代一个数组数组,在第一个位置有一个字符串,在第二个位置有一个字符串一个对象,忘记了对象,你有一个数组数组,它​​没有属性,只有元素,你无法告诉过滤器按一个数组元素的属性来命令,只要我知道 :)。

The best possible solution is to convert that array of arrays to an array of objects, in which each object have a title, shortDescription and a longDescription.

最好的解决方案是将该数组数组转换为对象数组,其中每个对象都有一个标题,shortDescription和一个longDescription。

{
    title: 'test1',
    shortDescription: 'a',
    longDescription: 'aaaaaaaa'
}