?
Solved

javascript : check for duplicates

Posted on 2014-08-22
11
Medium Priority
?
135 Views
Last Modified: 2014-09-03
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
0
Comment
Question by:royjayd
  • 5
  • 4
  • 2
11 Comments
 
LVL 6

Expert Comment

by:J N
ID: 40298625
Will the jquery unique function work?
http://api.jquery.com/jquery.unique/
0
 

Author Comment

by:royjayd
ID: 40299331
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.
0
 
LVL 6

Expert Comment

by:J N
ID: 40299347
how is val formatted?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:royjayd
ID: 40299419
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}
        ],
0
 
LVL 6

Expert Comment

by:J N
ID: 40299684
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
0
 
LVL 6

Assisted Solution

by:J N
J N earned 200 total points
ID: 40299693
val = 'jayme nagy jayme jayme';

function firstName(val){
     var trimmed = val.trim();

     //see if the string contains a space (jayme nagy)
     if(trimmed .indexOf(" ") > 0){
         //convert string into array
         var names = trimmed.split(" ");
		 
		 //replace first name
		 var other = trimmed.replace(names[0], "");
		
		//test for more similar names
		if(other.indexOf(names[0]) >0){
			//there is a duplicate	
			return '<span style="color:orange;">' + val + '</span>';
			console.log(val);
		}
    }
	
}
firstName(val);

Open in new window

0
 
LVL 83

Expert Comment

by:leakim971
ID: 40299835
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

0
 

Author Comment

by:royjayd
ID: 40301463
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');
0
 
LVL 83

Accepted Solution

by:
leakim971 earned 1800 total points
ID: 40301494
test page : http://jsfiddle.net/9mhv2z45/3/
var maco = {};
function firstnameRenderer(val) {
    if( !maco[val] ) {  // check if it already exists (return null if not)            
        alert('Inside else'); 
        maco[val] = true;
        return '<span style="color:orange;">' + val + '</span>';      
    }
    else {
        alert('Inside if');   
        return val;
    } 
}

Open in new window

0
 

Author Comment

by:royjayd
ID: 40301612
thanks leakim971 and everyone.
0
 

Author Closing Comment

by:royjayd
ID: 40301617
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question