July 5, 2006

Adding scrolling to the ScriptAculoUs autocomplete web control

ScriptAculoUs autocomplete web control from SimoneB is a nice lightweight easy to use ASP.NET autocomplete textbox control with many virtues. The only problem I had with it was that dropdown autocomplete list has no scrolling and so long autocomplete lists look ugly. Happily it comes with sources so I hacked it to add scrolling, here is the solution in case somebody needs it.

  1. In Suggestion.cs add a CSS class to the UL tag generated for an autocomplete list:
    StringBuilder returnValue = new StringBuilder("<ul class=\"autocomplete\">");
  2. In a stylesheet constrain autocomplete list height and enable scrolling on overflow:
    UL.autocomplete 
    {
        height: 10em;
        overflow:auto;
    }
    
  3. In controls.js, modify "render" function to make autocomplete list automatically scrolling so a selected item is always visible:
      render: function() {
        if(this.entryCount > 0) {
          for (var i = 0; i < this.entryCount; i++) {
            this.index==i ? 
              Element.addClassName(this.getEntry(i),"selected") : 
              Element.removeClassName(this.getEntry(i),"selected");        
            if (this.index == i) {
              var element = this.getEntry(i);
              element.scrollIntoView(false);
            }
          }        
          if(this.hasFocus) { 
            this.show();
            this.active = true;
          }
        } else {
          this.active = false;
          this.hide();
        }
      },
    
That does it. Works fine in both IE and FF.

July 5, 2006 1:00 PM | #ASP.NET
Comments

Thanks for the code and I think it would be helpful to mark the curly brackets of 'for' loop with blue as they don't appear in the original code.

Posted by: Andrey at September 6, 2007 11:38 AM

David, wouldn't it be fair to ask before publishing someone else's work on your website?
Btw I didn't include any external css because I embedded it in the code, and customized the css class name so that it applies only to the autocomplete control and not to other unordered lists which eventually have a css class name called "autocomplete".

Posted by: Simone at July 8, 2006 5:27 AM

Hi Oleg, I have already included your solution into the control. Thanks again. Can I just ask you to change the url that points to my blog post since I switched to DotNetSlackers? The new post url is: http://dotnetslackers.com/community/blogs/simoneb/archive/2006/07/03/142.aspx

Thanks

Posted by: Simone at July 6, 2006 8:55 PM

Hey Oleg,

Very cool! I've added it as a vendor project into the extensibleforge.net repository > http://dev.extensibleforge.net/browser/vendor/ScriptAculous.NET

It seems SimoneB beat me to the punch, and added this to the downloadable source base, but I did add a style.css file into a Resources folder as I couldn't find a default CSS file anywhere.

Thanks for the link and code!

Posted by: M. David Peterson at July 6, 2006 3:39 PM
Post a comment




Remember Me?