Blog

How to add scrollbars to a block (e.g. a <div> element)

If you need to customize a list box then the <select> HTML element might not be enough. In such a case you’d like to have something that simulates the <select> behaviour.

Here is the code to use to obtain a custom list box:

     
<html>
  <head>
  </head>
  
  <body>
    <div style="width: 200px; height: 200px; overflow: auto;">
      <table width="100%" height="100%">
        <script>
          for (var i = 0; i < 30; i++) {
            if (i % 2 == 0) {
              document.write(
              "<tr style='background-color:#0066ff; color: #ffffff'>" +
              "<td>JavaScript</td></tr>");
            }
            else {
              document.write(
              "<tr style='background-color:#ff6600; color: #ffffff'>" +
              "<td>JavaScript</td></tr>");
            }
          }
        </script>
      </table>
    </div>
  </body>
</html>

The key CSS element is overflow. If you use overflow: auto then the scrollbars are displayed only if they are needed. If you want to display them in any case then you need to use overflow: scroll.

Read more...

Sorting an array of objects with the sortBy method of Prototype

Here is an example of how easy it is to sort an array of objects using the Enumerable.sortBy method of the Prototype framework:

    
var obj1 = {
  lastName : "Lacava",
  firstName : "Alessandro"
};

var obj2 = {
  lastName : "Brown",
  firstName : "John"
};

var obj3 = {
  lastName : "Simpson",
  firstName : "Bart"
};

var arr = [obj1, obj2, obj3];

//order by last name
var sorted = arr.sortBy(function(obj) {
    return obj.lastName.toLowerCase();
  }
);

var str = "";
sorted.each( function(obj, index) {
    str += index + 1 + " - " + obj.lastName + " " + obj.firstName;
  }
);

//display the elements ordered by last name
document.write(str);
Read more...

Why you shouldn't be concerned about optimization

When you were at the university or in any programming course, you–like me–were probably thought of the importance of optimization. In the real world though, you should strive for writing good code–possibly using bullet-proof Design Patterns–and defer optimization until later. Actually, you should optimize your code only if, after testing, you realize that optimization is strongly necessary. There are three aphorisms concerning optimization that you cannot but know:

  1. More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason, including blind stupidity. William A. Wulf

    Read more...

How to select any character across multiple lines in Java

You can do that using the following pattern in the compile static method of the java.util.regex.Pattern class. The pattern is (.|n|r)*? which means: any character (the .) or (the |) n or r. Zero or more times (the *) of the whole stuff.

Example: The following method strips the multiline comments (those between /* and */) from a string passed in and returns the resulting string:

 
import java.util.regex;

// ... other code 

// Strip multiline comments
public void deleteMultilineComments(String subject) {
  Pattern pattern = Pattern.compile("(/\\*(.|n|r)*?\\*/)");
  Matcher matcher = pattern.matcher(subject);
  return matcher.replaceAll("");
}

Note: rn works for Windows systems. n works for Unix-like systems. r works for Mac systems.

Read more...