-
Notifications
You must be signed in to change notification settings - Fork 0
/
accordion.js
78 lines (58 loc) · 3.23 KB
/
accordion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*************************
** Project : SharePoint Online
** File : Accordion.js
** Name : Accordion Template
** Description : This javascript is used to create accordion control using jslink approach.
** Authorizer : NA
** Date : 11/02/2017
** Modified On :
**************************
** Change History
**************************
** Version Date Author Description
** -- -------- ------- ------------------------------------
** 01 11/02/2017 Sachchin Annam This javascript is used to create accordion control using jslink approach.
**************************/
(function () {
// jQuery library is required in this sample
// Fallback to loading jQuery from a CDN path if the local is unavailable
(window.jQuery || document.write('<script src="/SiteAssets/jquery1.11.min.js"><\/script>'));
(window.load || document.write('<link rel="stylesheet" href="/SiteAssets/Accordion/accordion.css">'));
// Create object that have the context information about the field that we want to change it's output render
var accordionCSRCtx = {};
accordionCSRCtx.Templates = {};
// Be careful when add the header for the template, because it's will break the default list view render
accordionCSRCtx.Templates.Header = "<ul class='accordion'>";
accordionCSRCtx.Templates.Footer = "</ul>";
//template ID of Custom List - this line will make sure that CSR will render for custom list only
accordionCSRCtx.ListTemplateType = 100;
// Add OnPostRender event handler to add accordion click events and style
accordionCSRCtx.OnPostRender = accordionOnPostRender;
// This line of code tell TemplateManager that we want to change all HTML for item row render
accordionCSRCtx.Templates.Item = accordionTemplate;
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(accordionCSRCtx);
})();
// This function is used for creating logic
function accordionTemplate(ctx) {
var title = ctx.CurrentItem["Title"]; //internal name of the column
var description = ctx.CurrentItem["Answer"]; //internal name of the column
// Return whole item html
return "<li><a>" + title + "</a><div>" + description + "</div></li>";
}
function accordionOnPostRender() {
$(document).ready(function () {
// $('.accordion > li:eq(0) a').addClass('active').next().slideDown();
$('.accordion a').click(function (j) {
var dropDown = $(this).closest('li').find('div');
$(this).closest('.accordion').find('div').not(dropDown).slideUp();
if ($(this).hasClass('active')) {
$(this).removeClass('active');
} else {
$(this).closest('.accordion').find('a.active').removeClass('active');
$(this).addClass('active');
}
dropDown.stop(false, true).slideToggle();
j.preventDefault();
});
});
}