jquery控制listbox中項(xiàng)的移動(dòng)并排序的實(shí)現(xiàn)代碼
更新時(shí)間:2010年09月28日 08:49:18 作者:
listbox中項(xiàng)的移動(dòng)并排序的jquery實(shí)現(xiàn)代碼,使用jquery與listbox的朋友可以參考下。
首先是html代碼,頁(yè)面上放2個(gè)listbox控件和2個(gè)按鈕用于移動(dòng)項(xiàng)目
<table border="0">
<tr>
<td width="156">全部水果:</td>
<td width="142"> </td>
<td width="482">我挑選的:</td>
</tr>
<tr>
<td rowspan="2"><asp:ListBox SelectionMode="Multiple" ID="listall" Rows="12" Width="156" runat="server"></asp:ListBox></td>
<td height="41" align="center">
<input type="button" id="btnleftmove" value=">>>" onclick="move('listall','listmy');"/><br /><br />
<input type="button" id="btnrighttmove" value="<<<" onclick="move('listmy','listall');"/>
</td>
<td rowspan="2"><asp:ListBox SelectionMode="Multiple" ID="listmy" Rows="12" Width="156" runat="server"></asp:ListBox></td>
</tr>
</table>
下面是在.cs文件中綁定一些數(shù)據(jù)
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
ArrayList list=DataArray();
for (int i = 0; i < list.Count; i++)
{
listall.Items.Add(list[i].ToString());
listall.Items[i].Attributes["tag"] = i.ToString(); //用tag記錄排序字段
}
}
private ArrayList DataArray()
{
//用到的一些數(shù)據(jù),這里已默認(rèn)按第一個(gè)字的拼音排序
ArrayList list = new ArrayList();
list.Add("草莓");
list.Add("梨");
list.Add("桔子");
list.Add("芒果");
list.Add("蘋(píng)果");
list.Add("香蕉");
return list;
}
}
在實(shí)際使用時(shí)可根據(jù)數(shù)據(jù)庫(kù)中的字段排序
下面是jquery的代碼:
//移動(dòng)用戶選擇的角色
//setname:要移出數(shù)據(jù)的列表名稱 getname:要移入數(shù)據(jù)的列表名稱
function move(setname,getname)
{
var size=$("#"+setname+" option").size();
var selsize=$("#"+setname+" option:selected").size();
if(size>0&&selsize>0)
{
$.each($("#"+setname+" option:selected"), function(id,own){
var text=$(own).text();
var tag=$(own).attr("tag");
$("#"+getname).prepend("<option tag=\""+tag+"\">"+text+"</option>");
$(own).remove();
$("#"+setname+"").children("option:first").attr("selected",true);
});
}
//重新排序
$.each($("#"+getname+" option"), function(id,own){
orderrole(getname);
});
}
//按首字母排序角色列表
function orderrole(listname)
{
var size=$("#"+listname+" option").size();
var one=$("#"+listname+" option:first-child");
if(size>0)
{
var text=$(one).text();
var tag=parseInt($(one).attr("tag"));
//循環(huán)列表中第一項(xiàng)值下所有元素
$.each($(one).nextAll(), function(id,own){
var nextag=parseInt($(own).attr("tag"));
if(tag>nextag)
{
$(one).remove();
$(own).after("<option tag=\""+tag+"\">"+text+"</option>");
one=$(own).next();
}
});
}
}
復(fù)制代碼 代碼如下:
<table border="0">
<tr>
<td width="156">全部水果:</td>
<td width="142"> </td>
<td width="482">我挑選的:</td>
</tr>
<tr>
<td rowspan="2"><asp:ListBox SelectionMode="Multiple" ID="listall" Rows="12" Width="156" runat="server"></asp:ListBox></td>
<td height="41" align="center">
<input type="button" id="btnleftmove" value=">>>" onclick="move('listall','listmy');"/><br /><br />
<input type="button" id="btnrighttmove" value="<<<" onclick="move('listmy','listall');"/>
</td>
<td rowspan="2"><asp:ListBox SelectionMode="Multiple" ID="listmy" Rows="12" Width="156" runat="server"></asp:ListBox></td>
</tr>
</table>
下面是在.cs文件中綁定一些數(shù)據(jù)
復(fù)制代碼 代碼如下:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
ArrayList list=DataArray();
for (int i = 0; i < list.Count; i++)
{
listall.Items.Add(list[i].ToString());
listall.Items[i].Attributes["tag"] = i.ToString(); //用tag記錄排序字段
}
}
private ArrayList DataArray()
{
//用到的一些數(shù)據(jù),這里已默認(rèn)按第一個(gè)字的拼音排序
ArrayList list = new ArrayList();
list.Add("草莓");
list.Add("梨");
list.Add("桔子");
list.Add("芒果");
list.Add("蘋(píng)果");
list.Add("香蕉");
return list;
}
}
在實(shí)際使用時(shí)可根據(jù)數(shù)據(jù)庫(kù)中的字段排序
下面是jquery的代碼:
復(fù)制代碼 代碼如下:
//移動(dòng)用戶選擇的角色
//setname:要移出數(shù)據(jù)的列表名稱 getname:要移入數(shù)據(jù)的列表名稱
function move(setname,getname)
{
var size=$("#"+setname+" option").size();
var selsize=$("#"+setname+" option:selected").size();
if(size>0&&selsize>0)
{
$.each($("#"+setname+" option:selected"), function(id,own){
var text=$(own).text();
var tag=$(own).attr("tag");
$("#"+getname).prepend("<option tag=\""+tag+"\">"+text+"</option>");
$(own).remove();
$("#"+setname+"").children("option:first").attr("selected",true);
});
}
//重新排序
$.each($("#"+getname+" option"), function(id,own){
orderrole(getname);
});
}
//按首字母排序角色列表
function orderrole(listname)
{
var size=$("#"+listname+" option").size();
var one=$("#"+listname+" option:first-child");
if(size>0)
{
var text=$(one).text();
var tag=parseInt($(one).attr("tag"));
//循環(huán)列表中第一項(xiàng)值下所有元素
$.each($(one).nextAll(), function(id,own){
var nextag=parseInt($(own).attr("tag"));
if(tag>nextag)
{
$(one).remove();
$(own).after("<option tag=\""+tag+"\">"+text+"</option>");
one=$(own).next();
}
});
}
}
您可能感興趣的文章:
- jquery ui sortable拖拽后保存位置
- 通過(guò)jquery-ui中的sortable來(lái)實(shí)現(xiàn)拖拽排序的簡(jiǎn)單實(shí)例
- jquery sortable的拖動(dòng)方法示例詳解
- jquery實(shí)現(xiàn)的鼠標(biāo)拖動(dòng)排序Li或Table
- jquery控制listbox中項(xiàng)的移動(dòng)并排序
- jquery對(duì)元素拖動(dòng)排序示例
- 基于JQuery的列表拖動(dòng)排序?qū)崿F(xiàn)代碼
- jQuery拖動(dòng)元素并對(duì)元素進(jìn)行重新排序
- Jquery實(shí)現(xiàn)上下移動(dòng)和排序代碼
- jQuery-ui插件sortable實(shí)現(xiàn)自由拖動(dòng)排序
相關(guān)文章
jquery實(shí)現(xiàn)下拉菜單的手風(fēng)琴效果
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)下拉菜單的手風(fēng)琴效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07jquery 回調(diào)操作實(shí)例分析【回調(diào)成功與回調(diào)失敗的情況】
這篇文章主要介紹了jquery 回調(diào)操作,結(jié)合實(shí)例形式分析了jQuery回調(diào)成功與回調(diào)失敗的情況及相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-09-09擴(kuò)展jQuery 鍵盤(pán)事件的幾個(gè)基本方法
擴(kuò)展jQuery 鍵盤(pán)事件的幾個(gè)基本方法(練習(xí)jQuery插件擴(kuò)展)2009-10-10jQuery實(shí)現(xiàn)拖拽可編輯模塊功能代碼
這篇文章主要介紹了jQuery實(shí)現(xiàn)拖拽可編輯模塊功能代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-01-01jQuery中[attribute^=value]選擇器用法實(shí)例
這篇文章主要介紹了jQuery中[attribute^=value]選擇器用法,實(shí)例分析了[attribute^=value]選擇器的功能、定義及匹配以某些值開(kāi)始的元素的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12基于jquery用于查詢操作的實(shí)現(xiàn)代碼
通過(guò)javascript得到用戶操作改變url參數(shù)從而實(shí)現(xiàn)某些功能,如查詢(具體的查詢由服務(wù)器端代碼得到url中的參數(shù)組成查詢語(yǔ)句實(shí)現(xiàn))。2010-05-05