4

Background:

I asked this question yesterday:

How to modify the orientation of a <ul> spanning multiple columns?

asking how to convert a list like this:

a  b  c

d  e  f

g  h  i

j  k  l

into a list like this:

a  e  i

b  f  j

c  g  k

d  h  l

and I got this awesome response by beeflavor: http://jsfiddle.net/H4FPw/12/

Problem:

Unfortunately I didn't specify that there could be any number of list items, so his response is hard-coded to 4 rows, and uses a tricky matrices algorithm (read: black magic) that I can't wrap my head around.

I'm poking and prodding at this, trying to add variability but unfortunately it's not coming together for me and today's the deadline for this stuff.

This is an updated example of the problem I'm having: http://jsfiddle.net/H4FPw/13/

Is there anyone out there with a better head for this stuff who can give me a steer in the right direction?

Community
  • 1
  • 1
DaveDev
  • 41,155
  • 72
  • 223
  • 385

5 Answers5

5

I know it's not as "elegant" as your accepted answer in that question, but the code I linked to in my answer yesterday does work perfectly for you:

http://jsfiddle.net/AcdcD/

If you don't need to handle resizing, it can be simplified slightly:

http://jsfiddle.net/AcdcD/1/

Maybe you can use this if you run out of time?

Community
  • 1
  • 1
thirtydot
  • 224,678
  • 48
  • 389
  • 349
  • I saw your solution yesterday, and I thought it was good, but you stated "Well, you can if you don't care about IE".. the thing is, I do have to cater for IE so I assumed that this wouldn't be a runner. *However*, I just loaded this in IE8, IE7 & Quirks Mode and it's working perfectly! Do you mean IE6? Unfortunately I don't have a copy of IE6 (never thought I'd ever say that!), so I can't test it in that. What is the effect in IE6? – DaveDev Jun 03 '11 at 10:51
  • The "if you don't care about IE" was just for CSS3 columns, see: http://caniuse.com/#search=multiple%20column. CSS3 columns would have been the *perfect* solution, but that darned IE.. The jQuery will work in all browsers. (I just checked IE6, and it also seems to work there). – thirtydot Jun 03 '11 at 10:53
  • Dude, in my opinion you're the king of the world today. This is excellent. – DaveDev Jun 03 '11 at 11:20
3

jQuery plugin .transpose()

Exactly what you want it to do. I needed the same thing so I've written a general jQuery plugin that transposes any floated or inline-blocked elements that seem to be in columns but their order goes in rows.

Check out my detailed blog post with an example for transposing US states, and then head over to GitHub where the plugin is maintained and you can get a minified version (915 bytes as of version 1.2) of it as well.

All you need to do is:

$("yourSelector").transpose();

In your case that would be

$("li").transpose();

The good thing is that plugin checks how many columns are there originally (before transposition) and transposes to the same amount of columns afterwards. It doesn't add any additional HTML elements into DOM (like floated column wrappers or similar) it just rearranges existing elements. This is very good when it comes to CSS settings of the page because it doesn't interfere with them in any way shape or form.

And it also distinguishes between different lists of elements (items that are contained within different containers) as if you'd have two UL elements that need transposition. You don't have to call .transpose() on each because plugin will do that for you. You'd still just use the same selector as previously written.

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
  • Thanks for this. Looks really good. I've already implemented the other solution, but I'll definitely keep this in mind if I have to do something similar again. – DaveDev Aug 03 '11 at 09:54
0

What about this code:

jsfiddle

// I add a number of new elements to the <ul>, in this case, 'm', 'n', 'o' &'p'

var rowcount = 6; // I increase rowcount from 4 to 6 to accommodate the new elements
var colcount = 3;
var ichild=$('.directory-result-list-bottom').children();
var wf= "<li class='directory-result-text'>";
var wb= "</li>"
var k="";
var u;
var v;
var carryover=0;

$('.directory-result-list-bottom').empty();

for(var i = 0; i<rowcount; i++){
    carryover=0;
    for(var j=0; j<colcount; j ++){
        k=k+wf+(ichild[(j*rowcount)+i].innerHTML)+wb;
    }  
}    
document.getElementById('place').innerHTML=k;
Farshid Zaker
  • 1,960
  • 2
  • 22
  • 39