jquery控制listbox中項(xiàng)的移動并排序的實(shí)現(xiàn)代碼
更新時(shí)間:2010年09月28日 08:49:18 作者:
listbox中項(xiàng)的移動并排序的jquery實(shí)現(xiàn)代碼,使用jquery與listbox的朋友可以參考下。
首先是html代碼,頁面上放2個(gè)listbox控件和2個(gè)按鈕用于移動項(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("蘋果");
list.Add("香蕉");
return list;
}
}
在實(shí)際使用時(shí)可根據(jù)數(shù)據(jù)庫中的字段排序
下面是jquery的代碼:
//移動用戶選擇的角色
//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("蘋果");
list.Add("香蕉");
return list;
}
}
在實(shí)際使用時(shí)可根據(jù)數(shù)據(jù)庫中的字段排序
下面是jquery的代碼:
復(fù)制代碼 代碼如下:
//移動用戶選擇的角色
//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拖拽后保存位置
- 通過jquery-ui中的sortable來實(shí)現(xiàn)拖拽排序的簡單實(shí)例
- jquery sortable的拖動方法示例詳解
- jquery實(shí)現(xiàn)的鼠標(biāo)拖動排序Li或Table
- jquery控制listbox中項(xiàng)的移動并排序
- jquery對元素拖動排序示例
- 基于JQuery的列表拖動排序?qū)崿F(xiàn)代碼
- jQuery拖動元素并對元素進(jìn)行重新排序
- Jquery實(shí)現(xiàn)上下移動和排序代碼
- jQuery-ui插件sortable實(shí)現(xiàn)自由拖動排序
相關(guān)文章
jquery處理頁面彈出層查詢數(shù)據(jù)等待操作實(shí)例
這篇文章主要介紹了jquery處理頁面彈出層查詢數(shù)據(jù)等待操作,實(shí)例分析了jquery實(shí)現(xiàn)等待效果的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
JavaScript和JQuery實(shí)用代碼片段(一)
JavaScript和JQuery實(shí)用代碼片段,喜歡學(xué)習(xí)jquery的朋友可以參考下。2010-04-04
基于JQuery的6個(gè)Tab選項(xiàng)卡插件
今天在修整博客側(cè)欄信息時(shí),利用到了Tab選項(xiàng)卡方式,因?yàn)閆Blog封裝的是JQuery庫,所以很自然地就想到了IdTabs。2010-09-09
jQuery實(shí)現(xiàn)轉(zhuǎn)動隨機(jī)數(shù)抽獎(jiǎng)效果的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)轉(zhuǎn)動隨機(jī)數(shù)抽獎(jiǎng)效果的方法,涉及jQuery操作隨機(jī)數(shù)及頁面元素的相關(guān)技巧,需要的朋友可以參考下2015-05-05
jQuery實(shí)現(xiàn)百葉窗焦點(diǎn)圖動畫效果代碼分享(附源碼下載)
這篇文章主要介紹了jQuery實(shí)現(xiàn)百葉窗焦點(diǎn)圖動畫效果代碼分享(附源碼下載)的相關(guān)資料,需要的朋友可以參考下2016-03-03
Easyui 去除jquery-easui tab頁div自帶滾動條的方法
這篇文章主要介紹了Easyui 去除jquery-easui tab頁div自帶滾動條的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05
基于jQuery實(shí)現(xiàn)表格數(shù)據(jù)的動態(tài)添加與統(tǒng)計(jì)的代碼
使用jQuery可以大大減輕工作量,在實(shí)際開發(fā)中,使用了jQuery的clone(true)函數(shù),該函數(shù)可以創(chuàng)建一個(gè)jQury對象的副本,并且參數(shù)為true時(shí),可以復(fù)制該元素的所有事件處理函數(shù)。2011-01-01
cnblogs TagCloud基于jquery的實(shí)現(xiàn)代碼
自創(chuàng)"山寨版"的"博客園"TagCloud!...2010-06-06

