のように、にdiv
基づいて一意の注文番号でラベル付けされた親のコレクションがあります。それぞれに、注文に関する情報(注文番号(上記と同じ)、名前、注文日(mm / dd / yy))を含む子divがあります。id
<div id='parent_uniqueID'>
これらの各列に基づいて、これらの親divを並べ替えようとしています。
注文番号で並べ替えたり、親divを並べ替えたりするのは簡単です。それらを並べ替えてから、$('#parent_' + order_number); `のような親divを選択し、ページに追加します。英数字の場合も簡単です。英数字の文字列の最後に、並べ替える前に順序を追加します。これは、2つの注文に同じ名前が付いている場合、親divがデフォルトで注文番号で並べ替えられるため便利です。
日付で並べ替えた後、どの日付がどの親divに属しているかを区別して、親divを取得ids
し、ページ上で再配置する方法がわかりません。
数値による並べ替えを理解しようとしたときに、この非常に優れたソリューションを受け取りました。これは、日付に適応させようとしています。
$('.sortable').click(function () {
if ($(this).hasClass('sortable_date')) {
var sort_order = sort_class_distribution($(this)),
sort_column = this.id.replace("header_", "") + "_div",
content2 = $('#currentOrdersContent2'),
date_holder = "",
i = 0;
var sorted = $('.' + sort_column).map(function () {
date_holder = $.trim($(this).text());
//get order number from parent div
//only_numbers function strips out everything but numbers
cur_order = only_numbers($(this).parents('.order_details_bg_trigger').attr('id'));
//check that the text is not "Not given" or "Unavailable"
if (date_holder.indexOf("/")) {
cur_date = date_holder.split(" / ");
//change the text string into a proper javascript date
return new Date(cur_date[2], cur_date[0] - 1, cur_date[1]);
} else {
//if it is 'Not given', assign it today's date
return new Date();
}
}).toArray().sort(sort_order = "asc" ? date_sort_asc : date_sort_desc);
//change javascript date back to mm / dd / yy format
//weak attempt to match sorted dates with dates contained in table
$.each(sorted, function (i, item) {
cur_date = ('0' + (item.getMonth() + 1)).slice(-2) + ' / ' + ('0' + item.getDate()).slice(-2) + ' / ' + item.getFullYear();
});
}
});
これらは日付ソート関数です: ソース:https ://gist.github.com/1772996
function date_sort_asc(date1, date2) {
if (date1 > date2) return 1;
if (date1 < date2) return -1;
return 0;
}
function date_sort_desc(date1, date2) {
if (date1 > date2) return -1;
if (date1 < date2) return 1;
return 0;
}
これを行う方法に関する提案、または新しいパッテンを歓迎します。ありがとう!