Sharepoint显示模板如何使用组显示模板

问题描述 投票:0回答:1

控件显示模板中的代码使用_#= ctx.RenderGroups(ctx) =#_,还有另一个名为group_content.js的JS文件被调用,如何将数据拆分成组以便为每个组呈现一些HTML?

javascript sharepoint display-templates
1个回答
0
投票

经过深入研究,您需要手动操作才能使用它。

  1. 创建组显示模板文件,只需使用“搜索”或“内容搜索”文件夹中的默认“group_xxx.html”,无论您使用什么。
  2. 必须下载一个副本或您搜索Web部件(导出)并将“Group TemplateId”中的值更改为您的组JS文件。
  3. CODING对象模仿ResultTables对象

最终代码

ctx.ClientControl.set_groupTemplateId('~sitecollection/_catalogs/masterpage/display templates/content web parts/Group_Content_QA.js');
ctx.ListDataJSONGroupsKey = "ResultGrouped"
ctx.ListData.ResultGrouped = [];

//function to create the ResultTables fake array
function createResultGroup(items){
 return {
  ResultRows: items, 
  RowCount: items.length,
  TableType: "RelevantResults",
 }
};

//just a ref
var _items = ctx.ListData.ResultTables[0].ResultRows;

//categories dictionary
var _groupDictionary = {};

//populating dictionary categories
_items.forEach(function(e) {
    if ( !_groupDictionary[e.QACategoryOWSCHCS] ){
   _groupDictionary[e.QACategoryOWSCHCS] = [];
    }
});

//populating dictionary categories with items
_items.forEach(function(e) {
 _groupDictionary[e.QACategoryOWSCHCS].push(e);
});

//adding to ctx categories as a named array
ctx.groupsNames = Object.keys(_groupDictionary);

//populating fake ResultTables array (ResultGrouped)
ctx.groupsNames.forEach(function(g) {
 ctx.ListData.ResultGrouped.push(
  createResultGroup(_groupDictionary[g])
 );
});

read more here

© www.soinside.com 2019 - 2024. All rights reserved.