Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Multiple Html with columns - possible?

I'm trying to achieve the following:

<select ...>
  <option>Column 1     Column 2</option>
  <option>Line 1       Data 1</option>
  <option>Line 2       Data 2</option>
  <option>Line 3       Data 3</option>
  <option>...          ...</option>
  <option>Line n       Data n</option>
</select>

Without using a fixed-width font. I have an option + description that I would like to display for each of the options in the <select multiple />.

Is this possible with straight css/html, or do I need to look for a plugin?

like image 983
SB2055 Avatar asked Apr 28 '26 10:04

SB2055


2 Answers

The first snippet works for two-column select list while the second one can handle multiple columns. Semicolon ; is used as a separator.

// two-column multiple select list
window.onload = function(){
  var s = document.getElementsByTagName('SELECT')[0].options, 
      l = 0, 
      d = '';
  for(i = 0; i < s.length; i++){
    if(s[i].text.length > l) l = s[i].text.length; 
  }
  for(i = 0; i < s.length; i++){
    d = '';  
    line = s[i].text.split(';');
    l1 = (l - line[0].length);
    for(j = 0; j < l1; j++){
      d += '\u00a0'; 
    }
    s[i].text = line[0] + d + line[1];
  }  
};

Working jsBin

// multiple-column multiple select list
window.onload = function(){

  var s = document.getElementsByTagName('SELECT')[1].options, l = [];

  for(i = 0; i < s.length; i++){
    column = s[i].text.split(';');
    for(j = 0; j < column.length; j++){
      if(!l[j]) l[j] = 0;
      if(column[j].length > l[j]){
        l[j] = column[j].length;
      }      
    }    
  }  

  for(i = 0; i < s.length; i++){
    column = s[i].text.split(';');
    temp_line = '';
    for(j = 0; j < column.length; j++){
      t = (l[j] - column[j].length);
      d = '\u00a0';
      for(k = 0; k < t; k++){
        d += '\u00a0';
      }
      temp_line += column[j] + d;
    }
    s[i].text = temp_line;    
  }  

};

Working jsBin

like image 176
hex494D49 Avatar answered Apr 30 '26 22:04

hex494D49


Just a different approach - FIDDLE.

Used code from the jQuery 'selectable' and adapted it.

Used divs to create the columns.

CSS

#holder .ui-selecting {
    background: #e0e6fa;
}
#holder .ui-selected {
    background: #d1daf4;
    color: white;
}
#holder {
    list-style-type: none; 
    margin: 0; 
    padding: 0; 
    width: 270px; }
div {
    display: inline-block;
}
li div:first-child {
    width: 100px;
    color: red;
    padding: 3px 3px 3px 10px;
}
li div:nth-child(2) {
    width: 120px;
    color: blue;
    padding: 3px 3px 3px 10px;   
}
like image 43
TimSPQR Avatar answered Apr 30 '26 22:04

TimSPQR