Menu

multiple drop downs in one page

Yann
2006-06-30
2013-04-11
  • Yann

    Yann - 2006-06-30

    I needed to have multiple lists on the same page, I changed the code a bit to use a dataSrc attribute.
    The code will now read the attribute and reset the list. Seems to be working fine.
    function processSmartInput(inputBox) {
        if ( lastCollectionName != null && inputBox.getAttribute( "dataSrc" ) != null  && inputBox.getAttribute( "dataSrc" ).length > 0 && lastCollectionName != inputBox.getAttribute( "dataSrc" ) ) {
            updateCollection( eval( inputBox.getAttribute( "dataSrc" ) ) );
        }
        else if ( lastCollectionName == null && inputBox.getAttribute( "dataSrc" ) != null && inputBox.getAttribute( "dataSrc" ).length > 0  ) {
            updateCollection( eval( inputBox.getAttribute( "dataSrc" ) ) );
        }
        lastCollectionName = inputBox.getAttribute( "dataSrc" );

    function updateCollection( newCollection ) {
        collection = newCollection;   
        collectionIndex = new Array();
    }
    is basically all I had to do.

    Thanks for the code in the first place.

     
    • Chris Holland

      Chris Holland - 2006-06-30

      NICE!

       
    • Evan Miller

      Evan Miller - 2007-01-06

      Another way to do this is to structure your data sources like this:

      var my_data = { addresses: [ ... ],
                      searches: [ .... ],
      };

      Then put the associated key in the "dataSrc" attribute, so the patch would just look like:

      function processSmartInput(inputBox) {
      collection = my_data[inputBox.getAttribute("dataSrc");
      ...

      This might save a few CPU cycles because you're not re-creating the whole data object each time the input box switches, just reassigning a reference. And you're not calling "eval". (But I'm no JavaScript expert, so take all this with a quantity of salt about the size of that mountain of cocaine that Al Pacino buries his face in at the end of Scarface.)

       
    • Barry Priest

      Barry Priest - 2008-08-02

      implemented Evan Miller's Suggestion:
      it required a few other changes:

      in wick_sample.html changed collection.length to new TotalCollectionLength

      <!-- WICK STEP 3: INSERT WICK LOGIC -->
      <script>
      TotalCollectionLength=0
      for (var dataSrc in my_data)
      {
      TotalCollectionLength += my_data[dataSrc].length
      }
      document.getElementById("wickStatus").innerHTML = '<a target="_blank" href="./sample_data.js">Loaded <b>' + TotalCollectionLength + '</b> Sample Addresses</a>';
      </script>
      ---------------------------------------
      in wick.js:
      Changed runMatchingLogic to include dataSrc as 3rd calling argument:

      function runMatchingLogic(userInput, standalone, dataSrc) {
      ...
              collection = my_data[dataSrc]; //addded
              pointerToCollectionToUse = collection;
      ---------------------------------------
      in wick.js:
      fixed calls to runMatchingLogic with 3rd Arg:
      in function smartInputData
      ...
      runMatchingLogic(userInput,false,siw.inputBox.getAttribute("dataSrc"));

      in function activateCurrentSmartInputMatch:
      ...
      runMatchingLogic(addedValue, true, siw.inputBox.getAttribute("dataSRC"));

      comment out or fix comma replacement:
      //note: instruct users to the fact that no commas should be present in entries.
      //it would make things insanely messy.
      //this is why i'm filtering commas here:
      //for (x=0;x<collection.length;x++) {
      //collection[x] = collection[x].replace(/\,/gi,'');
      //}//

      I commented it  out but you could use something similar to to the TotalCollectionLength to fix ... like (untested):

      for (var dataSrc in my_data)
      {
      for (x=0;x<my_data[dataSrc].length;x++) {
      my_data[dataSrc][x] = my_data[dataSrc][x].replace(/\,/gi,'');
      }
      }

       
    • Barry Priest

      Barry Priest - 2008-08-02

      For what it's worth:
      I typoed DataSRC  above:

      >in function activateCurrentSmartInputMatch:
      >...
      >runMatchingLogic(addedValue, true, siw.inputBox.getAttribute("dataSRC"));

      It should be:
      in function activateCurrentSmartInputMatch:
      ...
      runMatchingLogic(addedValue, true, siw.inputBox.getAttribute("dataSrc"));

      still worked though.

       

Log in to post a comment.

MongoDB Logo MongoDB