【发布时间】:2017-12-20 18:57:27
【问题描述】:
控件显示模板中的代码使用_#= ctx.RenderGroups(ctx) =#_,并且正在调用另一个名为group_content.js 的JS 文件,如何将我的数据分成组以便为每个组呈现一些HTML?
【问题讨论】:
标签: javascript sharepoint display-templates
控件显示模板中的代码使用_#= ctx.RenderGroups(ctx) =#_,并且正在调用另一个名为group_content.js 的JS 文件,如何将我的数据分成组以便为每个组呈现一些HTML?
【问题讨论】:
标签: javascript sharepoint display-templates
经过深入研究,你需要手动做一些才能使用它。
创建组显示模板文件,只需从“搜索”或“内容搜索”文件夹中获取默认的“group_xxx.html”,无论您使用什么。
必须下载副本或搜索 WebPart(导出)并将“GroupTemplateId”中的值更改为您的组 JS 文件。
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])
);
});
【讨论】:
在完成您的答案时,我认为使用 ctx.ListData[ctx.ListDataJSONGroupsKey] 数组的美妙之处在于为每个组使用不同的组和项目显示模板。使用开发者工具观察这个数组会显示以下形式的对象数组:
GroupTemplateId: null
ItemTemplateId: null
Properties: {GenerationId: 9223372036854776000, indexSystem: "", ExecutionTimeMs: 63, QueryModification: "path:"https://modernsharepointexperiences.sharepoi…tItem") -ContentClass=urn:content-class:SPSPeople", RenderTemplateId: "~sitecollection/_catalogs/masterpage/Display Templates/Search/Group_Default.js", …}
QueryId: "ca540784-162f-4a9e-9ad4-da45e615ca06"
QueryRuleId: "00000000-0000-0000-0000-000000000000"
ResultRows: (10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
ResultTitle: null
ResultTitleUrl: null
RowCount: 10
TableType: "RelevantResults"
TotalRows: 10
TotalRowsIncludingDuplicates: 31
_ObjectType_: "Microsoft.SharePoint.Client.Search.Query.ResultTable"
在这里,我认为,我们可以为上述数组的每个元素(实际上是每个组)设置组显示模板和项目显示模板。如果您认为我错了,请纠正我!谢谢
【讨论】: