It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

JavaScript filter

JavaScript filter

Postby saiko on Fri Mar 06, 2009 9:22 am

Hello everyone,

I'm referring to you for some help on JS filter, I have just recently started with JS so I don't know a lot of even major basic things. Please, don't be hard on me :)

You will see below a code I have adopted to my needs - so far so good. But there are one minor glitch, the glitch is that there are 17 competencies, however JS returns when selected, for example, 14 as 14 and 4 too. So, if I write something like this in my code:

Code: Select all
   p = new Array();
      p['name'] = 'Company 3';
      p['cityJuridical'] = '1';
      p['address'] = 'Address 3';
      p['contact'] = 'Contact 3';
      p['competencies'] = '1:10:12';
      p['status'] = '1';
   partnersJuridical.push(p);


I will have as return 1, 10, 12 and 2 competency which isn't needed!

Code: Select all
var competencies = new Array();
   competencies.push(' ');
   competencies.push('Competency 1');                     //1
   competencies.push('Competency 2');                     //2
   competencies.push('Competency 3');                     //3
   competencies.push('Competency 4');                     //4
   competencies.push('Competency 5');                     //5
   competencies.push('Competency 6');                     //6
   competencies.push('Competency 7');                     //7
   competencies.push('Competency 8');                     //8
   competencies.push('Competency 9');                     //9
   competencies.push('Competency 10');                     //10
   competencies.push('Competency 11');                     //11
   competencies.push('Competency 12');                     //12
   competencies.push('Competency 13');                     //13
   competencies.push('Competency 14');                     //14
   competencies.push('Competency 15');                     //15
   competencies.push('Competency 16');                     //16
   competencies.push('Competency 17');                     //17

var cities = new Array();
      cities.push(' ');
      cities.push('New York');                        //1
        cities.push('Tokyo');                           //2
        cities.push('Athens');                           //3

var pStatus = new Array();
   pStatus.push(' ');
   pStatus.push('Gold');                              //1
   pStatus.push('Silver');                              //2

var partnersJuridical = new Array();
   p = new Array();
      p['name'] = 'Company 1';
      p['cityJuridical'] = '1';
      p['address'] = 'Address 1';
      p['contact'] = 'Contact 1, [http://example1.com]';
      p['competencies'] = '1:10:';
      p['status'] = '1';
   partnersJuridical.push(p);

      p = new Array();
      p['name'] = 'Company 2';
      p['cityJuridical'] = '2';
      p['address'] = 'Address 2';
      p['contact'] = '[http://example2.com]';
      p['competencies'] = '2:7:8:';
      p['status'] = '2';
   partnersJuridical.push(p);

   p = new Array();
      p['name'] = 'Company 3';
      p['cityJuridical'] = '1';
      p['address'] = 'Address 3';
      p['contact'] = 'Contact 3';
      p['competencies'] = '1:10:12';
      p['status'] = '1';
   partnersJuridical.push(p);

   var fcSelect = false;
   var flSelect = false;
   var lChanged = false;
   var cChanged = false;
   
function g(what){
   return document.getElementById(what);
}
function populateJuridicalPartnerList(){
   cList = g('competency');
   for(i = 0; i < competencies.length; i++){
      option = new Option(competencies[i], i);
      cList.options.add(option);   
   }
   cList = g('cityJuridical');
   for(i = 0; i < cities.length; i++){
      option = new Option(cities[i], i);
      cList.options.add(option);   
   }
   //filter();
}
function filter(){
   if(fcSelect == false && cChanged == true){
      fcSelect = true;
      cList = g('competency');
      cList.remove(0);
   }
   if(flSelect == false && lChanged == true){
      flSelect = true;
      lList = g('cityJuridical');
      lList.remove(0);
   }

   t = g('partnersJuridical');
   while(t.childNodes.length > 0){
      node =    t.childNodes[0];      
      t.removeChild(node);
   }
   
   l = partnersJuridical.length;

   comp = g('competency').value;
   loc = g('cityJuridical').value;
   //status = g('status').value;
   for(i = 0; i < l; i++){
      if(comp > 0 && partnersJuridical[i]['competencies'].indexOf(comp + ':') < 0)
         continue;
      if(loc > 0 && partnersJuridical[i]['cityJuridical'] != loc)
         continue;

      var row = document.createElement('tr');

      var cell = document.createElement('td');
      var img = document.createElement('img');
      if(pStatus[partnersJuridical[i]['status']] == 'Gold')
         img.src = 'goldStatus.jpg';
      else
         img.src = 'certifiedStatus.jpg';
      
      var text = document.createTextNode(partnersJuridical[i]['name']);
      cell.appendChild(img);
      var br = document.createElement('br');
                cell.appendChild(br);
                cell.appendChild(text);
      cell.className = 'partnerName';

      row.appendChild(cell);

      var cell = document.createElement('td');
      var comps = partnersJuridical[i]['competencies'].split(':');
      ll = comps.length;
      compsText = '';
      var lst = document.createElement('ul');
      for(ii = 0; ii < ll; ii++){

         if(comps[ii].length > 0){
            
            compsText = competencies[comps[ii]];
            var text = document.createTextNode(compsText);
            var li = document.createElement('li');
            var sp = document.createElement('span');
            sp.appendChild(text);
            li.appendChild(sp);
            lst.appendChild(li);
         }
      }
      cell.appendChild(lst);
      cell.className = 'partnerCompetencies';
      row.appendChild(cell);

      var cell = document.createElement('td');
      var text = document.createTextNode(partnersJuridical[i]['address']);
      cell.appendChild(text);
      var br = document.createElement('br');
      cell.appendChild(br);
      
      contactText = partnersJuridical[i]['contact'];
      start = contactText.indexOf('[');
      end = contactText.indexOf(']');
      if(start > -1 && end > -1){
         lnk = contactText.substring(start + 1, end);
         var linkNode = document.createElement('a');
         linkNode.href = lnk;
         linkNode.innerHTML = lnk;
         linkNode.target = '_blank';
         contactText = contactText.substring(0, start - 1) + ' ';// + linkHTML + contactText.substring(end + 1);
         var text = document.createTextNode(contactText);
         var br = document.createElement('br');
         cell.appendChild(text);
         cell.appendChild(br);
         cell.appendChild(linkNode);
      } else {
         var text = document.createTextNode(partnersJuridical[i]['contact']);
         cell.appendChild(text);
      }
      row.appendChild(cell);
      cell.className = 'Address';
      t.appendChild(row);
      
   }

}
saiko
 
Posts: 1
Joined: Fri Mar 06, 2009 9:01 am

Who is online

Users browsing this forum: No registered users and 10 guests