3

I have a requirement, an item should be added to a list, in the list web-part it should only display the title field value with unique values.

enter image description here

In the list as shown above, it should display developer team, architech team etc only once.

Is there any jquery to do this?

KumarV
  • 2,680
  • 12
  • 53
  • 98
  • Are you looking for a script to run in a page that performs a scan to remove duplicate values in the list web part or you want a script that can be used in a content editor to display list items with unique title values ? – Arvi Nov 17 '15 at 07:13
  • I need a script to run in a page that removes duplicate title column values of list items in the list view web part – KumarV Nov 17 '15 at 07:18

4 Answers4

3

You can use CSR instead of jQuery to display duplicate items only once.

In order to do that you have to write some code in

OnPreRender: removeDuplicateByTitle

In the OnPreRender you have to remove duplicates from ctx.ListData.Row. ctx.ListData.Row is an array of objects.

There are many ways to remove duplicates. You can try lodash.

function removeDuplicateByTitle(ctx) {
      ctx.ListData.Row = _.uniq(ctx.ListData.Row, 'Title');
    }

Prerequisite

Add lodash script reference in your master page or in a particular
page. Download it from here

You can use Cisar for CSR. Find full code in JS Bin

View before applying JSLink

enter image description here

View after applying JSLink

enter image description here

See this post by Danny Engelman for using Cisar

Atish Kumar Dipongkor
  • 13,371
  • 5
  • 32
  • 64
3

Have you tried the group-by function in your view? It's not what you aksed for, but maybe it is enough

Go to your list

Click on the thre dots and choose "modify view"

Scroll down to "Group by"

Choose the Column you want to group by (probably "Title" in your case)

Dave
  • 617
  • 1
  • 7
  • 22
2

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

Danny '365CSI' Engelman
  • 21,176
  • 7
  • 35
  • 79
2

You will be required to follow a 2 stepped approach in order to get unique values from the List.

The stages would be:

  1. Getting all list items from the List
  2. Using LINQ to get unique values from ListItemCollection.

Use the code given below:

//1 Execute query against SP Linq provider   
string keyCol = "SPFieldName";
CamlQuery keyColumnValuesQuery = CamlQuery.CreateAllItemsQuery();
ListItemCollection keyColumnValues = srcDocLib.GetItems(keyColumnValuesQuery);
spContext.Load(keyColumnValues, items => items.Include(item => item[keyCol]), items => items.OrderBy(item => item[keyCol]));
spContext.ExecuteQuery();
//2. Transform LiItemCollection to a List and perform Distinct operation 
var uniqueKeyColumnValues = keyColumnValues.ToList().Select(i => i[keyCol]).Distinct(); 
Mancy Desaee
  • 1,817
  • 4
  • 23
  • 46