I am working on an enterprise wiki site collection inside sharepoint server 2013. and inside the Home page i added the following code snippet, to dynamically build a summary table using javascript/api calls:-
<style unselectable="on">
.table tbody tr:hover {
background-color:#eeeeea;
cursor:pointer;
}
.table th, .table td {
padding:0.5em;
}
.table tbody tr td, .table tbody tr th {
background-color:transparent;
}
</style>
<div style="padding: 5px 10px; border: 1px solid grey; width: 100%; margin-left: 20%;" unselectable="on">
<div style="text-align: left; font-size: 18px;" unselectable="on"><b unselectable="on">Plan Summary</b></div>
<div id="inserthere" style="margin-left: 50%;" unselectable="on"></div>
</div>
then inside my enterprise wiki page layout i added reference to the javaScript file which is going to build the table (ofcourse i do not want to render the script on edit mode that why i wrap it with <PublishingWebControls:EditModePanel PageDisplayMode="Display" runat="server">
):-
<PublishingWebControls:EditModePanel PageDisplayMode="Display" runat="server">
<script src="/kb/Business/Resources/customSummary.js" type="text/javascript"></script>
</PublishingWebControls:EditModePanel>
</asp:Content>
finally here is the customSummary.js
script:-
$(document).ready(function() {
function appendinit(){
alert("first");
var htmlinit = "";
htmlinit = "<image id= 'customloader' src= '/resources/ajax-loader.gif'></image>";
$("#inserthere").after(htmlinit+"");
return "done";
}
function getCurrentMainCategoryID(){
alert("third");
var results = "";
var html="";
html="<table class='table' style='border-collapse:collapse'><thead><tr><th style='text-align:left'></th><th style='text-align:right'>Pages</th></tr></thead><tbody>";
var url = "/kb/Business/_api/web/lists/getbytitle('MainCategory')/items?$select=Title&$orderby=MainCategoryOrder asc";
$.ajax({
url: url,
method: "GET",
async: false,
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
if(data.d.results.length>0){
results = data.d.results;
for(var j2=0;j2<results.length;j2++)
{
var currentMainCatTitle = results[j2].Title;
$.ajax({
url: "/kb/Business/_api/web/lists/getbytitle('pages')/items??$select=Title,MainCategory&$filter=MainCategory eq " + "'" + currentMainCatTitle +"'" ,
method: "GET",
async: false,
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
if(data.d.results.length>0){
html=html+"<tr><td>"
+ "</td>"
+ "<td style='text-align:right'>"+data.d.results.length+"</td></tr>";
}
},
error: function (data) {
}
});
}
}
},
error: function (data) {
}
});
$("#customloader").hide();
$("#inserthere").after(html+"</tbody></table>");
return results;
}
var test = appendinit();
alert("second");
getCurrentMainCategoryID();
$("#customloader").hide();
});
now my initial plan was:-
- Click on the Home page >> then the the loading image will be shown.
- need to wait for a couple of seconds.
- do the api calls.
- hide the loading image.
- build the dynamic table.
but now i am getting this behavioure (i added the alerts to try to understand the execution sequence):-
- I will get alert (First)
- I will get alert (Second)
- I will get alert (Third)
- wait for a couple of seconds...
- then after that the loading image will appear for milliseconds then get hide at the same time the table being rendered..
so not sure why i am getting this behavioure ? i mean the loading image should first get rendered,, after that i have to wait for the table to get dynamically drawn ...