In SharePoint 2013/SharePoint online using client side rendering (CSR), I am trying to remove rows from List view webpart (LVWP). I am using OnPreRender event of CSR as below:-
(function () {
var bodyFieldContext = {};
bodyFieldContext.Templates = {};
bodyFieldContext.OnPreRender = listPreRender;
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(bodyFieldContext);
})();
function listPreRender(ctx)
{
var baseURL = _spPageContextInfo.webAbsoluteUrl;
$.ajax({
url: baseURL + "/_api/Web/CurrentUser?$select=Id",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
//Below two lines have no effect here and no error is thrown
var rows = ctx.ListData.Row;
rows.splice(1, 1);
},
error: function (error) {
alert("error");
}
});
//UnComment below two lines of script, these will hide the row
//THIS WORKS OUTSIDE OF AJAX REQUEST.BUT NOT INSIDE "SUCCESS" FUNCTION ABOVE
/*var rows = ctx.ListData.Row;
rows.splice(1, 1);*/
}
The issue is that removing rows form LVWP only works out side of AJAX request, not inside (Marked by comments in code above).
Any Idea why it doesn’t execute inside, but same lines of script works outside? And how to make it work from inside of AJAX call?
The reason I have to make AJAX requests is because, I need to remove rows from current LVWP, based on values in another list. Because List A and List B has no direct common field.
In List view webpart JSLink I gave the URL of above script file:- ~site/SiteAssets/Code/FilterEmployeesCSR.js
Thanks,