选择、取消、全选、全部取消、获取行ids
/** * Created by lizongqiong on 2016/1/8. */ var $ = require('jquery'); var table = { init:function(ele){ this.$ele = $(ele); this.initCheckbox(); }, /** * checkbox 事件初始化 */ initCheckbox:function(){ var _this = this; _this.$ele.on('mouseup',".title-checkbox",function(e){//全选checkbox e.preventDefault(); var o = $(this); if(o.attr('checked')){ _this.clearSelection(); }else{ _this.selectAll(); } }); _this.$ele.on('mouseup','.row-select',function(e){//table中checkbox e.preventDefault(); var row = $(this); if($(this).attr('selected')){ _this.deselect(row); }else{ _this.select(row); } }); }, /** * 选中一行 * @param row:行 */ select:function(row){ row.attr('selected',true); $(row.find("td :checkbox").eq(0)).attr('checked','checked'); }, /** * 反选一行 * @param row:行 */ deselect:function(row){ row.removeAttr('selected'); $(row.find("td :checkbox").eq(0)).removeAttr('checked'); }, /** * 清空选中行 */ clearSelection:function(){ var _this = this; _this.$ele.find("tr[selected]").each(function(){ _this.deselect($(this)); }) }, /** * 全部选中 */ selectAll:function(){ var _this = this; _this.$ele.find('tbody tr').each(function(){ var row = $(this); row.attr('selected',true); $(row.find("td :checkbox").eq(0)).attr('checked','checked'); }); }, /** * 获取选中行 * @return [tr,tr,...] */ getSelection:function(){ return this.$ele.find("tr[selected]"); }, /** * 获取选中id数组 * @return [1,2,3,...] */ getSelectedIds:function (){ var _this = this, rows = _this.getSelection(), ids = []; for(var i=0,len=rows.length;i<len;i++){ ids.push($(rows[i]).attr('rid')); } return ids; } } module.exports = table;