SharePoint: Add tooltip on list view using JSLink

List view web parts are very useful to show the summarize information for the lists and user can easily walk through all list items in quick glance. Normally we keep columns with smaller values in list view like Title, date, status, type and priority etc. to have user friendly view of the list. But sometime we get the requirement to show lists with images or multiline text fields to get more detail on list view page instead of going to list item detail. Sometime this result in unreadable output with long vertical scroll on the page.

To minimize this effort, I am sharing a JSLink script and its integration detail to trim the multiline text field and show the text in tool tip instead on the page.

Below is a simple script which you can save in SharePoint’s siteassets or any other document library:

(function () {
    var priorityFiledContext = {};
    priorityFiledContext.Templates = {};
    priorityFiledContext.Templates.Fields = {
        "Project_x0020_Notes": {
            "View": bodyFiledTemplate
        }
    };
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(priorityFiledContext);
 
})();

function bodyFiledTemplate(ctx) {
 
    var bodyValue = ctx.CurrentItem[ctx.CurrentFieldSchema.Name];
 
    //This regex expression use to delete html tags from the Body field 
    var regex = /(<([^>]+)>)/ig;
 
    bodyValue = bodyValue.replace(regex, "");
 
    var newBodyValue = bodyValue;
 
    if (bodyValue && bodyValue.length >= 100) {
        newBodyValue = bodyValue.substring(0, 100) + " ...";
    }
     bodyValue  = bodyValue.replace(/'/g, " ");
      return "<span title='" + bodyValue + "'>" + newBodyValue + "</span>";
}

Below are steps to integrate JSLink file on a list view webpart:

  1. Edit the list view page, this will show the list web part in edit able mode.
  2. Open the web part properties for list view web part.
  3. Scroll down and expand the Miscellaneous node and enter the url of JSLink file as shown in below screenshot

jslink edit web part

  1. Press Ok to save the changes, and then save changes on the page and you can see output similar to below screenshot which showing trimmed multiline text field and it’s full detail in tool tip.

JSlink tooltip

I just rich text field to show output as plan text, you can also play with the css for a better look n feel for the tooltip.

Adnan Amin

Adnan is two time SharePoint MVP (Most Valuable Professional) with over 12 years of extensive experience with major expertise on SharePoint Server, Office 365, .Net Platform and Microsoft BI. He is currently working SharePoint Architect at FMT Consultants. He is MCT Regional Lead for Pakistan Chapter since 2012. He is working on SharePoint for past nine years and worked on different intranet/intranet solutions for private & govt. sector majorly in Gulf region, which include OOB and customized solutions. He is a trainer, technology evangelist and also speaks in community forums.

Leave a Reply