Javascript 使用.sort(函数(a,b)以数字降序列出已解析的xml数据
因此,基本上我有一个xml提要,它将房屋属性列为一个自定义的wordpress主题[xml提要托管在expertagent上],我一直在尝试以降序数字顺序输出结果,降序数字顺序由每个xml项目[在本例中为属性]中的价格值决定。我得到了按状态[待售、已售出等]显示物业的提要,但我似乎无法让这部分正常工作。我要么破坏提要,要么输入的代码根本不起作用 注意:我通常不会处理javescript或jquery方面的事情,所以我对这方面非常熟悉 以下是我的.js文件和我当前的尝试:Javascript 使用.sort(函数(a,b)以数字降序列出已解析的xml数据,javascript,jquery,xml,Javascript,Jquery,Xml,因此,基本上我有一个xml提要,它将房屋属性列为一个自定义的wordpress主题[xml提要托管在expertagent上],我一直在尝试以降序数字顺序输出结果,降序数字顺序由每个xml项目[在本例中为属性]中的价格值决定。我得到了按状态[待售、已售出等]显示物业的提要,但我似乎无法让这部分正常工作。我要么破坏提要,要么输入的代码根本不起作用 注意:我通常不会处理javescript或jquery方面的事情,所以我对这方面非常熟悉 以下是我的.js文件和我当前的尝试: jQuery(funct
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');
}
});
所以我试着让这些房产从最高的价格到最低的价格(但如果知道如何交换价格就好了)在priority.startsWith部分中。价格数据包含在price_文本和/或numeric_price元素中。在我的代码中,我尝试了这两种方法,但我认为numeric_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>
非常感谢您的帮助!以下是您需要的排序功能: 函数priceSorta,b{ 返回编号$b.find'numeric_price'。text-编号$a.find'numeric_price'。text; } 就像创建HTML一样,您需要找到价格,然后获取其文本,因为它是子节点,而不是属性。您还需要将其转换为数字,否则JavaScript会将它们作为字符串进行比较 您可以在现有代码中使用该函数,替换parseXml的第一行,如下所示: $$xml.findproperty.get.sortpriceSort.eachfunction{
首先获取所有属性,调用get将它们转换为非jQuery节点数组,调用数组上的sort,然后将结果传递回$以再次将其转换为jQuery集合。最后,调用每个属性以创建HTML部分。调用多个元素上的文本返回一个字符串,所有文本内容连接在一起,然后jQuery没有排序功能。好吧,这就解释了为什么它不做任何事情。我可以用什么来代替?太棒了,这对我来说几乎很有用,它确实按照我的意愿对属性排序,但它现在似乎没有显示我的else循环,所以它只按顺序显示ifpriority.startsWith'On Market'。这是代码@Moose,我从我的列表中删除了else部分举个简单的例子,因为我找不到这两个区块之间的区别。好吧,我只是想知道我做错了什么,包括你给我看的排除了else部分的内容。不,等等,忽略我lol。它没有排除它,我只是瞎了!
<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>