简体   繁体   English

按字母顺序/按日期排序多列列表,而不使用Dropbox已完成的HTML / javascript / jQuery中的表

[英]Sort multi-column list alphabetically/by date without using a table in HTML/javascript/jQuery as Dropbox has done

I'd like to sort multi-column list without using a table. 我想在不使用表的情况下对多列列表进行排序。 I know of solutions for single column lists but I can't seem to find one that sorts on multiple Ul/columns while keeping the list/row items together. 我知道单列列表的解决方案,但我似乎找不到一个在多个UI /列上排序,同时保持列表/行项目在一起。

Ultimately I'd like to make something similar to what dropbox has done for displaying files. 最后,我想做一些类似于Dropbox为显示文件所做的事情。 Yes, Dropbox doesn't use a sortable table. 是的,Dropbox不使用可排序表。 It's a list instead of tr's with a bunch of divs instead of td's. 这是一个列表,而不是tr与一堆div而不是td的。

I don't want to use a table because tables drive me crazy for styling purposes. 我不想使用表格,因为表格会让我为造型目的而疯狂。 Also, I don't want to use a plugin (like tablesorter.js) because javascript has a built in sort() function so it can't be that many lines of code. 另外,我不想使用插件(如tablesorter.js),因为javascript有一个内置的sort()函数,所以它不能是那么多行代码。

Anyway, here is a nice single column demonstration taken from another SO question, and the original SO reference . 无论如何,这是一个很好的单列演示,取自另一个SO问题,以及原始的SO参考

Basically I'd like to do what's in the fiddle but be able to sort by names, and for example addresses, here's a fiddle building upon this first fiddle where you can see where i'd like to go with this http://jsfiddle.net/trpeters1/De8Ku/955/ . 基本上我想做小提琴里的东西但是能够按名字排序,例如地址,这里是一个小提琴,建立在这个第一个小提琴上,你可以看到我想去哪里用这个http:// jsfiddle .net / trpeters1 / De8Ku / 955 / As you'll notice, this fiddle can't sort based on the address which is what I'd like find a solution for. 正如您将注意到的,这个小提琴无法根据我想要找到解决方案的地址进行排序。

UPDATE With many thanks to eterps (see below) now the fiddle can sort by multiple columns. 更新非常感谢eterps(见下文),现在小提琴可以按多列排序。 I'd now like to next include sorting by date. 我现在想接下来包括按日期排序。 Here is a fiddle which makes an attempt at this but doesn't quite work. 这是一个小提琴,试图尝试,但不是很有效。 You'll notice I tried to convert the innerHTML to a Date() object but this didn't solve this problem. 你会注意到我试图将innerHTML转换为Date()对象,但这并没有解决这个问题。 http://jsfiddle.net/trpeters1/De8Ku/983/ http://jsfiddle.net/trpeters1/De8Ku/983/

In this fiddle, can someone help explain why the sorting of the dates is still just alpha/numerical and not by date? 在这个小提琴中,有人可以帮助解释为什么日期的排序仍然只是字母/数字而不是日期?

Using the code you provided in your jsFiddle examples, you can add some identifying information to your span tags to differentiate the text into columns. 使用您在jsFiddle示例中提供的代码,您可以向span标记添加一些标识信息,以将文本区分为列。 Then you can use that information to do your sort. 然后,您可以使用该信息进行排序。 I altered the jsFiddle example to use a sorting function that will look at whichever column is specified. 我更改了jsFiddle示例以使用排序函数来查看指定的列。 I'm sure there are more elegant ways of doing this, but it should get you started. 我确信有更优雅的方法可以做到这一点,但它应该让你开始。

http://jsfiddle.net/De8Ku/976/ http://jsfiddle.net/De8Ku/976/

Here is the code in case the jsFiddle isn't working or if someone want to edit this answer: 这是代码,以防jsFiddle不工作或有人想编辑这个答案:

HTML: HTML:

<input type="button" id="test" value="sort by names"/>
<input type="button" id="test1" value="sort by address"/>

<ul id="list">
   <li ><span class="name">peter</span><span class="address"> 812 jones st.</span></li>
   <li><span class="name">zack</span><span class="address"> 512 jones st.</span></li>
   <li><span class="name">alex</span><span class="address"> 712 jones st.</span></li>
   <li><span class="name">sue</span><span class="address"> 112 jones st.</span></li>
   <li><span class="name">jenny</span><span class="address"> 912 jones st.</span></li>
</ul>

JS: JS:

function sortUnorderedList(ul, sortDescending, sortClass) {
    if (typeof ul == "string") {
        ul = document.getElementById(ul);
    }

    var lis = ul.getElementsByTagName("LI");
    var vals = [];

    // Instead of filling an array with list items, 
    // create an array of objects that contain the list item itself, 
    // and the value from the sort-by column
    for (var i = 0, l = lis.length; i < l; i++) {
        vals.push({
            sortFieldVal: lis[i].getElementsByClassName(sortClass)[0].innerText,
            val: lis[i].innerHTML
        });
    }

    // Use a sort function to compare string values of sortFieldVal for each item
    vals.sort(function(a, b) {
        var nameA=a.sortFieldVal.toLowerCase(), nameB=b.sortFieldVal.toLowerCase()
        if (nameA < nameB) //sort string ascending
        return -1 
        if (nameA > nameB)
        return 1
        return 0 //default return value (no sorting)
    });

    if (sortDescending) vals.reverse();

    for (var i = 0, l = lis.length; i < l; i++)
    lis[i].innerHTML = vals[i].val;
}

window.onload = function() {
    var desc = false;
    document.getElementById("test").onclick = function() {
        sortUnorderedList("list", desc, "name");
        desc = !desc;
        return false;
    }

    document.getElementById("test1").onclick = function() {
        sortUnorderedList("list", desc, "address");
        desc = !desc;
        return false;
    }
}​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM