[英]How to sort item using <td> class inside ListView?
I have a ListView
.我有一个
ListView
。 Inside that I am using <table>
as item template.在里面我使用
<table>
作为项目模板。 I want to sort the items using <td>
class name.我想使用
<td>
class 名称对项目进行排序。
How can I do that?我怎样才能做到这一点? And this should work on button click.
这应该适用于按钮点击。
<asp:ListView ID="lstvRCGroupSource" runat="server" ViewStateMode="Disabled">
<LayoutTemplate>
<ul id="list3" class="conn" style="width: 90%; height: 171px;">
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li class="add" id="l3">
<table id="tbl" style="width: 100%;">
<tr class="mytr" style="width: 100%;">
<td class="border1" style="width: 50%;"><%# Eval("code") %></td>
<td class="border2" style="width: 50%;"><%# Eval("regon_name") %></td>
</tr>
</table>
</li>
</ItemTemplate>
</asp:ListView>
function sortUnorderedList(ul, sortDescending) {
if (typeof ul == "string")
ul = document.getElementById(ul);
var lis = ul.getElementsByTagName("li");
var vals = [];
for (var i = 0, l = lis.length; i < l; i++)
vals.push(lis[i].innerHTML);
vals.sort();
if (sortDescending)
vals.reverse();
for (var i = 0, l = lis.length; i < l; i++)
lis[i].innerHTML = vals[i];
}
window.onload = function () {
var desc = false;
document.getElementById("stCodeDSC").onclick = function () {
sortUnorderedList("list3", desc);
desc = !desc;
return false;
}
}
I would use DataGrid or DataGridView.我会使用 DataGrid 或 DataGridView。 It has sorting mechanisms already built in.
它已经内置了排序机制。
Why not to use OnItemCommand:为什么不使用 OnItemCommand:
Keep LinkButton in the ListView's Table:将 LinkButton 保留在 ListView 的表中:
<table id="tbl" style="width: 100%;">
<tr class="mytr" style="width: 100%;">
<td class="border1" style="width: 50%;">
<asp:LinkButton ID="LinkButton1" runat="server"><%# Eval("code") %></asp:LinkButton>
</td>
<td class="border2" style="width: 50%;">
<asp:LinkButton ID="LinkButton2" runat="server"><%# Eval("regon_name") %></asp:LinkButton>
</td>
</tr>
</table>
Find LinkButton Click event in OnItemCommand:在 OnItemCommand 中找到 LinkButton Click 事件:
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName.Equals("Code"))
{
// sorting code
}
if (e.CommandName.Equals("RegionName"))
{
// sorting code
}
}
Take look at this jQuery sorting function .看看这个jQuery 排序 function 。 It should be pretty easy to implement with your code.
使用您的代码应该很容易实现。
Here's the function:这是 function:
jQuery.fn.sortElements = (function(){
var sort = [].sort;
return function(comparator, getSortable) {
getSortable = getSortable || function(){return this;};
var placements = this.map(function(){
var sortElement = getSortable.call(this),
parentNode = sortElement.parentNode,
// Since the element itself will change position, we have
// to have some way of storing its original position in
// the DOM. The easiest way is to have a 'flag' node:
nextSibling = parentNode.insertBefore(
document.createTextNode(''),
sortElement.nextSibling
);
return function() {
if (parentNode === this) {
throw new Error(
"You can't sort elements if any one is a descendant of another."
);
}
// Insert before flag:
parentNode.insertBefore(this, nextSibling);
// Remove flag:
parentNode.removeChild(nextSibling);
};
});
return sort.call(this, comparator).each(function(i){
placements[i].call(getSortable.call(this));
});
};
})();
And the implementation should look something like this:实现应该是这个样子:
$("#list3 li td").sortElements(function(a, b){
return $(a).attr("class") > $(b).attr("class") ? 1 : -1;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.