Truncate Multiline Field in ListView

(function () { var bodyFiledContext = {}; bodyFiledContext.Templates = {}; bodyFiledContext.Templates.Fields = { "InternalColumnName": { "View": bodyFiledTemplate } //Can Truncate more than one column just add comma then this again }; bodyFiledContext.OnPostRender = postRenderHandler; SPClientTemplates.TemplateManager.RegisterTemplateOverrides(bodyFiledContext); })(); function postRenderHandler(ctx) { $("div[name='InternalColumnName']").css("width", "200px"); /Sets the width of a column } function bodyFiledTemplate(ctx) { var bodyValue = ctx.CurrentItem[ctx.CurrentFieldSchema.Name]; var regex = /(<([^>]+)>)/ig; bodyValue = bodyValue.replace(regex, ""); var newBodyValue = bodyValue; if (bodyValue && bodyValue.length >= 300) { newBodyValue = bodyValue.substring(0, 300) + " ..."; } return "<span title='" + bodyValue + "'>" + newBodyValue + "</span>"; }
This uses the JSLink inside of the miscellaneous settings in SharePoint. Create a JavaScript file place it in a folder (I use Site Assests). In the JSLink field of the webpart properties window, the user will need to type "~site/SiteAssets/myFile.js" without the quotations but make sure to use "~site" for the beginning of the link, or the styling/truncation will not work as it should. You will also need to clear your cookies as well or use a "private" browser to do testing.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.