[英]Javascript ordering elements based on comparing child element content with target heading content
I have n amount of items (html elements, vcard microformat), containing other elements with contents, each element represents a store and has a child element (".locality") where is kept information about a city, where the store is. 我有n项商品(html元素,vcard微格式),包含其他具有内容的元素,每个元素代表一家商店,并有一个子元素(“ .locality”),其中存储着有关商店所在城市的信息。 I want to make a section for every original city, output city´s name as a heading of section and order each item under correct section.
我想为每个原始城市创建一个区域,输出城市名称作为区域的标题,并在正确的区域下订购每个项目。 This is where I have gone so far:
到目前为止,这是我去过的地方:
http://jsfiddle.net/eliasondrej/0zqhpfLn/1/
HTML: HTML:
<table class="vcard">
<tr class="info">
<td>
<h3 class="fn org">some text </h3>
</td>
<td>
some text
</td>
<td>
<p class="addr">
<span class="locality">New York </span> <br />
</p>
</td>
</tr>
</table>
<table class="vcard">
<tr class="info">
<td>
<h3 class="fn org">some text </h3>
</td>
<td>
some text
</td>
<td>
<p class="addr">
<span class="locality">Paris </span> <br />
</p>
</td>
</tr>
</table>
<table class="vcard">
<tr class="info">
<td>
<h3 class="fn org">some text </h3>
</td>
<td>
some text
</td>
<td>
<p class="addr">
<span class="locality">Rome </span> <br />
</p>
</td>
</tr>
</table>
<table class="vcard">
<tr class="info">
<td>
<h3 class="fn org">some text </h3>
</td>
<td>
some text
</td>
<td>
<p class="addr">
<span class="locality">Berlin </span> <br />
</p>
</td>
</tr>
</table>
</div>
jQuery: jQuery的:
jQuery(document).ready(function() {
var prodejny = [];
var localityTexts = [];
jQuery(".vcard .locality").each(function() {
localityTexts.push(jQuery(this).text() );
});
jQuery(".vcard").each(function() {
prodejny.push(jQuery(this).html() );
});
var localities = [];
for (var i=0; i<localityTexts.length; i++) {
var locality = localityTexts[i];
if (localities.indexOf(locality) === -1) {
localities.push(locality);
}
}
localities.sort();
for (i = 0; i < localities.length;i++) {
jQuery('.item-page').append( '<div class="lokalita"><h2> ' + localities[i] + '</h2><div class="prodejny-v-lokalite"> </div></div>');
}
});
I don´t know how to campare Item´s location with that in section heading and then order it to their section. 我不知道如何在部分标题中将该项目的位置保留下来,然后将其订购到他们的部分。 Thank you much for any advice.
非常感谢您的任何建议。
As I can click your trouble, you should use objects instead of lots of Arrays. 当我可以解决您的麻烦时,您应该使用对象而不是大量的数组。 Be more functionality.
具有更多功能。
Something like this: 像这样:
var $page = $('.item-page');
$('.vcard').map(function (i, el) {
var $vcard = $(el);
return {
label: $.trim($vcard.find('.locality').text()),
order: i + 1 // Coz we start count from 0
};
}).sort(function (a, b) {
return a.label > b.label;
}).each(function (i, locality) {
$page.append('<div class="lokalita"><h2> ' + locality.label + '</h2><div class="prodejny-v-lokalite">'+ locality.order + '</div></div>');
});
And link to result: 并链接到结果:
http://jsfiddle.net/99oup27k/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.