4

I have created a Gantt View for a List in Sharepoint. The List has a field named Status based on which the Gantt bars' color need to be changed. By Default the gantt bar is in blue color. It should be something like

  1. Complete- Green
  2. Dropped-Red

Could anyone help me with this?

Thanks.

1

This can be achieved by using JS Link propery. You either specify the JS Link property for a field, view, or list view web part. Not all web parts have a JS Link property. You won’t be able to set it to an XsltListViewWebPart or any other web part that is not inherited from IListWebPart. The best approach, in my opinion, is setting the JS Link property of an SPView – this would work everywhere the view is being used.

Your JSLink file should have all logic regarding what color (or any customization) and on what conditions to assign. The following is an example, which changes cell's background based on the value of the field:

(function () {
    var FieldContext = {};
    FieldContext.Templates = {};
    FieldContext.Templates.Fields = {
        "CRiskRAG": {
            "View": RAGTemplate,
            'DisplayForm': null,
            'NewForm': null,
            'EditForm': null
        }
    };
  SPClientTemplates.TemplateManager.RegisterTemplateOverrides(FieldContext);
})();

function RAGTemplate(ctx) {
    var rag = ctx.CurrentItem[ctx.CurrentFieldSchema.Name];
    switch (rag) {
        case "Green":
            return "<div style='background-color :#40f607'>" + rag + "</div>";
            break;
        case "Amber":
            return "<div style='background-color :#f9af00'>" + rag + "</div>";
            break;
        case "Red":
            return "<div style='background-color :#f93030'>" + rag + "</div>";
    }
}

A while ago I wrote a post regarding customization of Gantt view. You can find it here. You might find some useful info there.

If you cannot do any coding, you can set JSLink property by powershell:

Field:

$web = Get-SPWeb http://site
$field = $web.Fields["Status"]
$field.JSLink = "/_layouts/15/Status.js"
$field.Update($true)

Or view:

$web = Get-SPWeb http://site
$list = $web.Lists["Tasks"]
$view = $list.Views["Gantt View"]
$view.JSLink = "/_layouts/15/Status.js"
$view.Update()
  • Thanks for the answer. But whether i will be able to add JS Link for a Gantt View. Because I don't see any option for that? – Sharanya Oct 14 '15 at 14:12
  • Check the blog post at the end. SPView has a property called JSLink. Otherwise, set JSLink for the "Status" field. By the way, what do you mean by "Option" are you trying to achieve this through the UI? You will at least need to use PowerShell for this. Check my updated answer. – Paul Strupeikis Oct 14 '15 at 14:22
  • Firstly I meant it like the way you have given in the blog Edit Page -> Edit Web Part -> Miscellaneous -> JS Link. I will try using powershell for giving JSLink. Thanks for the answer. – Sharanya Oct 15 '15 at 6:02
  • I tried to use the following suggestion, but the JSLink does not get registered. I linked the Script with the View and with the Field as well but it doesn't get loaded. Any suggestions? Thanx in advance! – Ioannis Nov 18 '16 at 12:50
0

you would have to inject your own custom javascript directly into the page, for instance using a Script Editor Webpart. Unfortunately it is not possible to use JSlink on a Gannt View.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.