我如何在工具提示中显示HTML源代码

时间:2019-03-13 15:49:37

标签: javascript jslink

我有以下JSLINK脚本:-

(function () {

    // Create object that have the context information about the field that we want to change it's output render 
    var bodyFiledContext = {};
    bodyFiledContext.Templates = {};
    bodyFiledContext.Templates.Fields = {
        // Apply the new rendering for Body field on list view
        "Body": { "View": bodyFiledTemplate }
    };

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(bodyFiledContext);

})();

// This function provides the rendering logic
function bodyFiledTemplate(ctx) {

    var bodyValue = ctx.CurrentItem[ctx.CurrentFieldSchema.Name];


    bodyValue = bodyValue.replace(regex, "");

    var newBodyValue = bodyValue;

    if (bodyValue && bodyValue.length >= 100)
    {
        newBodyValue = bodyValue.substring(0, 100) + " ...";
    }

    return "<span title='" + bodyValue + "'>" + newBodyValue + "</span>";

}

主要使用前100个字符,然后在工具提示中显示全文。但是我面临的问题是ctx.CurrentItem[ctx.CurrentFieldSchema.Name]是HTML代码,因此在工具提示中我将获得实际的html标签,而我想显示实际的数据而不是源HTML。所以我可以这样做吗?

编辑

这是一个字段的HTML代码示例(我用点替换了真实文本。):-

<div class="ExternalClass365A46B23B5F4AE38B2909AB3EF63011"><ul><li><p>​Ticket was raised March 1st 7&#58;28</p></li><li><p>User indicated ....</p></li><li><p>No action ....s</p></li><li><p>........</p></li><li><p>............</p></li><li><p>.....</p></li></ul><p><br></p></div>

我更新了我的脚本,如下所示:-

bodyValue.replace(/[\u00A0-\u9999<>\&]/gim, function(i) { return '&#'+i.charCodeAt(0)+';'; });

如下:-

(function () { 

    // Create object that have the context information about the field that we want to change it's output render  
    var bodyFiledContext = {}; 
    bodyFiledContext.Templates = {}; 
    bodyFiledContext.Templates.Fields = { 
        // Apply the new rendering for Body field on list view 
        "UserFeedbackAnalysis": { "View": bodyFiledTemplate } 
    }; 

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(bodyFiledContext); 

})(); 

// This function provides the rendering logic 
function bodyFiledTemplate(ctx) { 

    var bodyValue = ctx.CurrentItem[ctx.CurrentFieldSchema.Name]; 



    var newBodyValue = ""; 

    if (bodyValue && bodyValue.length >= 100) 
    { 
        newBodyValue = bodyValue.substring(0, 100) + " ..."; 
    } 
    var fullval = bodyValue.replace(/[\u00A0-\u9999<>\&]/gim, function(i) {
   return '&#'+i.charCodeAt(0)+';';
});

    return "<span title='" + fullval + "'>" + newBodyValue + "</span>"; 

} 

现在截断将发生...但是在工具提示中,我仍然得到真正的html标签,如下所示: enter image description here

0 个答案:

没有答案