5

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?

3

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');

})();
  • Diego, i'm glad it helped – Vadim Gremyachev Mar 17 '14 at 11:54
  • May I know the internal name for Title (link to item) which will goto /Lists/List4/DispForm.aspx?ID=9 (not edit form) – Mark L Jun 24 '15 at 2:43
0

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

  • There are 3 types of "Title" OOTB. I know "Title" and "LinkTitle" but I fail to find out what is the 3rd one. Do you happen to know? Thanks. – Mark L Jun 25 '15 at 3:20
  • It is "LinkTitle2" which has a link to the ViewForm, but no "..." to open up the menu. – pierrebo Apr 28 '16 at 14:07

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.