0

I'm desperate and need your help on this script. I have a "multi line text" column named "Comments", and i added the following script into a script editor but it is not working for me. could you please help fix this?

<script type="text/javascript">
(function () {

 var linkFieldContext = {};
 linkFieldContext.Templates = {};
 linkFieldContext.Templates.Fields = { 
 "Comments": { "View": CommentRendering}

};
 SPClientTemplates.TemplateManager.RegisterTemplateOverrides(linkFieldContext);
})();

function ExpandDescription(a){

var small=a.parentNode.parentNode.getElementsByClassName('Small');
 small[0].style.display='none'; 
var full=a.parentNode.parentNode.getElementsByClassName('Full');
 full[0].style.display='block';

}

function CollapseDescription(a){

var small=a.parentNode.parentNode.getElementsByClassName('Small');
 small[0].style.display='block'; 
var full=a.parentNode.parentNode.getElementsByClassName('Full');
 full[0].style.display='none';

}

function CommentRendering(ctx)
{
 var htmlStr = "";
 var itemId = ctx.CurrentItem.ID;
 var itemDesc = ctx.CurrentItem.Comments;
var div = document.createElement("div");

div.innerHTML =itemDesc;

itemDesc = div.textContent || div.innerText || "";
if(itemDesc.length>50){
 var itemDescSmall=itemDesc.substring(0, 50);
 htmlStr='+itemDescSmall+…Expand
'}else{return ctx.CurrentItem.Comments;} return htmlStr;}
</script> 
1
  • where did you add tag with "Small" and "Full" class name? Sep 10, 2019 at 9:56

1 Answer 1

0

Adding this script in script editor will not work.

You need to add it in JSLink property of List web part.

Try below steps:

How to set the JSLink property of your Web Part:

  1. Create JavaScript file with the code you are using.
  2. Click on Gear icon at top right corner.
  3. Edit the Page on which you have added list web part.
  4. Select the list web part and edit web part properties.
  5. Go to Miscellaneous --> JS Link property.
  6. Add the reference to newly create file in JSLink property.

Path:

  1. Save this JSLink.js file in same site and add path to miscellaneous--> as ~site/SiteAssets/JSLink.js. OR
  2. Save this JSLink.js file in same site collection and add path to miscellaneous--> as ~sitecollection/SiteAssets/JSLink.js.

For more details, check my answer given at Color Coding List Item by Web Part based on value

3
  • Hi Ganesh, thanks a lot for your help. the list view webpart is already linked to one JS Link file, as such i cannot link this one
    – T Zhang
    Sep 10, 2019 at 6:03
  • Ok, But you can merge both codes in order to make it work. Sep 10, 2019 at 6:11
  • Have you managed to get it working? Or you are facing any issue? Nov 20, 2019 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.