您好,欢迎访问一九零五行业门户网

jquery操作表格实例详解(附代码)

这次给大家带来jquery操作表格实例详解(附代码),jquery操作表格的注意事项有哪些,下面就是实战案例,一起来看一下。
最近做东西需要对表格进行操作,用到的动作包括:添加一行数据、删除一行数据、上下移动数据,网上找了很多,但是不能完全满足我的需求,自己琢磨了下,搞了个这个东东
代码很简单,在附件中,各位可以下载后直接运行.
<script type="text/ javascript " language="javascript"> $(function() { jquery.fn.alternaterowcolors = function() { //做成插件的形式 $('tbody tr:odd', this).removeclass('even').addclass('odd'); //隔行变色 奇数行 $('tbody tr:even', this).removeclass('odd').addclass('even'); //隔行变色 偶数行 return this; }; $('table.mytable').each(function() { var $table = $(this); //将table存储为一个jquery对象 $table.alternaterowcolors($table); //在排序时隔行变色 $('th', $table).each(function(column) { var findsortkey; if ($(this).is('.sort-alpha')) { //按字母排序 findsortkey = function($cell) { return $cell.find('sort-key').text().touppercase() + '' + $cell.text().touppercase(); }; } else if ($(this).is('.sort-numeric')) { //按数字排序 findsortkey = function($cell) { var key = parsefloat($cell.text().replace(/^[^\d.]*/, '')); return isnan(key) ? 0 : key; }; } else if ($(this).is('.sort-date')) { //按日期排序 findsortkey = function($cell) { return date.parse('1 ' + $cell.text()); }; } if (findsortkey) { $(this).addclass('clickable').hover(function() { $(this).addclass('hover'); }, function() { $(this).removeclass('hover'); }).click(function() { //反向排序状态声明 var newdirection = 1; if ($(this).is('.sorted-asc')) { newdirection = -1; } var rows = $table.find('tbody>tr').get(); //将数据行转换为数组 $.each(rows, function(index, row) { row.sortkey = findsortkey($(row).children('td').eq(column)); }); rows.sort(function(a, b) { if (a.sortkey < b.sortkey) return -newdirection; if (a.sortkey > b.sortkey) return newdirection; return 0; }); $.each(rows, function(index, row) { $table.children('tbody').append(row); row.sortkey = null; }); $table.find('th').removeclass('sorted-asc').removeclass('sorted-desc'); var $sorthead = $table.find('th').filter(':nth-child(' + (column + 1) + ')'); //实现反向排序 if (newdirection == 1) { $sorthead.addclass('sorted-asc'); } else { $sorthead.addclass('sorted-desc'); } //调用隔行变色的函数 $table.alternaterowcolors($table); //移除已排序的列的样式,添加样式到当前列 $table.find('td').removeclass('sorted').filter(':nth-child(' + (column + 1) + ')').addclass('sorted'); $table.trigger('repaginate'); }); } }); }); }); //分页 $(function() { $('table.paginated').each(function() { var currentpage = 0; var numperpage = 10; var $table = $(this); $table.bind('repaginate', function() { $table.find('tbody tr').hide().slice(currentpage * numperpage, (currentpage + 1) * numperpage).show(); }); var numrows = $table.find('tbody tr').length; var numpages = math.ceil(numrows / numperpage); var $pager = $('<p class="pager"></p>'); for (var page = 0; page < numpages; page++) { $('<span class="page-number"></span>').text(page + 1) .bind('click', { newpage: page }, function(event) { currentpage = event.data['newpage']; $table.trigger('repaginate'); $(this).addclass('active').siblings().removeclass('active'); }).appendto($pager).addclass('clickable'); } $pager.insertbefore($table); $table.trigger('repaginate'); $pager.find('span.page-number:first').addclass('active'); }); }); </script>
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
jquery表格插件datatables使用详解
详解json.parse()与json.stringify()的区别以及使用方法
解析json有哪些方法
以上就是jquery操作表格实例详解(附代码)的详细内容。
其它类似信息

推荐信息