views:

2782

answers:

11

I'm using ClodFusion(sic) to populate a template that includes HTML lists (<ul>'s).

Most of these aren't that long, but a few have ridiculously long lengths and could really stand to be in 2-3 columns.

Is there an HTML, ColdFusion or perhaps JavaScript (I have jQuery available) way to do this easily? It's not worth some over-complicated heavy weight solution to save some scrolling.

+2  A: 

There is no pure CSS/HTML way to achieve this, as far as I know. Your best bet would be to do it in pre-processing (if list length > 150, split into 3 columns, esle if > 70, split into 2 columns, else 1). The other option, using javascript (I'm not familiar with the jquery library specifically) would be to iterate through lists, probably based on them being a certain class, count the number of children, and if it is a high enough number, dynamically create a new list after the first, transferring some number of list items to the new list. As far as implementing the columns, you could probably float them left, followed by an element that had the style clear:left or clear:both.

CSS:

ul.column {
float:left;
width:50%;
}
div.clear {
clear:both;
}

HTML:

<ul class="column">
<li>Item 1</li>
<li>Item 2</li>
...
<li>Item 49</li>
<li>Item 50</li>
</ul>
<ul class="column">
<li>Item 51</li>
<li>Item 52</li>
...
<li>Item 99</li>
<li>Item 100</li>
</ul>
<div class="clear" />

Good luck.

Chris Marasti-Georg
+6  A: 

So I dug up this article from A List Apart CSS Swag: Multi-Column Lists. I ended up using the first solution, it's not the best but the others require either using complex HTML that can't be generated dynamically, or creating a lot of custom classes, which could be done but would require loads of in-line styling and possibly a huge page.

Other solutions are still welcome though.

alexp206
It's sad that two years later there is still no clean way to do this.Thanks IE.
Keyo
+2  A: 

If Safari and Firefox support is good enough for you, there is a CSS solution:

UL {
column-count:3; -moz-column-count:3; -webkit-column-count:3;
column-gap:2em; -moz-column-gap:2em; -webkit-column-gap:2em;
}

I'm not sure about Opera.

doekman
A: 

I wish Safari and Firefox were my biggest concern, unfortunately our intranet is all IE but you did bring something to my attention that I was not aware of, thanks!

alexp206
A: 

To output the list into multiple grouped tag you can loop in this fashion.

<cfset list="1,2,3,4,5,6,7,8,9,10,11,12,13,14">
<cfset numberOfColumns = "3">

<cfoutput>
<cfloop from="1" to="#numberOfColumns#" index="col">
  <ul>
  <cfloop from="#col#" to="#listLen(list)#" index="i" step="#numberOfColumns#">
    <li>#listGetAt(list,i)#</li>
  </cfloop>
  </ul>
</cfloop>
</cfoutput>
Dan Roberts
A: 

The following JavaScript code works only in Spidermonkey and Rhino, and it operates on E4X nodes--i.e., this is useful only for server-side JavaScript, but it might give someone a starting point for doing a jQuery version. (It's been very useful to me on the server side, but I haven't needed it on the client badly enough to actually build it.)

function columns(x,num) {
 num || (num = 2);
 x.normalize();
 var cols, i, j, col, used, left, len, islist;
 used = left = 0;
 cols = <div class={'columns cols'+num}></div>;
 if((left = x.length())==1) left = x.children().length();
 else islist = true;
 for(i=0; i<num; i++) {
  len = Math.ceil(left/(num-i));
  col = islist ? new XMLList : <{x.name()}></{x.name()}>;
  if(!islist && x['@class'].toString()) col['@class'] = x['@class'];
  for(j=used; j<len+used; j++) islist ? (col += x[j].copy()) : (col.appendChild(x.child(j).copy()));
  used += len; left -= len;
  cols.appendChild(<div class={'column'+(i==(num-1) ? 'collast' : '')}>{col}</div>);
 }
 return cols;
}

You call it like columns(listNode,2) for two columns, and it turns:

<ul class="foo">
  <li>a</li>
  <li>b</li>
  <li>c</li>
</ul>

into:

<div class="columns cols2">
  <div class="column">
    <ul class="foo">
      <li>a</li>
      <li>b</li>
    </ul>
  </div>
  <div class="column collast">
    <ul class="foo">
      <li>c</li>
    </ul>
  </div>
</div>

It's meant to be used with CSS like this:

div.columns { overflow:hidden; _zoom:1; }
div.columns div.column { float:left; }
div.cols2 div.column { width:47.2%; padding:0 5% 0 0; }
div.cols3 div.column { width:29.8%; padding:0 5% 0 0; }
div.cols4 div.column { width:21.1%; padding:0 5% 0 0; }
div.cols5 div.column { width:15.9%; padding:0 5% 0 0; }
div.columns div.collast { padding:0; }
+1  A: 

I've done this with jQuery - it's cross-platform and a minimum of code.

Select the UL, clone it, and insert it after the previous UL. Something like:

$("ul#listname").clone().attr("id","listname2").after()

This will insert a copy of your list after the previous one. If the original list is styled with a float:left, they should appear side by side.

Then you can delete the even items from the left-hand list and the odd items from the right hand list.

$("ul#listname li:even").remove();
$("ul#listname2 li:odd").remove();

Now you have a left to right two column list.

To do more columns you'll want to use .slice(begin,end) and/or the :nth-child selector. ie, for 21 LIs you could .slice(8,14) to create a new UL inserted after your original UL, then select the original UL and delete the li's selected with ul :gt(8).

Try the Bibeault/Katz book on jQuery it's a great resource.

Thumbkin
A: 

the thing that most people are forgetting is that when floating li items, all the item have to be the same height or the columns start getting out of whack.

since you're using a server side language, my recommendation would be to use cf to split the list into 3 arrays, then you can use an outer ul to wrap the 3 inner ul like so:

<cfset thelist = "1,2,3,4,5,6,7,8,9,10,11,12,13">
<cfset container = []>
<cfset container[1] = []>
<cfset container[2] = []>
<cfset container[3] = []>

<cfloop list="#thelist#" index="i">
<cfif i mod 3 eq 0>
<cfset arrayappend(container[3], i)>
<cfelseif i mod 2 eq 0>
<cfset arrayappend(container[2], i)>
<cfelse>
<cfset arrayappend(container[1], i)>
</cfif>
</cfloop>

<style type="text/css">
ul li {float:left;}
ul li ul li {clear:left;}
</style>

<cfoutput>
<ul>
<cfloop from="1" to="3" index="a">
<li>
<ul>
<cfloop array="#container[a]#" index="i">
<li>#i#</li>
</cfloop>
</ul>
</li>
</cfloop>
</ul>
</cfoutput>

A: 

Is this jquery plugin of any use to you? http://www.givainc.com/labs/mcdropdown_jquery_plugin.htm

Quog
A: 

Using a modulo operation, you can quickly split your list into multiple lists by inserting a </ul><ul> during your loop.

<cfset numberOfColumns = 3 />
<cfset numberOfEntries = 34 />
<ul style="float:left;">
    <cfloop from="1" to="#numberOfEntries#" index="i">
     <li>#i#</li>
      <cfif NOT i MOD ceiling(numberOfEntries / numberOfColumns)>
       </ul>
       <ul style="float:left;">
      </cfif>
    </cfloop>
</ul>

Use ceiling() instead of round() to ensure that you don't have extra values at the end of the list and that the last column is shortest.

jonah
A: 

Here is a variation on Thumbkin's example (using Jquery):

var $cat_list = $('ul#catList'); // UL with all list items.
var $cat_flow = $('div#catFlow'); // Target div.
var $cat_list_clone = $cat_list.clone(); // Clone the list.
$('li:odd', $cat_list).remove(); // Remove odd list items.
$('li:even', $cat_list_clone).remove(); // Remove even list items.
$cat_flow.append($cat_list_clone); // Append the duplicate to the target div.

Thanks Thumbkin!

mhulse