I have a document list with 2 columns. the first column is a category (Example: Menu, General, Information) The second is an image. I would like the image to be automatically calculated according to the selected category, but I can't do it. I am a novice and I am learning to use SharePoint 2019. What is the best solution ?
1 Answer
1.You could create a calculated column with following formula:
=IF(menu="a","<DIV><img src='http://xx/sites/xx/SiteAssets/image1.png'></DIV>","<DIV><img src='http://xx/sites/xx/SiteAssets/image2.jpg'></DIV>")
Remember to select Number:
2.Then add a Script Editor Web Part with following codes to write HTML in to calculated column and convert HTML tag to image:
<script type="text/javascript">
//
// Text to HTML
// Feedback and questions: Christophe@PathToSharePoint.com
//
var theTDs = document.getElementsByTagName("TD");
var i=0;
var TDContent = " ";
while (i < theTDs.length) {
try {
TDContent = theTDs[i].innerText || theTDs[i].textContent;
if ((TDContent.indexOf("<DIV") == 0) && (TDContent.indexOf("</DIV>") >= 0)) {
theTDs[i].innerHTML = TDContent;
}
}
catch(err){}
i=i+1;
}
//
// ExpGroupRenderData overwrites the default SharePoint function
// This part is needed for collapsed groupings
//
function ExpGroupRenderData(htmlToRender, groupName, isLoaded) {
var tbody=document.getElementById("tbod"+groupName+"_");
var wrapDiv=document.createElement("DIV");
wrapDiv.innerHTML="<TABLE><TBODY id=\"tbod"+ groupName+"_\" isLoaded=\""+isLoaded+ "\">"+htmlToRender+"</TBODY></TABLE>";
var theTBODYTDs = wrapDiv.getElementsByTagName("TD"); var j=0; var TDContent = " ";
while (j < theTBODYTDs.length) {
try {
TDContent = theTBODYTDs[j].innerText || theTBODYTDs[j].textContent;
if ((TDContent.indexOf("<DIV") == 0) && (TDContent.indexOf("</DIV>") >= 0)) {
theTBODYTDs[j].innerHTML = TDContent;
}
}
catch(err){}
j=j+1;
}
tbody.parentNode.replaceChild(wrapDiv.firstChild.firstChild,tbody);
}
</script>
My test result:
For Reference: Add image to custom list with calculated / conditional column
-
The first part seems to work, but when I try to add a script with the "script editor" web component I got this error in my console log :
Uncaught SyntaxError: Invalid or unexpected token
. I don't see where this error came from, since I have copy/paste your code. Sep 27, 2021 at 13:31