javascriptsharepointjslink

what is wrong with this js link code? the array should show its data but it gives undefined. I want to display data in two columns


(function () {

    var itemCtx = {};
    itemCtx.Templates = {};

        itemCtx.Templates.Header = "<div><b>Announcements</b></div><table>";
        itemCtx.Templates.Item = ItemOverrideFun;
        itemCtx.Templates.Footer = "</table>";



    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(itemCtx);

})();


function ItemOverrideFun(ctx) {

var _announcementTitle = ctx.CurrentItem.Title;


var _announcementID = ctx.CurrentItem.ID;


return "<tr><td><p><b>" + _announcementTitle + "</b></p></td></tr>";

}

---with this code m getting values row by row as below:

1)Book Your Summer Vacation Now

2)We were Recognized as Exceptional Service

3)Ancmnts

4)XYX

----but i need to display it side by side as

1)Book Your Summer Vacation Now 2) We were Recognized as Exceptional Service

3)Ancmnts 4) XYZ

so i have made some changes in the code and used an array for it, now m getting format as two columns but m not getting data as array value is displayed undefined.

(function() {
    var itemCtx = {};
    itemCtx.Templates = {};
    enter code here
    itemCtx.Templates.Header = "<div><b>Announcements</b></div>";

    itemCtx.Templates.Item = ItemOverrideFun;

    itemCtx.Templates.Footer = "</table>";

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


function ItemOverrideFun(ctx) {
    var _announcementTitle = ctx.CurrentItem.Title;
    var _announcementID = ctx.CurrentItem.ID;
    var myArray = new Array();

    for (var i = 0; i < 5; i++) {
        myArray[i] = _announcementTitle;
    }

    return "<table><tr><td><p><b>" + myArray[i] + "</b></p></td><td><p><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + myArray[i + 1] + "</p></b></td></tr></table>";
}

Array value is not coming in its proper way,it is displaying undefined in in place of its value.

Result:

Announcements

undefined undefined

undefined undefined

undefined undefined


Solution

  • I guess you need to render list items from Announcements list horizontally, right?

    The following example demonstrates how to accomplish it:

    SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {
    
        SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
    
          Templates: {
               View: renderAnnouncementsView
          },
    
          ListTemplateType: 104
    
        });
    
    });
    
    
    function renderAnnouncementsView(rCtx)
    {
        var listData = rCtx.ListData;
        var viewHtml ='';
    
        viewHtml = '<table><tr>';
        for (var idx in listData.Row) {
            var listItem = listData.Row[idx];
            viewHtml += '<td>';
            viewHtml += listItem.Title;
            viewHtml += '</td>';
        }
        viewHtml += '</tr></table>';
        return viewHtml;
    }