Question

I'm trying to truncate text on an Announcements webpart. I utilized this post, and everything works except the Body variable isn't displayed at all. Here is my code:

<script>(function () {
var itemCtx = {};
itemCtx.Templates = {};
    itemCtx.Templates.Header = "<div class='announcementsContainer'><ul class='newsHeadlines'>"
    itemCtx.Templates.Item = announcementsBeautified;
    itemCtx.Templates.Footer = "</ul></div>";
itemCtx.BaseViewID = 1;
itemCtx.ListTemplateType = 104;
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(itemCtx);
})();

function announcementsBeautified(ctx){
    var headliner = ctx.CurrentItem.Title;
    var msg = $('ctx.CurrentItem.Body').text();
    var author = ctx.CurrentItem.Author[0].title;
    var date = ctx.CurrentItem.Date;
    var msgLimit = 275;
    var html = "<li><div class='announcementsBody'><h3>" + headliner + "</h3> <div class='announcementsDate'><font class='author'>" + date + "</font></div> <br/>posted by <font class='author'>" + author +  "</font><br/>" + msg + "<br/>Read More" + "</div></li>";



if (msg.length > msgLimit){
 msg = msg.substring(0,msgLimit);
}
        return html
}</script>
Was it helpful?

Solution

Try below code:

<script>(function () {
var itemCtx = {};
itemCtx.Templates = {};
    itemCtx.Templates.Header = "<div class='announcementsContainer'><ul class='newsHeadlines'>"
    itemCtx.Templates.Item = announcementsBeautified;
    itemCtx.Templates.Footer = "</ul></div>";
itemCtx.BaseViewID = 1;
itemCtx.ListTemplateType = 104;
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(itemCtx);
})();

function announcementsBeautified(ctx){
    var headliner = ctx.CurrentItem.Title;
    var body = ctx.CurrentItem.Body;

    var msg = body.replace(/<(?:.|\n)*?>/gm, '');
    var author = ctx.CurrentItem.Author[0].title;
    var date = ctx.CurrentItem.Date;
    var msgLimit = 275;
    var html = "<li><div class='announcementsBody'><h3>" + headliner + "</h3> <div class='announcementsDate'><font class='author'>" + date + "</font></div> <br/>posted by <font class='author'>" + author +  "</font><br/>" + msg + "<br/>Read More" + "</div></li>";



if (msg.length > msgLimit){
 msg = msg.substring(0,msgLimit);
}
        return html
}</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top