开发者

Text to HTML in SharePoint using Javascript

开发者 https://www.devze.com 2023-01-07 11:40 出处:网络
I have a summary single-line text column in SharePoint 2007 that is a truncation of a multi-line text column.Going through the complicated process to get there, it turns into text which then needs to

I have a summary single-line text column in SharePoint 2007 that is a truncation of a multi-line text column. Going through the complicated process to get there, it turns into text which then needs to be converted back to HTML, so that the tags like <div> don't show. The following code works if the multi-line colum开发者_StackOverflown is rich text, but not if it's enhanced rich text. Does anyone have the code handy to make this work? (Note: I am working on it but haven't really done any javascript up until now, so it's slow going).

<script type="text/javascript">
  var theTDs = document.getElementsByTagName("TD");
  var i=0;
  var TDContent = " ";
  while (i < theTDs.length)
  {
    try
    {
      TDContent = theTDs[i].innerText || theTDs[i].textContent;
      if (TDContent.indexOf("<div") == 0)
        {
          theTDs[i].innerHTML = TDContent;
        }
    }
  catch(err){}
  i=i+1;
  }
</script>

The result I'm getting now is nothing visible, because with enhanced rich text the div tag is longer than my 45 character truncation limit.


How about using Christophe's techniques to output HTML using a calculated column.

Specifically he has written javascript that will turn the encoded HTML (which you've now got) into HTML.

Add the following into a Content Editor Web Part (CEWP) on the same page.

<script type="text/javascript">
/*
Text to HTML Lite - version 2.1.1
Questions and comments: Christophe@PathToSharePoint.com
*/

function TextToHTML(NodeSet, HTMLregexp) {
   var CellContent = "";
   var i=0;
   while (i < NodeSet.length)
   {
      try 
      {
         CellContent = NodeSet[i].innerText || NodeSet[i].textContent;
         if (HTMLregexp.test(CellContent)) 
            { NodeSet[i].innerHTML = CellContent; }
      } 
      catch(err)
      {}

      i=i+1;
   }
}

// Calendar views
var regexpA = new RegExp("\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*");
TextToHTML(document.getElementsByTagName("a"),regexpA);

// List views
var regexpTD = new RegExp("^\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*$");
TextToHTML(document.getElementsByTagName("TD"),regexpTD);

</script>


I have modified the TextToHTML code from below link, source is PathToSharePoint.com and I have added an event listener which works on SharePoint 2016 successfully in IE compatiblity mode which runs as IE10 and Chrome latest version: Text to Html conversion in Sharepoint 2010

<script type="text/javascript">
  /*
  Text to HTML Lite - version 2.1.1
  Questions and comments: Christophe@PathToSharePoint.com
  */
  document.addEventListener("DOMContentLoaded", function() {
    function TextToHTML(NodeSet, HTMLregexp) {
      var CellContent = "";
      var i = 0;
      while (i < NodeSet.length) {
        try {
          CellContent = NodeSet[i].innerText || NodeSet[i].textContent;
          if (HTMLregexp.test(CellContent)) {NodeSet[i].innerHTML = CellContent;}
        }
        catch (err) {}
        i = i + 1;
      }
    }

    // Calendar views
    var regexpA = new RegExp("\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*");
    TextToHTML(document.getElementsByTagName("a"), regexpA);

    // List views
    var regexpTD = new RegExp("^\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*$");
    TextToHTML(document.getElementsByTagName("TD"), regexpTD);

    // This function is call continuesly every 100ms until the length of the main field changes
    // after which the convert text to HTML is executed.
    var postElemLength = 0;
    function PostConvertToHtml() {
      if (postElemLength == document.getElementsByTagName("TD").length) {
        setTimeout(PostConvertToHtml, 100);
      }
      else {
        var regexpTD = new RegExp("^\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*$");
        TextToHTML(document.getElementsByTagName("TD"), regexpTD);
      }
    }

    // Grouped list views
    ExpGroupRenderData = (function(old) {
      return function(htmlToRender, groupName, isLoaded) {

        var result = old(htmlToRender, groupName, isLoaded);
        var regexpTD = new RegExp("^\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*$");
        TextToHTML(document.getElementsByTagName("TD"), regexpTD);
      };
    })(ExpGroupRenderData);

    // Preview pane views
    if (typeof (showpreview1) == "function") {
      showpreview1 = (function(old) {
        return function(o) {
          var result = old(o);
          var regexpTD = new RegExp("^\\s*<([a-zA-Z]*)(.|\\s)*/\\1?>\\s*$");
          TextToHTML(document.getElementsByTagName("TD"), regexpTD);
        };
      })(showpreview1);
    }
  });
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消