简体   繁体   English

SharePoint 在线 - SPFx - 如何在 PnPjs 中使用 GroupBy

[英]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.

相关问题 如何在 SharePoint 在线页面布局中引用 Jquery? - How to refer Jquery in SharePoint Online Page layouts? SPFx 不能使用 JQuery? - SPFx can't use JQuery? 如何在SharePoint Online中为内容搜索Web部件创建排序/筛选? - How to create Sorting/filtering for Content Search Web Part in SharePoint Online? 如何使用Jquery将Sharepoint联机列表与HTML表绑定 - how to bind sharepoint online list with HTML table using Jquery 如何在Angular 4应用程序中将文件上传到SharePoint Online库? - How to upload a file to SharePoint Online library in Angular 4 application? 如何在SharePoint托管的应用程序(SharePoint Online)中使用$ .ajax调用外部Restful - How to call an external restful using $.ajax from within a SharePoint-hosted app (SharePoint Online) Sharepoint 365(在线)重新加载WebPart - Sharepoint 365 (online) reload webpart 如何在动态crm 2011在线使用jquery? - how to use jquery in dynamics crm 2011 online? sharepoint 在线 - 如何检索具有多个用户的人员列...需要有关所有可用选项的信息 - sharepoint online - how to retrieve a person column having multiple users…need information on all the options available 如何使用ODATA和传输URL使用Kendo UI网格对SharePoint Online列出项目 - How to SharePoint Online List Items with Kendo UI Grid using ODATA and Transport URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM