/    Sign up×
Community /Pin to ProfileBookmark

Comments to understand what code is doing

Hello, I realize putting a bunch of code out here is not ideal and frowned upon, I read the announcement this morning ?

However, I needed to put it all because I need comments to all of it.

This is producing an accordion effect on a SharePoint List. I didn’t write the code but I’m using it and now I need to change the style of the accordion but I don’t understand what each piece is or what it is doing. Is there anyway someone could put comments on the end of the lines to say this is doing xyz. If this is to much I understand and apologize for the post. This is the first time I have implemented any JavaScript and I haven’t grasped it yet so a breakdown would be very helpful so I could try and move forward and make changes to the style. Thank you!

[CODE][code=php](function () {

JQUERY_SCRIPT = “https://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.2.min.js”;
JQUERYUI_SCRIPT = “https://ajax.aspnetcdn.com/ajax/jquery.ui/1.11.4/jquery-ui.js”;
JQUERYUI_CSS = “//code.jquery.com/ui/1.11.4/themes/start/jquery-ui.css”;

function OnAccordionViewPostRender(renderCtx) {
$(‘#accordionFAQ’).accordion({
collapsible: true
});
}

function OnAccordionViewPreRender(renderCtx) {
}

function RenderAccordionViewBodyTemplate(renderCtx) {

var listData = renderCtx.ListData;
if (renderCtx.Templates.Body == ”) {
return RenderViewTemplate(renderCtx);
}
var accordionHtml = ”;

accordionHtml = ‘<div id=”accordionFAQ”>’;
for (var idx in listData.Row) {
var listItem = listData.Row[idx];
accordionHtml += ‘<h3>’;
accordionHtml += listItem.Title;
accordionHtml += ‘</h3>’;
accordionHtml += ‘<div>’;
accordionHtml += listItem.Answer;
accordionHtml += ‘</div>’;
}

accordionHtml += ‘</div>’;

return accordionHtml;
}

function registerAccordionViewTemplate() {

var accordionViewContext = {};

//accordionViewContext.BaseViewID = ‘Accordion’;
accordionViewContext.Templates = {};
accordionViewContext.Templates.View = RenderAccordionViewBodyTemplate;
accordionViewContext.OnPreRender = OnAccordionViewPreRender;
accordionViewContext.OnPostRender = OnAccordionViewPostRender;
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(accordionViewContext);
}

loadjQueryScript = function () {
loadScript(JQUERY_SCRIPT, loadjQueryUIScript)
}

loadjQueryUIScript = function () {
loadScript(JQUERYUI_SCRIPT, registerAccordionViewTemplate)
}

function loadScript(scriptUrl, callback)
{
var head = document.getElementsByTagName(‘head’)[0];
var script = document.createElement(‘script’);
script.src = scriptUrl;
var done = false;
script.onload = script.onreadystatechange = function () {
if (!done && (!this.readyState || this.readyState == “loaded” || this.readyState == “complete”)) {
done = true;
callback();
}
};
head.appendChild(script);
}

function loadCss(url) {
var link = document.createElement(‘link’);
link.href = url;
link.rel = ‘stylesheet’;
document.getElementsByTagName(‘head’)[0].appendChild(link);
}

//****
//**** main flow
//****
debugger;
loadCss(JQUERYUI_CSS);
ExecuteOrDelayUntilScriptLoaded(loadjQueryScript, ‘clienttemplates.js’)

})();[/code]

[/CODE]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@cbVisionAug 17.2015 — Hey there.

It looks like the JavaScript is outputting your HTML like this for each accordion section:

[CODE]
<div id="accordionFAQ">
<h3>Title</h3>
<div>Answer</div>
</div>
[/CODE]


The markup seems simple enough where you may not have to modify the JavaScript for a style change. You should be able to simply use CSS. For example, to change the color of the title to red, simply add this:

[CODE]
#accordionFAQ h3 {color: #f00;}
[/CODE]


How are you trying to style it?
Copy linkTweet thisAlerts:
@LCorkumauthorAug 17.2015 — Thanks very much, currently it is using jquery for styling.

JQUERY_SCRIPT = "https://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.2.min.js";

JQUERYUI_SCRIPT = "https://ajax.aspnetcdn.com/ajax/jquery.ui/1.11.4/jquery-ui.js";

JQUERYUI_CSS = "//code.jquery.com/ui/1.11.4/themes/start/jquery-ui.css";

I have tried removing the reference to the ui but then it breaks completely and goes back to a standard list view. I didn't know if I need to try and leave that in there and then override with inline code or reference another external CSS file. If inline is the way to go I wasn't really sure how to approach that either. Thanks for your reply!
×

Success!

Help @LCorkum spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...