Question

I need to make the action of clicking on the title of a list item open the edit form, and not the view form. To do this I chose to use CSR.

I have the following code:

(function () {
    var statusFieldCtx = {};

    statusFieldCtx.Templates = {};
    statusFieldCtx.Templates.Fields = {
        "Title": {"View": DataFieldViewTemplate},

    };

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(statusFieldCtx);
})();

function DataFieldViewTemplate(ctx) {       
    var data = ctx.CurrentItem.Title;
    console.log(data);      
}

But I see nothing in the browser console. If I change the code, this works, but the title column does not exist.

Here is my edited code:

(function () {
    var statusFieldCtx = {};

    statusFieldCtx.Templates = {};
    statusFieldCtx.Templates.Fields = {
        "dt_x0020_proximo_x0020_contato": {"View": DataFieldViewTemplate},

    };

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(statusFieldCtx);
})();

function DataFieldViewTemplate(ctx) {       
    var data = ctx;
    console.log(data);      
}

My questions are: Is there any error in my code? Can I do what I want in a different way?

Was it helpful?

Solution

It seems you are trying to customize LinkTitle field - Title (linked to item with edit menu), but not a Title field.

The following example demonstrates how to render LinkTitle with a Url set to to Edit Form instead of Display Form:

(function () {


   function titleRenderer(renderCtx) {
       var item = ctx.CurrentItem;
       return '<a class="ms-listlink" onfocus="OnLink(this)" href="' + renderCtx.editFormUrl + '" onclick="EditLink2(this,' + renderCtx.ListTemplateType + ');return false;" target="_self">' + item.Title + '</a>';  
    }


    function registerRenderer()
    {
      var ctxTitleField = {};
      ctxTitleField.Templates = {};
      ctxTitleField.Templates.Fields = {
        "LinkTitle": {
            "View": titleRenderer
        }
      };

      SPClientTemplates.TemplateManager.RegisterTemplateOverrides(ctxTitleField);
    } 
    ExecuteOrDelayUntilScriptLoaded(registerRenderer, 'clienttemplates.js');

})();

OTHER TIPS

Is the title field in the view you try to apply this to? There actually exists a number of variants of the title field (for example with the (...) menu) so make sure the one with the internal name of Title is added to your view.

Or use the information you get from ctx.CurrentItem in your second code snippet to check if there isn't another property you can use

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top