Created
March 9, 2012 09:33
-
-
Save alvinsj/2005841 to your computer and use it in GitHub Desktop.
[jquery] sort then group by character
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.sort_then_group_by_character = (sortable, ul_class)-> | |
$.sort_by_words(ul_class, sortable) | |
$('ul.group').empty() | |
count = 0 | |
character = '' | |
$("ul.#{ul_class} li").each (index, li_el) -> | |
# get attributes | |
str = $(li_el).data(sortable).toLowerCase() | |
content = $(li_el).html() | |
# reset element when letter change | |
if character isnt str[0] | |
character = null | |
# new character, build box | |
unless character | |
character = str[0] | |
$('ul.group').append("<li id='box-#{character}' class='group-item'><div class='outer'><div class='title'>#{character.toUpperCase()}</div><ul class='subgroup'></li>") | |
# prepare item | |
item = $("<a id='item-#{character}'>#{content}</a>") | |
$.each item.data(), (k,v) -> | |
item.data(k,$(li_el).data(k)) | |
# wrap item | |
el = $("<li class='subgroup'></li>").append(item) | |
# add item | |
$("li#box-#{character} ul.subgroup").append(el) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment