Last active
April 10, 2016 15:33
-
-
Save marineko/7a1061a633f5e21560ee to your computer and use it in GitHub Desktop.
Infinite scroll SharePoint items
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function(){ | |
var setArea = $('#loadarea'), | |
loadNum = 1, // 読み込む個数 | |
loadTxt = 'Now Loading...', // Loading中の表示テキスト | |
fadeSpeed = 500; // フェードスピード | |
var onclickStr; | |
var txt; | |
$.ajax({ | |
url: "/[your site]/_api/web/lists/getbytitle('test')/items?$select=*", | |
method: "GET", | |
headers: { "Accept": "application/json; odata=verbose" }, | |
success : function(data){ | |
for (var i=0; i<5; i++) { | |
onclickStr = "openInDialog('/[your site]/Lists/test/DispForm.aspx?ID=" + data.d.results[i].ID + "',500,500,true,true,false); return:false;"; | |
txt = data.d.results[i].Body; | |
if(txt.length > 200){txt = txt.substr(0, 200) + "...";} | |
$('<div id="' + data.d.results[i].Title + '" class="loadItem"><h3>' | |
+ data.d.results[i].Title + '</h3><br>' | |
+ txt + ' <a href="javascript:void(0)" onclick=' | |
+ onclickStr + '>' + '[read more]'+ '</a></div>').appendTo(setArea); | |
} | |
} | |
}); | |
$("#s4-workspace").scroll(function(){ | |
var winHeight = $("#s4-workspace").height(); | |
var posBottom = $("#s4-bodyContainer").height() - winHeight; | |
if($("#s4-workspace").scrollTop() >= posBottom) { | |
var adjScrTop = $("#s4-workspace").scrollTop(); | |
$("#s4-bodyContainer").animate({scrollTop:(adjScrTop)-1},0); | |
$.ajax({ | |
url: "/[your site]/_api/web/lists/getbytitle('test')/items?$select=*", | |
method: "GET", | |
headers: { "Accept": "application/json; odata=verbose" }, | |
success : function(data){ | |
var dataLengh = data.d.results.length, | |
loadItemLength = setArea.find('.loadItem').length, | |
setAdj = (dataLengh)-(loadItemLength), | |
setBeg = (dataLengh)-(setAdj); | |
if(!(dataLengh == loadItemLength)){ | |
setArea.append('<div id="nowLoading">' + loadTxt + '</div>'); | |
if(loadItemLength == 0){ | |
for (var i=0; i<loadNum; i++) { | |
onclickStr = "openInDialog('/[your site]/Lists/test/DispForm.aspx?ID=" + data.d.results[i].ID + ",500,500,true,true,false'); return:false;"; | |
txt = data.d.results[i].Body; | |
if(txt.length > 200){txt = txt.substr(0, 200) + "...";} | |
$('<div id="' + data.d.results[i].Title + '" class="loadItem"><h3>' | |
+ data.d.results[i].Title + '</h3><br>' | |
+ txt + ' <a href="javascript:void(0)" onclick=' | |
+ onclickStr + '>' + '[read more]'+ '</a></div>').appendTo(setArea) | |
.css({opacity:'0'}).animate({opacity:'1'},fadeSpeed); | |
} | |
} else if(loadItemLength > 0 && loadItemLength < dataLengh){ | |
if(loadNum < setAdj){ | |
for (var i=0; i<loadNum; i++) { | |
v = i+setBeg; | |
onclickStr = "openInDialog('/[your site]/Lists/test/DispForm.aspx?ID=" + data.d.results[v].ID + "',500,500,true,true,false); return:false;"; | |
txt = data.d.results[v].Body; | |
if(txt.length > 200){txt = txt.substr(0, 200) + "...";} | |
$('<div id="' + data.d.results[v].Title + '" class="loadItem"><h3>' | |
+ data.d.results[v].Title + '</h3><br>' | |
+ txt + ' <a href="javascript:void(0)" onclick=' | |
+ onclickStr + '>' + '[read more]'+ '</a></div>').appendTo(setArea) | |
.css({opacity:'0'}).animate({opacity:'1'},fadeSpeed); | |
} | |
} else if(loadNum >= setAdj){ | |
for (var i=0; i<setAdj; i++) { | |
v = i+setBeg; | |
onclickStr = "openInDialog('/[your site]/Lists/test/DispForm.aspx?ID=" + data.d.results[v].ID + "'500,500,true,true,false); return:false;"; | |
txt = data.d.results[v].Body; | |
if(txt.length > 200){txt = txt.substr(0, 200) + "...";} | |
$('<div id="' + data.d.results[v].Title + '" class="loadItem"><h3>' | |
+ data.d.results[v].Title + '</h3><br>' | |
+ txt + ' <a href="javascript:void(0)" onclick=' | |
+ onclickStr + '>' + '[read more]'+ '</a></div>').appendTo(setArea) | |
.css({opacity:'0'}).animate({opacity:'1'},fadeSpeed); | |
} | |
} | |
} else if(loadItemLength == dataLengh){ | |
return false; | |
} | |
} else { | |
return false; | |
} | |
}, | |
complete : function(){ | |
$('#nowLoading').each(function(){ | |
$(this).remove(); | |
}); | |
return false; | |
} | |
}); | |
return false; | |
} | |
}); | |
//add button go back to page top | |
var topBtn = $('#page-top'); | |
topBtn.hide(); | |
$('#s4-workspace').scroll(function () { | |
if ($(this).scrollTop() > 400) { | |
topBtn.fadeIn(); | |
} else { | |
topBtn.fadeOut(); | |
} | |
}); | |
topBtn.click(function () { | |
$('#s4-workspace').animate({ | |
scrollTop: 0 | |
}, 500, "swing"); | |
return false; | |
}); | |
}); | |
//Open item in dialog | |
function openInDialog(pageUrl, dlgWidth, dlgHeight, dlgAllowMaximize,dlgShowClose,needCallbackFunction){ | |
var options = { | |
url: pageUrl, | |
width: dlgWidth, | |
height: dlgHeight, | |
allowMaximize: dlgAllowMaximize, | |
showClose: dlgShowClose | |
}; | |
if(needCallbackFunction){ | |
options.dialogReturnValueCallback = Function.createDelegate(null, CloseDialogCallback); | |
} | |
SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options); | |
} | |
function CloseDialogCallback(dialogResult, returnValue){ | |
if(dialogResult == SP.UI.DialogResult.OK){ | |
//do something | |
; | |
}else if(dialogResult == SP.UI.DialogResult.cancel){ | |
; | |
}else{ | |
; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment