Skip to main content
Bounty Ended with 50 reputation awarded by CommunityBot
added 775 characters in body
Source Link
Damjan Tomic
  • 3.7k
  • 3
  • 19
  • 18

UPDATE 2:

I put both scripts to the same js file and attached them to the list webpart through Miscellaneous -> Jslink (didn't use powershell for this).

The first approach will work with old items which don't have the value for TrackingStatus field. The second approach changes the value of the TrackingStatus field after you edit any other field in the same row and it works with old and new rows (you could also use some other event. Here's the list of all events supported by JSGrid).

Also, please double check your column name and change the script accordingly, since you wrote "Tracking Status" (with space) in comments, and in the question it was TrackingStatus.

UPDATE 2:

I put both scripts to the same js file and attached them to the list webpart through Miscellaneous -> Jslink (didn't use powershell for this).

The first approach will work with old items which don't have the value for TrackingStatus field. The second approach changes the value of the TrackingStatus field after you edit any other field in the same row and it works with old and new rows (you could also use some other event. Here's the list of all events supported by JSGrid).

Also, please double check your column name and change the script accordingly, since you wrote "Tracking Status" (with space) in comments, and in the question it was TrackingStatus.

added 1365 characters in body
Source Link
Damjan Tomic
  • 3.7k
  • 3
  • 19
  • 18

UPDATE: There is another approach, that involves attaching the event to the jsGrid control, which checks for the field value and updates it if it's empty.

SP.SOD.executeFunc('clienttemplates.js', 'SPClientTemplates.TemplateManager.RegisterTemplateOverrides', function() {
        var alreadyAttachedEvent = false;
        SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
            OnPostRender: function(ctx) {               
                if (ctx.enteringGridMode || !ctx.inGridMode || alreadyAttachedEvent)
                  return;
                var jsGrid = null;
                for(var v in g_SPGridInitInfo)
                {
                    var jsGridContainer = $get("spgridcontainer_" + g_SPGridInitInfo[v].jsInitObj.qualifier);
                    var jsGrid = jsGridContainer.jsgrid;
                    break;
                }

                jsGrid.AttachEvent(SP.JsGrid.EventType.OnCellEditCompleted, function(args) {
                    var rec = jsGrid.GetRecord(args.recordKey);
                    if (!rec.properties["TrackingStatus"].localizedValue)
                    {
                        var update = SP.JsGrid.CreateUnvalidatedPropertyUpdate(args.recordKey,'TrackingStatus',"In Progress",false);
                        jsGrid.UpdateProperties([update], SP.JsGrid.UserAction.UserEdit);
                    }
                });
                alreadyAttachedEvent = true;
            }
        });
 });

UPDATE: There is another approach, that involves attaching the event to the jsGrid control, which checks for the field value and updates it if it's empty.

SP.SOD.executeFunc('clienttemplates.js', 'SPClientTemplates.TemplateManager.RegisterTemplateOverrides', function() {
        var alreadyAttachedEvent = false;
        SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
            OnPostRender: function(ctx) {               
                if (ctx.enteringGridMode || !ctx.inGridMode || alreadyAttachedEvent)
                  return;
                var jsGrid = null;
                for(var v in g_SPGridInitInfo)
                {
                    var jsGridContainer = $get("spgridcontainer_" + g_SPGridInitInfo[v].jsInitObj.qualifier);
                    var jsGrid = jsGridContainer.jsgrid;
                    break;
                }

                jsGrid.AttachEvent(SP.JsGrid.EventType.OnCellEditCompleted, function(args) {
                    var rec = jsGrid.GetRecord(args.recordKey);
                    if (!rec.properties["TrackingStatus"].localizedValue)
                    {
                        var update = SP.JsGrid.CreateUnvalidatedPropertyUpdate(args.recordKey,'TrackingStatus',"In Progress",false);
                        jsGrid.UpdateProperties([update], SP.JsGrid.UserAction.UserEdit);
                    }
                });
                alreadyAttachedEvent = true;
            }
        });
 });
Source Link
Damjan Tomic
  • 3.7k
  • 3
  • 19
  • 18

This script should do it:

(function () {
    var overrideCtx = {};
    overrideCtx.Templates = {}; 
    overrideCtx.Templates.OnPreRender = function(ctx) {
        if (!ctx.inGridMode)
        {
            return;
        }
        var statusField = ctx.ListSchema.Field.filter(function(f) {
            return f.Name === 'TrackingStatus';
        });
        if (statusField.length > 0) {
            for (var i = 0; i < ctx.ListData.Row.length; i++)
            {
                if (ctx.ListData.Row[i].TrackingStatus == "")
                {               
                    ctx.ListData.Row[i].TrackingStatus = "In Progress";
                }               
            }            
        }
    };
    
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
})();

The important thing to note is that the values are not saved to the list until you make some change to the row.