简体   繁体   English

单击时按字母顺序对div排序

[英]Sort div alphabetically when clicked

I have a grid which has five columns. 我有一个包含五列的网格。 Is there a way to sort the columns when a the particular heading is clicked? 单击特定标题时,是否可以对列进行排序? For example, when name is clicked, the columns are sorted in ascending order and vice versa. 例如,单击名称时,列将按升序排序,反之亦然。 I am not using tables. 我没有使用表格。 All the data is displayed using divs` 所有数据使用divs显示

 <div class="Table" id="tab"> <div class="Title"> <p>Financial data</p> </div> <div class="Heading"> <div class="Cell"> <p>Name</p> </div> <div class="Cell"> <p>Plan</p> </div> <div class="Cell"> <p>Forecast</p> </div> <div class="Cell"> <p>Best Case</p> </div> <div class="Cell"> <p>Commit</p> </div> </div> <!-- Structure to display the JSON Data--> <div class="Row"> <div class="Cell"> <p id="country_name1"></p> </div> <div class="Cell"> <p id="plan_1"></p> </div> <div class="Cell"> <p id="forecast_1"></p> </div> <div id="col3" class="Cell"> <p id="best_case1"></p> <p class="ftype" id="best_case1.1"></p> </div> <div id="col4" class="Cell"> <p id="commit1"></p> <p class="ftype" id="commit1.1"></p> </div> </div> 
Can this be done using javascript or jquery? 可以使用javascript或jquery完成吗? 在此处输入图片说明

It can be done with different plugins, for example: 可以使用不同的插件来完成,例如:

TinySort (rewritten in pure Javascript, compatible with divs) http://tinysort.sjeiti.com TinySort (用纯Javascript重写,与div兼容) http://tinysort.sjeiti.com

Datatables (jQuery) https://datatables.net/examples/basic_init/table_sorting.html 数据表 (jQuery) https://datatables.net/examples/basic_init/table_sorting.html

Tablesorter (jQuery) http://tablesorter.com/docs/ Tablesorter (jQuery) http://tablesorter.com/docs/

Other examples on Tympanus https://tympanus.net/codrops/2009/10/03/33-javascript-solutions-for-sorting-tables/ 鼓膜上的其他示例https://tympanus.net/codrops/2009/10/03/33-javascript-solutions-for-sorting-tables/

You can keep it in an array and then you can use sort, if you put all the names with the same class you can take it like 您可以将其保存在一个数组中,然后可以使用sort,如果将所有名称放在同一类中,则可以将其取为

var x = document.getElementsByClassName("Name").innerHTML;
x.sort(function(a, b) { return b - a });

for (var i = 0; i < x.length; i++) {
    document.getElementsByClassName("Name").innerHTML = x[i] ;
}

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

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