如何在JQuery中按日期排序xml数据 [英] How to sort xml data by Date in JQuery

查看:122
本文介绍了如何在JQuery中按日期排序xml数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此应用程序正在从RSS提要中提取信息,我需要按日期排序文章。 xml数据包含一个'Date'元素,以ddmmyy格式列出日期。我的问题是:是否有可以在jQuery中实现的排序函数来排序信息并以正确的日期顺序显示? (日期顺序应该是第一个显示的条目是最远的日期,例如:01/20/14 then 01/07/2014 then 12/22/13 etc ...
这是我到目前为止:

This application is extracting information from an RSS feed and I need to sort the articles by date. The xml data contains a 'Date' element which lists the date in ddmmyy format. My question: is there a sort function I can implement in jQuery to sort the information and display it in the correct date order? (Date order should be the first entry to display is the furthest date out,example: 01/20/14 then 01/07/2014 then 12/22/13 etc... Here is what I have so far:

<script type="text/javascript">
 $(document).ready(function() {

        $('input[type=radio]').click(function() {
            var id = this.id;
            if(id == 'radio-bio') { var categoryURL = '/BayAreaTech/wp-rss2.php?cat=15';}
            else if (id == 'radio-com'){ var categoryURL = '/BayAreaTech/wp-rss2.php?cat=13';}
            else if (id == 'radio-eleP'){ var categoryURL = '/BayAreaTech/wp-rss2.php?cat=9';}
            else if (id == 'radio-eleD'){ var categoryURL = '/BayAreaTech/wp-rss2.php?cat=10';}
            else if (id == 'radio-nano'){ var categoryURL = '/BayAreaTech/wp-rss2.php?cat=16';}
            else if (id == 'radio-opt'){ var categoryURL = '/BayAreaTech/wp-rss2.php?cat=12';}
            else if (id == 'radio-semi'){ var categoryURL = '/BayAreaTech/wp-rss2.php?cat=11';}
            else { var categoryURL = '/BayAreaTech/wp-rss2.php?cat=1';}

            $('#feedContainer').empty();
                        $.ajax({
                        type: 'GET',
                        url: categoryURL,
                        dataType: 'xml',
                        success: function (xml) {
                            $(xml).find("item").each(function () {
                                  var title = $(this).find("title").text();
                                  var date = $(this).find("Date").text();
                                  var region = date.substr(6);
                                        if (region.length < 3) { region = "ALL"; }  
                                  var description = $(this).find("description").text();
                                  var descriptdisplay = description.substr(0, description.indexOf(",")+6); //Parsed DATE from description
                                        if (descriptdisplay.length > 35) { descriptdisplay = "See event for details"; }
                                    //var locationdisplay = description.substr(description.indexOf(",")+6,4); //Parsed the location from description
                                  var category = $(this).find("category").text();
                                  var linkUrl = $(this).find("link").text();
                                  var displaytitle = "<a href='" + linkUrl + "' target='_blank'>" + title + "</a>"  
                                  $('#feedContainer').append('<h3>'+displaytitle+'</h3><p>'+"Event Date: "+descriptdisplay+'</p><p>'+"Location: "+region+'</p');

                            });
                        }
                    });

        });
 });
 </script>


推荐答案

建议您首先将xml解析成数组可以分解的对象,在解析为html

Would suggest you first parse the xml into an array of objects that can be sorted, prior to parsing into html

success: function (xml) {
    var data=[];
    $(xml).find("item").each(function () {
      var dateText= $(this).find("Date").text()

      var item={
           title: $(this).find("title").text(),
           dateText =dateText,
           date : new Date( dateText),
           /* other properties*/
        }  
        /* push object to array*/
        data.push( item);

    });

    /* sort data*/
    data.sort(function(a,b){
         return a.date > b.date;
    });

     /* now parse to html */    
      $.each(data, function(index, item){


      })

这篇关于如何在JQuery中按日期排序xml数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆