[英]SharePoint Online - SPFx - How to use GroupBy in PnPjs
I am new to SPFx PnPjs.我是 SPFx PnPjs 的新手。 I want to use GroupBy in query but not seeing intellisense in visual code.
我想在查询中使用 GroupBy,但在可视代码中看不到智能感知。 Here is my query:
这是我的查询:
var query = pnp.sp.web.lists.getByTitle("Employee").items.select("ID,Title").filter(filterstring).get();
Output i want something like: Output 我想要类似的东西:
var query = pnp.sp.web.lists.getByTitle("Employee").items.GroupBy("Title").select("ID,Title").filter(filterstring).get();
Any suggestion how can I use "GroupBy" in above query?任何建议我如何在上述查询中使用“GroupBy”?
There are no way to user GroupBy in pnp js, because it not exists.在 pnp js 中无法使用 GroupBy,因为它不存在。
But, you can grouping data after you get it in javascript.但是,您可以在 javascript 中获取数据后进行分组。
For example, you have some interface to store result data, somthing like this:例如,您有一些接口来存储结果数据,如下所示:
interface IData{
Id: number;
Title: string;
}
interface IGroups{
Title: string;
Ids: number[];
}
And after you get request response, you create array of objects在获得请求响应后,您将创建对象数组
let result: IData[] = [];
query.then((items)=>{
items.map((item)=>{
result.push({Id: item.ID, Title: item.Title});
});
});
After this you get not grouped data, which you can group by filtering:在此之后,您将获得未分组的数据,您可以通过过滤对其进行分组:
let groups: IGroups[] = [];
result.map((item)=>{
//get only titles for grouping
return item.title;
}).filter((value, index, self)=>{
//get unique values
return self.indexOf(value)===index
}).map((item)=>{
//Here you get distinct title - you groups
//and creating groups
groups.push({
Title: item,
Ids: result.filter((r)=>{return r.title === item})
});
});
SharePoint Rest API does not support the GroupBy
, you could get all supported OData query operations here . SharePoint Rest API 不支持
GroupBy
,你可以在这里获得所有支持的 OData 查询操作。
PNP js is an encapsulation of Sharepoint Rest API, so it does not support group by so far. PNP js是Sharepoint Rest API的封装,所以目前还不支持group。
As a workaround, you can filter out the desired item in the return value.作为一种解决方法,您可以在返回值中过滤掉所需的项目。 Array.prototype.filter()
Array.prototype.filter()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.