Home › Forums › Requests › Modify HitCounter script to Add to Favourites button › Reply To: Modify HitCounter script to Add to Favourites button
Hi Alexander,
I’ve finally got this code working from a button in the List View.
Taken me about 3 weeks to reverse engineer each line to figure out how it worked, well worth it.
Just wondering if you have time to make some last adjustments?
1: The buttons reload each time an item is added or removed. Are you able to make it change the single button only?
2: For some reason, my Test User needs at least Contribute permission to the Courses list for the buttons to render, if user has Read permissions, error in console: Uncaught TypeError: jQspjs(…).filterNode is not a function.
Code for Calc Column: *Note the ID is copied to CoursesID via workflow.
="<img src='/_layouts/images/BLANK.GIF' onload=""getIsFavorite('"&CourseID&"')"" ><span id='favBtnPlaceholder"&CourseID&"'></span>"
I’ve changed the “Favorites” list to “Shortlist”.
Also to make the button ID unique, I added the courseID number to the #favBtnPlaceholder.
Code for CEWP:
<a href="/sites/server/training/SPJS/DFFS/plugins/jquery.js">/sites/server/training/SPJS/DFFS/plugins/jquery.js</a>
<a href="/sites/server/training/SPJS/DFFS/plugins/SPJS-utility.js">/sites/server/training/SPJS/DFFS/plugins/SPJS-utility.js</a>
<script type="text/javascript">
// Current issues:
// all buttons load each time an item is added to shortlist
// user must have contribute access to Courses list to generate buttons.
function getIsFavorite(courseID){
var currDocRes, favRes, b = [];
//grabs column variables from Courses list
currDocRes = spjs.utility.getItemByID({
"listName":"Courses",
"id":courseID,
"viewFields":["ID","Title","Group","Series","TextURL"]
});
if(currDocRes !== null){ //checks if exisiting item in Shortlist
favRes = spjs.utility.queryItems({
"listName":"Shortlist",
"query":"<Where><And><Eq><FieldRef Name='Author' LookupId='TRUE' /><Value Type='User'>"+_spPageContextInfo.userId+"</Value></Eq><Eq><FieldRef Name='Title' /><Value Type='Text'>"+currDocRes.Title+"</Value></Eq></And></Where>",
"viewFields":["ID"]
});
if(favRes.count === 0){
b.push("<input type='button' style='background-color:green;color:white;cursor: pointer' value='Add to Shortlist' onclick='toggleDocFav(false,\""+currDocRes.ID+"\",\""+currDocRes.Title+"\",\""+currDocRes.Group+"\",\""+currDocRes.Series+"\",\""+currDocRes.TextURL+"\")'>");
}else{
b.push("<input type='button' style='background-color:yellow;cursor: pointer' value='Remove from Shortlist' onclick='toggleDocFav(\""+favRes.items[0].ID+"\",\""+currDocRes.Title+"\")'>");
}
}
jQuery("#favBtnPlaceholder"+courseID).html(b.join(""));
}
function toggleDocFav(shortlistid,courseID,parentTitle,parentGroup,parentSeries,parentURL,parentDescription){
var res;
if(!shortlistid){
// add to Shortlist
res = spjs.utility.addItem({
"listName":"Shortlist",
"data":{
"CourseID": courseID,
"Title": parentTitle,
"Group": parentGroup,
"Series": parentSeries,
"Link": parentURL
}
});
if(res.success){
// alert("Successfully added as favourite");
// Refresh Shortlist web part
__doPostBack("ctl00$ctl39$g_388e590e_26cf_4a7d_8a6b_07c09678368a$ctl03","cancel");return false;
getIsFavorite();
}
}else{
res = spjs.utility.deleteItem({
"listName":"Shortlist",
"id":faveid
});
if(res.success){
// alert("Successfully removed from favourite");
// Refresh Shortlist web part
__doPostBack("ctl00$ctl39$g_388e590e_26cf_4a7d_8a6b_07c09678368a$ctl03","cancel");return false;
getIsFavorite();
}
}
}
</script>
Let me know if any further details needed.
Thanks.
