As Atish asked for a suggestion in his other answer...
This OnPostRender function filters out the duplicates after the list is displayed.
var allValues=[];
ctx.ListData.Row.forEach(function(item){
var duplicateCheck=item.Title,
TR=document.getElementById(GenerateIIDForListItem(ctx,item));
if(allValues.indexOf(duplicateCheck)>-1) TR.style.display='none';
allValues.push(duplicateCheck);
});
- This code displays the whole List, then hides duplicates.
It is better to use either the OnPreRender
or Item
template function to not display duplicates at all.
Update #1
As stated in the comments, you probably want this in the OnPostRender
function
because you want your users to see what the Duplicates are
I had to replicate some licensed code to add a Toggle button.
The licensed code is complete OOP and adds a Duplicate filter to all Columns.
This will get you started:
OnPostRender : function () {
var check = 'Title', //name of Item field to check for duplicates
all = [], //holds all values
duplicates = [], //holds all duplicate TR elements, EXCLUDING the first value
duplicatesShown = true,//will be reset on first init
button = document.createElement('BUTTON');
function addToggleButton() {
button.onclick = function (event) {
event.preventDefault();
duplicatesShown = !duplicatesShown;
duplicates.forEach(function (TR) {
TR.style.display = duplicatesShown ? 'inherit' : 'none';
});
button.innerHTML = (duplicatesShown ? 'Hide' : 'Show') + ' Duplicates';
};
document.getElementById('CSRListViewControlDivWPQ2').appendChild(button);
};
ctx.ListData.Row.forEach(function (item) {
if (all.indexOf(item[check]) > -1) {
var TR = document.getElementById(GenerateIIDForListItem(ctx, item));
TR.style.backgroundColor = 'pink';
duplicates.push(TR);
}
all.push(item[check]);
});
addToggleButton();
button.click();
}
I am still learning every day and copy/paste/pick apart examples as well
Just realised yesterday all examples out there declare the ctx
variable,
which is totally not required as ctx
is a Global object and in JS objects are passed by reference.
I tried to stick that TR reference on the item
but that doesn't work.. Seems SharePoint keeps it as a an immutable object... ah well.. more to investigate
If you want to highlight all duplicate values you have to add another loop for all items AFTER the first initialisation
that is probably why Atish had a loop IN a loop in his original code
ICSR