2 Replies Latest reply on Oct 8, 2011 1:23 AM by Tony Dew Too?

    Autocomplete editText field help please!

    Tony Dew Too?

      (Hello, I'll try posting this for the fifth sixth seventh eighth time now. The forum keeps hanging after pressing the "Post Message" button...)

       

      I'm trying to create an editText field autocomplete based on a somewhat related sample from Peter Kahrel's highly useful 'ScripUI for Dummies' PDF.

       

      My solution is not the most elegant thing in the land though; there is a jarring clearing / filling of the editText control on every keystroke with a matching result. Is there a way to set a certain range of editText characters to be selected? If so, then I might be able to make the ScriptUI version of the web based sample here : http://momche.net/res/autocomplete. If not, any suggestions on how to refine my attempt?

       

      var myArray = ['Bill Melater', 'Hugh Betcha', 'Hugh Bert', 'Keith Richards'];

       

      var w = new Window ('dialog', 'editText Autocomplete');

       

      var myText = w.add ('editText', undefined);

          myText.minimumSize.width = 200;

          myText.active = true;

       

      var myBuffer = '';

       

      myText.onActivate = myText.onDeactivate = function () { myBuffer = ''; }

       

      myText.addEventListener ('keyup', function (k)

      {

          if (k.keyName == 'Backspace')

          {

              myBuffer = myBuffer.replace (/.$/, '');

          }

          else if (k.keyName.length > 0)

          {

              myBuffer += (k.keyName == 'Space') ? ' ' : k.keyName.toLowerCase();

          }

       

          var i=0;

       

          while (i < myArray.length-1 && myArray[i].toLowerCase().indexOf (myBuffer) != 0)

          {

              ++i;

          }

       

          if (myArray[i].toLowerCase().indexOf (myBuffer) == 0 && myBuffer.length > 0)

          {

              myText.text = myBuffer + myArray[i];

          }

      });

       

      w.add ( 'button', undefined, 'OK' );

      w.show ();

       

      Thanks in advance!

       

      Message was edited by: Tony Dew Too? Trying to add the actual code snippet again.

        • 1. Re: Autocomplete editText field help please!
          Tony Dew Too? Level 1

          Sorry to bump my own thread, but I was *finally* able to add the actual code snippet to the post instead of having a link to the pastie.org sample.

          • 2. Re: Autocomplete editText field help please!
            Tony Dew Too? Level 1

            Ha! I figured it out! I honestly didn't think this was going to be possible, but...

             

            var myArray = ['Bill Melater', 'Hugh Betcha', 'Hugh Bert', 'Keith Richards'];
            
            var w = new Window ('dialog', 'editText Autocomplete');
            
            var myText = w.add ('editText', undefined);
                myText.minimumSize.width = 200;
                myText.active = true;
            
            var myBuffer = '';
            
            myText.onActivate = myText.onDeactivate = function () { myBuffer = ''; }
            
            myText.addEventListener ('keydown', function (k)
            {
                if (k.keyName == 'Backspace')
                {
                    myBuffer = myBuffer.replace (/.$/, '');
                    // Needed for string aligment later.
                    backspacer = -1;
                }
                else if (k.keyName.length > 0)
                {
                    myBuffer += (k.keyName == 'Space') ? ' ' : k.keyName.toLowerCase();
                    // Needed for string alignment later
                    backspacer = 1;
                }
            
                var i=0;
            
                while (i < myArray.length-1 && myArray[i].toLowerCase().indexOf (myBuffer) != 0)
                {
                    ++i;
                }
            
                if (myArray[i].toLowerCase().indexOf (myBuffer) == 0)
                {
                    if ( myBuffer.length > 0 )
                    {
                        entered  = myArray[i].substring(0,(myBuffer.length - backspacer));
                        leftOver = myArray[i].substring(myBuffer.length);
            
                        myText.text = leftOver;
                        myText.textselection = entered;
                    }
                    else
                    {
                        myText.text = '';
                    }
                }
            });
            
            w.add ( 'button', undefined, 'OK' );
            w.show ();