Dynamically sort HTML table with row categories

There are some good JS libraries that make HTML tables dynamically accessible to the client, but I could not find one that handles the rows that are classified. Here is a small example of a table that I have in mind:

╔═══╦═════════╦═══════╦══════════════╗ β•‘ β•‘ Nameβ–Ύ β•‘ Zip β•‘ Pet β•‘ ╠═══╬═════════╬═══════╬══════════════╣ β•‘ β•‘ Alice β•‘ 14124 β•‘ Squirrel β•‘ β•‘ A β•‘ Alfred β•‘ 24601 β•‘ Meerkat β•‘ β•‘ β•‘ Anupam β•‘ 91532 β•‘ Gila monster β•‘ ╠═══╬═════════╬═══════╬══════════════╣ β•‘ β•‘ Bernice β•‘ 03413 β•‘ Rock β•‘ β•‘ B β•‘ Boris β•‘ 32610 β•‘ Fish β•‘ β•‘ β•‘ Betty β•‘ 71011 β•‘ Elephant β•‘ β•šβ•β•β•β•©β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•β•β•β• 

Someone wrote a table sorter that will automatically split the categorization column when the table is sorted by Zip or Pet columns (now it looks like this:

 ╔═══╦═════════╦═══════╦══════════════╗ β•‘ β•‘ Name β•‘ Zipβ–Ύ β•‘ Pet β•‘ ╠═══╬═════════╬═══════╬══════════════╣ β•‘ B β•‘ Bernice β•‘ 03413 β•‘ Rock β•‘ β•‘ A β•‘ Alice β•‘ 14124 β•‘ Squirrel β•‘ β•‘ B β•‘ Boris β•‘ 22310 β•‘ Fish β•‘ β•‘ A β•‘ Alfred β•‘ 24601 β•‘ Meerkat β•‘ β•‘ B β•‘ Betty β•‘ 71011 β•‘ Elephant β•‘ β•‘ A β•‘ Anupam β•‘ 91032 β•‘ Gila monster β•‘ β•šβ•β•β•β•©β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•β•β•β• 

) and re-merge it again when the table is sorted by name? Otherwise, what about a table sorter that knows how to sort only within categories, leaving only the order of the categories? (This is acceptable for a real table that I want to sort, although obviously not for this toy example.)

I will also be happy to accept the best ideas for this situation if anyone has it.

+4
source share
1 answer

Ok, I tried to create code for your problem. This is not the best way to do this, but it seems to have worked. Let me know if this is good for you, and if so, if you are fine, to implement it the way you want.

I will try to improve it and turn it into a jQuery plugin:

http://jsfiddle.net/chambs/TRnP7

 var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''); var data = [ {name: 'Alice', zip: '14124', pet: 'Squirrel'}, {name: 'Alfred', zip: '24601', pet: 'Meerkat'}, {name: 'Anupam', zip: '91532', pet: 'Gila monster'}, {name: 'Bernice', zip: '03413', pet: 'Rock'}, {name: 'Boris', zip: '32610', pet: 'Fish'}, {name: 'Betty', zip: '71011', pet: 'Elephant'} ]; var lastType = ''; function getHeader() { var row = this[0], header = []; for(var k in row) { header.push(k); } return header; } function sortBy(criteria) { lastType = criteria; var c = criteria || 'name'; return this.sort(function(a, b) { return a[c] > b[c]; }); } function render(tbl, data) { tbl.empty(); var buffer = "<tr><th class='letter'>&nbsp;&nbsp;</th>", header = getHeader.call(data); for(var i=0; i < header.length; i++) { buffer += "<th class='sort' data-type='"+header[i]+"'>"+header[i]+"</th>"; } buffer += "</tr>"; tbl.append(buffer); buffer = ""; for(var i=0; i < data.length; i++) { var l = data[i].name.substr(0, 1); buffer += "<tr><td class='"+l+"'>" + l + "</td>"; for(var j=0; j < header.length; j++) { var k = header[j]; buffer += "<td>" + data[i][k] + "</td>"; } buffer += "</tr>"; tbl.append(buffer); buffer = ""; } if(lastType === 'name') { merge(tbl); } } function merge(tbl) { for(var i=0; i < letters.length; i++) { var l = letters[i]; var td = $('.' + l, tbl); if(td.length > 1) { td.eq(0).attr('rowspan', td.length); $('.' + l + ':gt(0)', tbl).remove(); } } } sortBy.call(data, 'name'); render($('#tbl'), data); $(document).on('click', '.sort', function(ev) { var type = $(this).data('type'); sortBy.call(data, type); render($('#tbl'), data); }); 
+1
source

All Articles