(javascript)调整记录的顺序并保存序号

转载博客相关内容
该博客为转载内容,转载自https://www.cnblogs.com/ocean2000/archive/2007/07/09/810799.html ,未提及具体信息技术相关关键信息。
       做项目碰到一个问题,需要动态的调整记录的排列顺序,并更新数据库相应的序号字段。开始并没考虑使用javascript来做,因为感觉自己对于js还是比较弱,没胆量用js来做,于是开始的时候是用的调整顺序的时候提交数据库改变序号来做,我也知道如果这样对于用户体验是个很不好的效果,但是先把这个能跑起来再说,很快这个就弄完了,我也开始思考如何将这个改为js来做,翻看了网上很多资料,终于给我找到小山的blog上面有个功能更多的datagrid的一个例子,参照这个例子我只选取了部分js代码,经过几次修改大体运行无误了。特记录下代码以供自己和看到自己blog的人使用,
   ///代码版权归ocean2000(ocean chou),请转载使用的时候注明!
  ///edit by ocean chou 
 
  1None.gif// JScript 文件
  2ExpandedBlockStart.gifContractedBlock.gif   function get_Element(the_ele){
  3InBlock.gif        var res;
  4InBlock.gif        if((the_ele.firstChild == null)||(the_ele.firstChild =='undefined')) return ;
  5InBlock.gif        res = the_ele.firstChild;
  6InBlock.gif        if((res.firstChild == null)||(res.firstChild=='undefined')) return;
  7InBlock.gif        res = res.firstChild;
  8InBlock.gif        if(res.type =='radio') return res;
  9InBlock.gif        return(null);
 10ExpandedBlockEnd.gif     }

 11None.gif    
 12ExpandedBlockStart.gifContractedBlock.gif     function change_row(line1,line2){
 13InBlock.gif      // debugger;
 14InBlock.gif       var the_table =document.getElementById('table_sequence');
 15ExpandedSubBlockStart.gifContractedSubBlock.gif       if((the_table == null)||(the_table =='undefined')){ alert('程序异常'); return;}
 16InBlock.gif        var row1 = the_table.rows[line1];
 17InBlock.gif        var row2 = the_table.rows[line2];
 18InBlock.gif        
 19InBlock.gif        row1.swapNode(row2);
 20InBlock.gif        
 21InBlock.gif        var radio = get_Element(row1);
 22InBlock.gif        if(radio != null) radio.checked = true;
 23ExpandedBlockEnd.gif     }

 24None.gif     
 25None.gif     function change_select(line1)
 26ExpandedBlockStart.gifContractedBlock.gif     {
 27InBlock.gif        var the_table =document.getElementById('table_sequence');
 28ExpandedSubBlockStart.gifContractedSubBlock.gif       if((the_table == null)||(the_table =='undefined')){ alert('程序异常'); return;}
 29InBlock.gif        var row1 = the_table.rows[line1];
 30InBlock.gif        
 31InBlock.gif       if((row1 == null)||(row1 == 'undefined')) return;
 32InBlock.gif        
 33InBlock.gif        var radio = get_Element(row1);
 34InBlock.gif        if(radio != null) radio.checked = true;
 35ExpandedBlockEnd.gif     }

 36None.gif     
 37None.gif     function leftselect()
 38ExpandedBlockStart.gifContractedBlock.gif     {
 39InBlock.gif        var i=0
 40InBlock.gif       var selectitem = document.getElementsByName('SelectItem');
 41InBlock.gif       flag = false;
 42InBlock.gif       //debugger;
 43InBlock.gif       if((selectitem ==null)||(selectitem == 'undefined')||(selectitem.length <=0))
 44InBlock.gif          return false;
 45InBlock.gif       for(i=0;i<selectitem.length;i++)
 46ExpandedSubBlockStart.gifContractedSubBlock.gif       {
 47InBlock.gif          if(selectitem[i].checked == true)
 48ExpandedSubBlockStart.gifContractedSubBlock.gif            {flag = true;cur_row =i+1;break;}
 49ExpandedSubBlockEnd.gif       }

 50InBlock.gif       
 51InBlock.gif       if(!flag)
 52ExpandedSubBlockStart.gifContractedSubBlock.gif          { alert('select none');return false;}
 53InBlock.gif       if(cur_row==null || cur_row<=1)return;
 54InBlock.gif       change_select(i);
 55ExpandedBlockEnd.gif     }

 56None.gif     
 57None.gif     function rightselect()
 58ExpandedBlockStart.gifContractedBlock.gif     {
 59InBlock.gif       var i=0
 60InBlock.gif       var selectitem = document.getElementsByName('SelectItem');
 61InBlock.gif       flag = false;
 62InBlock.gif       //debugger;
 63InBlock.gif       if((selectitem ==null)||(selectitem == 'undefined')||(selectitem.length <=0))
 64InBlock.gif          return false;
 65InBlock.gif       for(i=0;i<selectitem.length;i++)
 66ExpandedSubBlockStart.gifContractedSubBlock.gif       {
 67InBlock.gif          if(selectitem[i].checked == true)
 68ExpandedSubBlockStart.gifContractedSubBlock.gif            {flag = true;cur_row =i+1;break;}
 69ExpandedSubBlockEnd.gif       }

 70InBlock.gif       
 71InBlock.gif       if(!flag)
 72ExpandedSubBlockStart.gifContractedSubBlock.gif          { alert('select none');return false;}
 73InBlock.gif          
 74InBlock.gif       change_select(cur_row+1);
 75ExpandedBlockEnd.gif     }

 76None.gif
 77None.gif    function upRow()
 78ExpandedBlockStart.gifContractedBlock.gif    {
 79InBlock.gif       var i=0
 80InBlock.gif       var selectitem = document.getElementsByName('SelectItem');
 81InBlock.gif       flag = false;
 82InBlock.gif       //debugger;
 83InBlock.gif       if((selectitem ==null)||(selectitem == 'undefined')||(selectitem.length <=0))
 84InBlock.gif          return false;
 85InBlock.gif       for(i=0;i<selectitem.length;i++)
 86ExpandedSubBlockStart.gifContractedSubBlock.gif       {
 87InBlock.gif          if(selectitem[i].checked == true)
 88ExpandedSubBlockStart.gifContractedSubBlock.gif            {flag = true;cur_row =i+1;break;}
 89ExpandedSubBlockEnd.gif       }

 90InBlock.gif       
 91InBlock.gif       if(!flag)
 92ExpandedSubBlockStart.gifContractedSubBlock.gif          { alert('select none');return false;}
 93InBlock.gif          
 94InBlock.gif       event.cancelBubble=true;
 95InBlock.gif       
 96InBlock.gif       if(cur_row==null || cur_row<=1)return;
 97InBlock.gif       change_row(cur_row,--cur_row);
 98ExpandedBlockEnd.gif    }

 99None.gif    
100None.gif    function downRow()
101ExpandedBlockStart.gifContractedBlock.gif    {
102InBlock.gif       var selectitem = document.getElementsByName('SelectItem');
103InBlock.gif       var i;
104InBlock.gif       flag = false;
105InBlock.gif       //debugger;
106InBlock.gif       var the_table =document.getElementById('table_sequence');
107InBlock.gif       if((selectitem ==null)||(selectitem == 'undefined')||(selectitem.length <=0))
108InBlock.gif          return false;
109InBlock.gif       for(i=0;i<selectitem.length;i++)
110ExpandedSubBlockStart.gifContractedSubBlock.gif       {
111InBlock.gif          if(selectitem[i].checked == true)
112ExpandedSubBlockStart.gifContractedSubBlock.gif            {flag = true; cur_row=i+1;break;}
113ExpandedSubBlockEnd.gif       }

114InBlock.gif       
115InBlock.gif       if(!flag)
116ExpandedSubBlockStart.gifContractedSubBlock.gif          { alert('select none');return false;}
117InBlock.gif       event.cancelBubble=true;
118InBlock.gif       if(cur_row==null || cur_row==the_table.rows.length-1 || cur_row==0)return;
119InBlock.gif       change_row(cur_row,++cur_row);
120InBlock.gif       
121ExpandedBlockEnd.gif    }

122None.gif    
123None.gif    function Check()
124ExpandedBlockStart.gifContractedBlock.gif    {
125InBlock.gif        //debugger;
126InBlock.gif        var selectitem = document.getElementsByName('SelectItem');
127InBlock.gif        if((selectitem ==null)||(selectitem == 'undefined')||(selectitem.length <=0))
128InBlock.gif          return false;
129InBlock.gif        var hiddenID = document.getElementById('hiddenID');
130InBlock.gif        hiddenID.value='';
131InBlock.gif        
132InBlock.gif       for(i=0;i<selectitem.length;i++)
133ExpandedSubBlockStart.gifContractedSubBlock.gif       {
134InBlock.gif          if(hiddenID.value =='')
135InBlock.gif             hiddenID.value = selectitem[i].value;
136InBlock.gif          else
137InBlock.gif             hiddenID.value = hiddenID.value + '`'+selectitem[i].value;
138ExpandedSubBlockEnd.gif       }
 
139InBlock.gif        
140ExpandedBlockEnd.gif    }

141None.gif
aspx页面的head部分加入
 1None.gif <script type = "Text/javascript">
 2None.gif     var cur_row    = null;
 3None.gif     var flag = false;
 4ExpandedBlockStart.gifContractedBlock.gif     document.onkeydown=function(){
 5InBlock.gif    // debugger;
 6InBlock.gif     var s = event.keyCode;
 7InBlock.gif     if(s == 37)
 8InBlock.gif       leftselect();
 9InBlock.gif     if(s == 38)
10InBlock.gif       upRow();
11InBlock.gif     if(s == 39)
12InBlock.gif       rightselect();
13InBlock.gif     if(s ==40)
14InBlock.gif       downRow();
15ExpandedBlockEnd.gif     }

16None.gif    </script>
然后后台通过读取hidden变量的值根据其顺序来得改变记录的顺序同时更新数据库相应字段。

转载于:https://www.cnblogs.com/ocean2000/archive/2007/07/09/810799.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值