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

该博客为转载内容,转载自https://www.cnblogs.com/ocean2000/archive/2007/07/09/810799.html ,未提及具体信息技术相关关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       做项目碰到一个问题,需要动态的调整记录的排列顺序,并更新数据库相应的序号字段。开始并没考虑使用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

### 实现HTML表格中序号列的排序功能 要在HTML表格中实现序号列的排序功能,可以采用多种方法。以下是基于JavaScript和jQuery的技术方案。 #### 方法一:纯前端实现——通过JavaScript动态更新序号 可以通过监听表头点击事件来触发排序逻辑,重新计算序号值。以下是一个简单的示例: ```javascript document.addEventListener('DOMContentLoaded', function () { const table = document.querySelector('table'); const tbody = table.querySelector('tbody'); // 获取所有行 let rowsArray = Array.from(tbody.querySelectorAll('tr')); // 排序函数 function sortTable(order) { rowsArray.sort((rowA, rowB) => { const keyA = parseInt(rowA.cells[0].textContent); const keyB = parseInt(rowB.cells[0].textContent); if (order === 'asc') { return keyA - keyB; } else { return keyB - keyA; } }); // 清空tbody重新插入已排序的行 while (tbody.firstChild) { tbody.removeChild(tbody.firstChild); } rowsArray.forEach(row => tbody.appendChild(row)); // 更新序号 updateSerialNumbers(); } // 动态更新序号 function updateSerialNumbers() { Array.from(tbody.querySelectorAll('tr')).forEach((row, index) => { row.cells[0].textContent = index + 1; }); } // 绑定点击事件到表头 table.querySelector('th').addEventListener('click', () => { const currentOrder = table.getAttribute('data-order') || 'desc'; const newOrder = currentOrder === 'asc' ? 'desc' : 'asc'; sortTable(newOrder); table.setAttribute('data-order', newOrder); }); }); ``` 此代码实现了对`<td>`中的数值进行升序或降序排列的功能[^1]。每次排序后会调用`updateSerialNumbers()`函数以确保序号始终连续且正确。 --- #### 方法二:借助第三方库Layui实现更复杂的交互效果 如果希望使用成熟的框架简化开发流程,则可以选择Layui这样的工具包。然而需要注意的是,在某些情况下可能会遇到数据错乱的问题[^2]。为了避免此类情况发生,建议按照官方文档配置渲染器以及绑定回调处理程序。 下面展示了一个基本的例子: ```html <div id="demo"></div> <script> layui.use(['table'], function(){ var table = layui.table; // 执行一个表格实例 table.render({ elem: '#demo' ,height: 315 ,url:'/some/data/source.json' // 数据接口地址 ,cols: [[ // 表头定义 {field:'id', title:'ID', sort:true} ,{field:'name', title:'名称'} ]] ,page: true // 开启分页支持 }); // 处理自定义行为(比如手动调整顺序) table.on('sort(test)', function(obj){ console.log(obj.field); // 得知当前排序字段名 console.log(obj.type); // 得知当前排序方式:"asc" 或者 "desc" // 可在此处执行额外操作... }); }); </script> ``` 上述脚本片段展示了如何利用Layui加载远程JSON资源允许用户按需切换显示模式。同时它还提供了灵活扩展的可能性以便满足特定需求场景下的定制化要求。 --- #### 方法三:结合服务器端完成持久化的解决方案 当涉及到大量记录或者需要保存最终状态时,仅依靠客户端可能不够理想。此时可考虑引入后台服务配合数据库共同协作达成目标。例如.NET平台下常用ADO.NET组件访问关系型存储引擎MySQL/SQL Server等,通过DataView对象设置Sort属性来进行初步筛选再返回给前台展现层[^4]。 具体做法如下: 1. 构造合适的SQL语句提取原始资料集; 2. 创建对应的DataTable结构装载检索出来的结果集合; 3. 基于用户的实际选择修改视图规则从而影响呈现次序; 4. 将整理完毕的数据序列反馈至页面供进一步加工美化之用。 这种方法虽然相对复杂一些,但它能够很好地应对大规模发请求且具备较高的稳定性和可靠性特性。 --- ### 总结 无论是单纯依赖浏览器环境内的技术手段还是融合前后两端的优势互补策略都可以有效解决HTML表格内序号列无法正常排序这一难题。开发者应根据项目实际情况权衡利弊选取最适合自己团队能力水平的方式加以实施部署。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值