简体   繁体   English

自定义订单由odata提供吗?

[英]Custom orderBy with odata?

In an AngularApp, i am getting my data from a WebAPI via OData queries. 在AngularApp中,我通过OData查询从WebAPI获取数据。

All results have a date and an int statuscode. 所有结果都有一个日期和一个int状态码。 Statuscode can be 1, 2, 3 or 4. 状态码可以是1、2、3或4。

I want to order my results, so all results with statuscode 1, goes to the top of the list. 我想对结果进行排序,因此所有状态码为1的结果都将移至列表顶部。 The rest of the results, with code 2, 3 and 4, gets sorted by the data attached to them, and not the statuscode. 其余结果(代码2、3和4)将根据附加到它们的数据而不是状态码进行排序。

Possible? 可能?

This is my code as it looks now. 这是我现在看起来的代码。 It just sorts by the date, and nothing else. 它只是按日期排序,仅此而已。

return Products.query({ $orderby: 'Date' });

From your description, it sounds like you want all Products with StatusCode of 1 to be treated equally. 从您的描述看来,您希望所有StatusCode为1的产品都受到同等对待。 That is, they are not further sorted by Date . 也就是说,它们不会按Date进一步排序。 If you can relax this requirement a bit and allow all Products to be sorted by StatusCode and Date , there is a straightforward solution: 如果您可以放宽此要求,并允许所有产品按StatusCode Date进行排序,则有一个简单的解决方案:

return Products.query({ $orderby: 'StatusCode,Date' });

The $orderby query option in OData v4 consists of "a comma-separated list of expressions whose primitive result values are used to sort the items". OData v4中的$orderby查询选项由“用逗号分隔的表达式列表组成,其原始结果值用于对项目进行排序”。 Property names are the most common expressions used to produce sort values. 属性名称是用于产生排序值的最常见的表达式。

If you want to reverse the ordering on a particular property, use the desc suffix on that property like so: 如果要反转特定属性的顺序,请在该属性上使用desc后缀,如下所示:

return Products.query({ $orderby: 'StatusCode,Date desc' });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM