在SharePoint中更改特定文本的文字颜色。

huangapple go评论81阅读模式
英文:

Change color text of specific text in sharepoint

问题

我正在进行一些测试(问题跟踪),并启动了一个工作流程,一个包含3个状态的工作流程。工作流程显示为已完成或正在进行中。我可以更改文字的颜色吗?例如,正在进行中可以设为橙色,已完成可以设为绿色。

请建议。

供参考:https://i.stack.imgur.com/PX1xx.jpg

英文:

I was doing some testing(issue tracking) and I initiated a workflow, a 3 state workflow. The workflow is shown as completed or in progress. Can I change the color of the words? For example, in progress would be orange and completed would be in green.

Please Advise.

For Reference: https://i.stack.imgur.com/PX1xx.jpg

答案1

得分: 1

SharePoint 2013中,我们可以将以下代码添加到脚本编辑器Web部件中以实现此功能。

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() { 
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
        OnPostRender: function (ctx) {
            $("td.ms-vb2>a>span").each(function(){
                if($(this).text()=="In Progress"){
                    $(this).css("color","orange");
                }
                if($(this).text()=="Completed"){
                    $(this).css("color","green");
                }
            });
            $("td.ms-vb2").each(function(){
                if($(this).text()=="Closed"){
                    $(this).css("color","red");
                }
                if($(this).text()=="Active"){
                    $(this).css("color","green");
                }
            });
        }
    });
});
</script>

在SharePoint中更改特定文本的文字颜色。

英文:

In SharePoint 2013, we can add the following code into a script editor web part to achieve it.

&lt;script src=&quot;https://code.jquery.com/jquery-1.12.4.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
SP.SOD.executeFunc(&quot;clienttemplates.js&quot;, &quot;SPClientTemplates&quot;, function() { 
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
        OnPostRender: function (ctx) {
            $(&quot;td.ms-vb2&gt;a&gt;span&quot;).each(function(){
                if($(this).text()==&quot;In Progress&quot;){
                    $(this).css(&quot;color&quot;,&quot;orange&quot;);
                }
                if($(this).text()==&quot;Completed&quot;){
                    $(this).css(&quot;color&quot;,&quot;green&quot;);
                }
            });
			$(&quot;td.ms-vb2&quot;).each(function(){
                if($(this).text()==&quot;Closed&quot;){
                    $(this).css(&quot;color&quot;,&quot;red&quot;);
                }
                if($(this).text()==&quot;Active&quot;){
                    $(this).css(&quot;color&quot;,&quot;green&quot;);
                }
            });
        }
    });
});
&lt;/script&gt;

在SharePoint中更改特定文本的文字颜色。

答案2

得分: 0

在工作表/Excel中,位于主页选项卡下,有一个字体组,您会看到一个带有字母 A 的图标,下面有一个粗体颜色通常为红色或黑色的下划线,表示字体颜色选项,用于更改特定文本或所有文本的颜色。

英文:

If you are working on a spreadsheet/excel under the home tab there is the font group there you will see an icon with the letter A underlined by a bold color usually red or black, that represents the font color option used to change color for specific text or all text.

答案3

得分: 0

以下是翻译好的部分:

如果你想要一个简单的解决方案,你需要改变两个地方。
首先,HTML文件。如果表格没有ID,你需要添加一个ID。
其次,你需要添加以下JavaScript代码。

它的作用是:
当页面加载时,它将获取表格中我们提到的带有ID的每个第二个单元格。然后它获取单元格的值,并将其与颜色对象的属性之一进行匹配。
最后,它将将该颜色应用于文本。

$(document).ready(function(){
  var indexOfColumn = 2;
  var colors = {"A": "red", "B B": "green", "C.C": "yellow"};
  
  $("#someID tbody tr td:nth-child("+ indexOfColumn +")").each(function(key, value){
    $(value).css("color", colors[$(value).text()])
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="someID">
  <tr><td>1</td><td>A</td></tr>
  <tr><td>2</td><td>B B</td></tr>
  <tr><td>3</td><td>C.C</td></tr>
  <tr><td>4</td><td>A</td></tr>
</table>

希望这有帮助!

英文:

If you want an easy solution. You will have two change two things.
First, the HTML file. You will need to add an Id to the table if it does not already have one.
Second, you will have to add the following javascript.

What it does is:
When the page is loaded, it will get every 2nd cell in the table we have mentioned with the Id. Then it gets the value of the cell and matchs is with one of the properties of the colors object.
Finally it will apply that color to text

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function(){
  var indexOfColumn = 2;
  var colors = {&quot;A&quot;: &quot;red&quot;, &quot;B B&quot;: &quot;green&quot;, &quot;C.C&quot;: &quot;yellow&quot;};
  
  $(&quot;#someID tbody tr td:nth-child(&quot;+ indexOfColumn +&quot;)&quot;).each(function(key, value){
    $(value).css(&quot;color&quot;, colors[$(value).text()])
  });
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;table id=&quot;someID&quot;&gt;
  &lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;A&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;B B&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;C.C&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;A&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

<!-- end snippet -->

答案4

得分: 0

老实说,我还没有尝试过使用工作流的默认列来实现这个功能,但你可以在列表中创建一个状态字段,例如“状态”,根据工作流的状态进行更新。也就是说,在工作流正在进行时,将字段值设置为“进行中”,在工作流成功完成之前,将其更新为“已完成”。然后使用以下的JS链接:

(function () {
    var overrideCtx = {};
    overrideCtx.Templates = {};
    overrideCtx.OnPostRender = [];

    overrideCtx.Templates.Fields =
    {
        'Status': { 'View': ChangeColor }
    };

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);

})();

function ChangeColor(ctx){ 
    var val = ctx.CurrentItem["Status"];
	if (val == "In Progress")
	{
		return "<span style='color: orange'>" + val + "</span>";
		
	}
	else {
		return "<span style='color: green'>" + val + "</span>";
	}
   
}

将上述代码放入一个js文件中,并将其放在某个地方,例如样式库中。然后,您可以通过配置列表视图Web部件并提供文件的服务器相对URL来引用此文件。
希望这能帮助你。

英文:

Honestly i haven't tried this with workflow's default column but you can create a status field e.g. 'Status' in your list which is updated based on your workflow status i.e. while workflow is in progress, set the field values as 'In Progress' and just before workflow successfully completes, update this to 'Completed' Then using JS link:

 (function () {
    var overrideCtx = {};
    overrideCtx.Templates = {};
    overrideCtx.OnPostRender = [];

    overrideCtx.Templates.Fields =
    {
        &#39;Status&#39;: { &#39;View&#39;: ChangeColor }
    };

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);

})();

function ChangeColor(ctx){ 
    var val = ctx.CurrentItem[&quot;Status&quot;];
	if (val == &quot;In Progress&quot;)
	{
		return &quot;&lt;span style=&#39;color: orange&#39;&gt;&quot; + val + &quot;&lt;/span&gt;&quot;;
		
	}
	else {
		return &quot;&lt;span style=&#39;color: green&#39;&gt;&quot; + val + &quot;&lt;/span&gt;&quot;;
	}
   
}

Put the above code in a js file, and put it somewhere e.g. in Style library. Then you can refer this file by configuring your list view webpart and providing file's server relative url .
Hope this helps.

huangapple
  • 本文由 发表于 2020年1月6日 15:55:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定