Link to home
Start Free TrialLog in
Avatar of Jay Roy
Jay RoyFlag for United States of America

asked on

javascript : check for duplicates

hi experts

I am using EXT-Js 4.2 code to design some UI for my project.

I have set a renderer in my javascript code for a grid column

function firstnameRenderer(val) {       
if(same firstname occurs twice , then color the two firstname with orange){ 
return '<span style="color:orange;">' + val + '</span>';	
}else{
    return val;
} 
} 
....
....
{
        id:'myGrid',
        xtype : 'grid', 
        stripeRows: true,
        
        title: 'User Information',
        store: Ext.data.StoreManager.lookup('mystore'),         
        autoHeight: true,
        plugins: [ cellEditing ],
        columns: [            
            {header: "First Name", width: 100, dataIndex: 'firstname', renderer: firstnameRenderer, sortable: true}
             


...

Open in new window

I want to check if a same firstname appears twice, if yes color it with orange.
Any idea how firstnameRenderer() function above should change ?

Thanks
Avatar of J N
J N

Will the jquery unique function work?
http://api.jquery.com/jquery.unique/
Avatar of Jay Roy

ASKER

I dont think so, i am using Ext-Js 4.2. Can you tell me how i can change the function
firstnameRenderer() in Ext-js

Thanks.
how is val formatted?
Avatar of Jay Roy

ASKER

sorry i dint get your question.

I have something like this working for me right nowas a test.

function firstnameRenderer(val) {        
if(val == 'Angela') {
    alert(val);
      return '<span style="color:orange;">' + val + '</span>';
}else{
    return val;
}
}

columns: [
                        {header: "First Name", width: 100, dataIndex: 'firstname', renderer: firstnameRenderer, sortable: true}
        ],
Hi,

here is a link on github for a way to elminate duplicates in a string
https://gist.github.com/lsauer/1305056

otherwise i would break the string down into parts and compare them
SOLUTION
Avatar of J N
J N

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of leakim971
var maco = {};
function firstnameRenderer(val) {
        if( maco[val] ) {  // check if it already exists (return null if not)
                maco[val] = true;
                return '<span style="color:orange;">' + val + '</span>';	
        }
        else {
               return val;
        } 
} 

Open in new window

Avatar of Jay Roy

ASKER

hi
I used the above code but looks like somethng is missing
I have
var maco = {};
function firstnameRenderer(val) {
    alert(val);
            if( maco[val] ) {  // check if it already exists (return null if not)            
            alert('Inside if');  
            maco[val] = true;
                return '<span style="color:orange;">' + val + '</span>';      
        }
        else {
             alert('Inside else');
               return val;
        }
}

val contain values
Angela
Michael
Michael
Kim

When val = Michael
alert('Inside if');   should pop up but its showing alert('Inside else');
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jay Roy

ASKER

thanks leakim971 and everyone.
Avatar of Jay Roy

ASKER

Any help with next question will be greatly appreciated
https://www.experts-exchange.com/questions/28510849/Grid-with-colored-columns.html