4
votes

I have a question about ECB: In Sharepoint 2013, how to create Edit Control Block (ECB) in view with JSLink.

  • I use JSLink to modify the default view, but I can't display the ECB context menu with "View item", "New item" ... I don't want use XSLT or custom action. Per my employer, I must to use JSLink to do it.
  • I update JSLink for View in FeatureReceiver.cs, not using orther webpart. My View is OK, but missing ECB.

Here is a picture of my custom view: enter image description here

Standard view in SP2013: enter image description here

This is my JavaScript code:

(function () {
    var itemCtx = {};
    itemCtx.Templates = {};
    itemCtx.Templates.Item = ItemOverrideFun;
    itemCtx.BaseViewID = 1;
    itemCtx.ListTemplateType = 104;
    itemCtx.OnPostRender = [];
    itemCtx.OnPostRender.push(function () {
        $(document).ready(function () {

        // use this method to extend ECB on common lists...
       function Custom_AddListMenuItems(m, ctx) {
       // Adding a simple command to the ECB
       CAMOpt(m, 'Go to', 'javascript:STSNavigate("http://www.abc.com");return false;');
       // Adding a separator to the ECB
       CAMSep(m);
       }
       // use this method to extend ECB on libraries...
       function Custom_AddDocLibMenuItems(m, ctx) {}

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

function ItemOverrideFun(ctx) {
    var id = ctx.CurrentItem["ID"];
    var _announcementTitle = ctx.CurrentItem["Title"];
    var _announcementDesc = ctx.CurrentItem["Content"];
    var createBy = ctx.CurrentItem["Author"][0].title;
    var modified = ctx.CurrentItem["Modified"];
    var att = ctx.CurrentItem["Attachments"];
    var attImg = "";
    if (att == true) {
        attImg = attImg + "<img src='/../../_layouts/15/images/attach.gif' />&nbsp;";
    }
    return "<tr><td></td><td vAlign='top' style='width:20px;padding-left:8px;'>" + attImg + "</td><td colspan='2' style='padding-left:40px;'><img style='cursor:pointer' onclick='Expand(" + id + ");' id='imgId" + id + "' src='" + L_Menu_BaseUrl + "/Images/minus.gif' /><span style='padding-left:5px'><a style='cursor:pointer;font-size:1em' href='" + L_Menu_BaseUrl + "/Lists/Bulletin%20Board/DispForm.aspx?ID=" + id + "'>" + _announcementTitle + "</a></span><div id='sh" + id + "' style='padding-left:15px;'>" + _announcementDesc + "</div></td><td>" + createBy + "</td><td>" + modified + "</td></tr>";
}

function Expand(id) {
    var dsStatus = document.getElementById('sh' + id).style.display;
    var url = L_Menu_BaseUrl;
    if (dsStatus == 'none') {
        jQuery("#imgId" + id).attr('src', url + '/Images/minus.gif');
        $('#sh' + id).show();
    }
    else {
        jQuery("#imgId" + id).attr('src', url + '/images/plus.gif');
        $('#sh' + id).hide();
    }
}

What is struct HTML of ECB? I return missing ECB, so how can I resolve that issue?

1

1 Answers

2
votes

ok, i have fixed this issue.

ECB not show because struct HTML return in ItemOverrideFun not correct.

This is final Javascript code:

https://sharepoint.stackexchange.com/questions/97189/create-edit-control-block-ecb-on-view-with-jslink/97229#97229