Created
March 9, 2012 10:05
-
-
Save alvinsj/2005924 to your computer and use it in GitHub Desktop.
[jquery] group by word
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
$.group_by_word = (ul_class, sortable, sub_string_count=0)-> | |
$('ul.group').empty() | |
count = 0 | |
word = '' | |
$("ul.#{ul_class} li").each (index, li_el) -> | |
# get attributes | |
str = if sub_string_count is 0 then $(li_el).data(sortable) else $(li_el).data(sortable).toString().substring(0,sub_string_count) | |
content = $(li_el).html() | |
# reset element when letter change | |
if word isnt str | |
word = null | |
# new character, build box | |
unless word | |
count++ | |
word = str | |
$('ul.group').append("<li id='box-#{count}' class='group-item'><div class='outer'><div class='title'>#{word}</div><ul class='subgroup'></li>") | |
# prepare item | |
item = $("<a id='item-#{count}'>#{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-#{count} ul.subgroup").append(el) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment