views:

157

answers:

1

Hi,

I have a set of divs table-like (with rows and cols), to display a soccer table. Now, I need to be able to order that table according to the key chosen, without refreshing the page and avoiding using ajax.

Being more specific, I need to catch the values inside each "columm" refeering to the key chosen and re-order the table.

I use jQuery, so the work should be fairly easy, just not sure how to re-order an array with the id of the team and the columm in cause. The table values are all numeric by the way.

Above is an example of the markup (row) :

<div class="rb">
        <div class="cname">Benfica</div>
        <div class="tdr bold">0</div>
        <div class="tdg bold">0</div>

        <div class="tdg bold">0</div>
        <div class="tdg bold">0</div>
        <div class="tdg bold">0</div>
        <div class="tdr bold" style="margin-left: 5px;">0</div>
        <div class="tdg bold">0</div>
        <div class="tdg bold">0</div>

        <div class="tdg bold">0</div>
        <div class="tdg bold">0</div>
        <div class="tdr bold" style="margin-left: 5px;">0</div>
        <div class="tdg bold">0</div>
        <div class="tdg bold">0</div>
        <div class="tdg bold">0</div>

        <div class="tdg bold">0</div>
        <div class="tdg bold" style="margin-left: 5px;">0</div>
        <div class="tdg bold">0</div>
        <div class="tdr bold">0</div>
</div>

Any thougts?

Thanks in advance,

Filipe

+2  A: 

Say you've got an array of objects, where every object corresponds to a table row, and every object has properties corresponding to table columns.

var table = [
   { rowId: 1, teamName: 'Man U', country: 'England' },
   { rowId: 2, teamName: 'FC Barcelona', country: 'Spain' }
];

now, if you've got your data arranged like this, you can create a function where you can just say drawTable(table) and it'll create the HTML markup you want. You can then do sorting in that variable, like this:

function sortTable(table, column) {
    table.sort(function(x,y) { return x[column] > y[column]; });
    return table;
}

$('#sortByTeamName').click(function() {
   table = sortTable(table, 'teamName');
   drawTable(table);
});
David Hedlund
that doesn't quite answer my question. the table in question has 16 teams, how do I run all the values and reorder them? Is there a javascript function that does that?
yoda
yes, and i wrote it for you. if you look at `table.sort` in the second code block, it sorts the data by whatever parameter you pass to the function. this is provided that you tell the javascript what your data looks like, which is what the first code block is all about
David Hedlund
Sorry, didn't saw that. Will try it, and give feedback later today or tomorrow. Thanks!
yoda
Wrote a short demo for you of what i mean: http://pastebay.com/72881You can paste that into a blank html file and it should work. sort by dob is alphabetic, if you want anything more advanced, you'll have to tamper with the `sort` function accordingly
David Hedlund
Oh, about what I said on date of birth, it's not the sort function that should be changed to accommodate that. The data in var table should be an actual date, rather than a string representation of one, and the sort will automatically work for that as well. the drawTable function should then be changed to create the string. Oh, we're on a side note...
David Hedlund