因此,基本上,我有一个XML提要,它将住房属性列出到自定义的wordpress主题中(该XML提要托管在Expertagent上),并且我一直试图将结果以降序输出,该降序由价格内的价格决定。每个xml“项目” [在本例中为“属性”]。我已经通过提要按其状态(出售,出售等)显示属性,但我似乎无法使这部分起作用。我要么中断了提要,要么我输入的代码根本没有任何作用!
注意:我通常不是那种处理javescript或jquery方面的人,所以我是新手。
这是我的.js文件和我当前的尝试:
jQuery(function( $ ){
$(document).ready(function() {
$.ajax({
type: "GET",
url: "properties2.xml",
dataType: "xml",
success: parseXml
});
});
function parseXml(xml) {
$(xml).find("property").each(function() {
var priority = $(this).find("priority").text();
if(priority.startsWith('On Market')) {
$("#xmlmain").append("<div class='xmlwrapper'><div class='xmlleft'><img src='"+$(this).find('[name="Photo 1"]').text()+"'/></div><div class='xmlright'><h2>"+$(this).find("advert_heading").text()+"</h2><p class='price'>"+$(this).find
("price_text").text()+"</p><p class='priority'>"+$(this).find("priority").text()+"</p><p>"+$(this).find("main_advert").text()+"</p><a href='"+$(this).find("web_link").text()+"' target='_blank'>VIEW > </a></div></div>");
}
else {
if(priority.startsWith('Under Offer')) {
$("#xmlmain").append("<div class='xmlwrapper'><div class='xmlleft'><img src='"+$(this).find('[name="Photo 1"]').text()+"'/></div><div class='xmlright'><h2>"+$(this).find("advert_heading").text()+"</h2><p class='price'>"+$(this).find
("price_text").text()+"</p><p class='priority'>"+$(this).find("priority").text()+"</p><p>"+$(this).find("main_advert").text()+"</p><a href='"+$(this).find("web_link").text()+"' target='_blank'>VIEW > </a></div></div>");
}
}
});
$('#xmlmain').find('numeric_price').text().sort(function (a, b) {
return $(a).attr('numeric_price') - $(b).attr('numeric_price');
})
.appendTo('#xmlmain');
}
});
很明显,下面的部分是我用来尝试对其进行排序的部分,但很明显,我要么犯了一些菜鸟错误,要么我走错了方向,因为它实际上什么也不做!
$('#xmlmain').find('numeric_price').text().sort(function (a, b) {
return $(a).attr('numeric_price') - $(b).attr('numeric_price');
})
.appendTo('#xmlmain');
因此,我试图在其priority.startsWith部分中让这些属性从最高价到最低价列出(但很高兴知道如何交换价格)。价格数据包含在“ price_text”和/或“ numeric_price”元素中。我已经使用了我拥有的代码,但我都尝试过,但是我假设使用numerical_price是更好的选择,因为它不包含数字数据。
这也是xml提要的示例:
<properties>
<property reference="MR139">
<instructedDate>06/08/2018 17:07:05</instructedDate>
<price_text>£600,000</price_text>
<numeric_price>600000.0000</numeric_price>
<priority>On Market</priority>
<advert_heading>house for sale</advert_heading>
<main_advert>some text about the property</main_advert>
<web_link>www.example.com</web_link>
<property_style>Detached</property_style>
<property_reference>111111</property_reference>
<newHome>NO</newHome>
<noChain>NO</noChain>
<furnished>Unknown</furnished>
<currency>GBP</currency>
<featuredProperty>NO</featuredProperty>
<pictures>
<picture name="Photo 1" lastchanged="2018-08-06T15:44:48.5800534Z">
<filename>example.jpg</filename>
</picture>
</pictures>
</property>
</properties>
任何帮助将不胜感激!
答案 0 :(得分:1)
这是您需要的排序功能:
function priceSort(a, b) {
return Number($(b).find('numeric_price').text()) - Number($(a).find('numeric_price').text());
}
就像创建HTML一样,您需要find()
的价格,然后获取其text()
,因为它是一个子节点,而不是属性。您还需要将其转换为数字,否则JavaScript会将它们作为字符串进行比较。
您可以在现有代码中使用该函数,像这样替换parseXml()
的第一行:
$($(xml).find("property").get().sort(priceSort)).each(function() {
这首先获取所有属性,调用get()
将它们转换为非jQuery节点数组,在数组上调用sort()
,然后将结果传递回$()
进行转换再次将其放入jQuery集合。最后,调用each()
来创建HTML部分。