| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | 1× 1× 1× 1× 1× 1× 4× 2× 2× 2× 2× 2× 1× 2× 2× 2× 2× 1× 1× 2× 2× 2× 2× 2× 2× 2× 1× 1× 1× 1× 1× 1× 2× 2× 2× 2× 1× 1× 1× 1× 1× 1× | /** * 表格选择器 * * @file table选择器插件 * @author Brian Li(lbxxlht@163.com) * * 宿主fire的事件: * select * * 暴露的API: * .selectColumn(column, select) * .selectRow(row, select) * .selectCell(index, select) * .getSelected() */ var define = typeof define === 'function' && define.amd ? define : function (factory) { typeof module === 'object' ? (module.exports = factory(require)) : ''; }; define(function (require) { var util = require('../core/util'); var Plugin = require('../core/plugin'); // public ///////////////////////////////////////////////////////////////// /** * 构造函数 * * @constructor * @param {Object} param 初始化对象 * @param {string} param.mode 工作模式:row; column; cell; * @param {string} param.select 选择模式:single; multiple */ function TableSelector(param) { if (!(this instanceof TableSelector)) { return new TableSelector(param); } param = param || {}; this.mode = param.mode || 'row'; this.select = param.select || 'single'; if (this.mode.indexOf('column') > -1) { this.required = ['TableHeader']; } this.selectedRows = []; this.selectedColumns = []; this.selectedCells = []; Plugin.call(this, param); } util.inherit(TableSelector, Plugin); /** * @override * * @param {Object} param 宿主UI初始化时的参数引用,***操作请小心*** * @param {Object} plugins 已经加载过的plugin * @return {boolean} 是否初始化成功 */ TableSelector.prototype.load = function (param, plugins) { param.pluginSkin = util.joinClassName(param.pluginSkin, 'table-selector'); Iif (plugins.TableSorter) { plugins.TableSorter.on('sorted', clearSelected, this); } util.appendHandler(param, 'click', mouseHandler); util.appendHandler(param, 'mouseover', mouseHandler); util.appendHandler(param, 'mouseout', mouseHandler); extendAPI(this.host, this); return true; }; /** * 选中列 * * @param {number} column 列序号,由0开始 * @param {boolean} select 是否选中 */ TableSelector.prototype.selectColumn = function (column, select) { if (isNaN(column) || column < 0) { return; } if (select && this.selectedColumns.indexOf(column) > -1) { return; } if (!select && this.selectedColumns.indexOf(column) < 0) { return; } var container = $(this.host.container); if (select) { this.selectedColumns.push(column); modColumn(container, column, 'addClass', 'selected'); this.host.fire('select', {column: this.selectedColumns}); return; } this.selectedColumns.remove(column); modColumn(container, column, 'removeClass', 'selected'); this.host.fire('select', {column: this.selectedColumns}); }; /** * 选中行 * * @param {number} row 行序号,由0开始 * @param {boolean} select 是否选中 */ TableSelector.prototype.selectRow = function (row, select) { if (isNaN(row) || row < 0) { return; } if (select && this.selectedRows.indexOf(row) > -1) { return; } if (!select && this.selectedRows.indexOf(row) < 0) { return; } var container = $(this.host.container); if (select) { this.selectedRows.push(row); modRow(container, row, 'addClass', 'selected'); this.host.fire('select', {row: this.selectedRows}); return; } this.selectedRows.remove(row); modRow(container, row, 'removeClass', 'selected'); this.host.fire('select', {row: this.selectedRows}); }; /** * 选中单元格 * * @param {string} index 单元格索引"row-column" * @param {boolean} select 是否选中 */ TableSelector.prototype.selectCell = function (index, select) { if (select && this.selectedCells.indexOf(index) > -1) { return; } if (!select && this.selectedCells.indexOf(index) < 0) { return; } var arr = index.split('-'); var row = ~~arr[0]; var column = ~~arr[1]; var container = $(this.host.container); if (select) { this.selectedCells.push(index); modCell(container, row, column, 'addClass', 'selected'); this.host.fire('select', {cell: this.selectedCells}); return; } this.selectedCells.remove(index); modCell(container, row, column, 'removeClass', 'selected'); this.host.fire('select', {cell: this.selectedCells}); }; /** * 获取选中数据 * * @return {Array} 选中的数据:row对应原始数据的item,column为当前选中列值的二维数组,cell为选中单元格值的一位数组 */ TableSelector.prototype.getSelected = function () { var result = []; var datasource = this.host.datasource; var selected = null; var container = $(this.host.container); if (this.mode === 'row') { selected = this.selectedRows.sort(); for (var n = 0; n < selected.length; n++) { result.push(datasource[selected[n]]); } } else if (this.mode === 'column') { selected = this.selectedColumns.sort(); for (var x = 0; x < selected.length; x++) { var line = []; var group = container.find('[data-ui-column="' + selected[x] + '"]').filter('.td'); for (var y = 0; y < group.length; y++) { line.push(group[y].innerHTML); } result.push(line); } } else if (this.mode === 'cell') { selected = this.selectedCells.sort(); for (var p = 0; p < selected.length; p++) { var arr = selected[p].split('-'); var row = ~~arr[0]; var column = ~~arr[1]; var value = container.find('[data-ui-row="' + row + '"]') .filter('[data-ui-column="' + column + '"]') .html(); result.push({ row: row, column: column, value: value }); } } return result; }; return TableSelector; // private ///////////////////////////////////////////////////////////////// /** * 扩展宿主对象API * * @param {Table} table table实例 * @param {TableSorter} selector TableSelector实例 */ function extendAPI(table, selector) { table.selectColumn = function (a, b) { return selector.selectColumn(a, b); }; table.selectRow = function (a, b) { return selector.selectRow(a, b); }; table.selectCell = function (a, b) { return selector.selectCell(a, b); }; table.getSelected = function () { return selector.getSelected(); }; } /** * 处理一切鼠标事件 * * @param {Event} evt 鼠标事件句柄 */ function mouseHandler(evt) { var dataset = util.getDataset(evt.target); if (dataset.uiLevel !== undefined) { // 有uiLevel属性说明鼠标操作的是附加按钮之类,不对这些交互做相应 return; } var selector = this.plugin.TableSelector; var container = this.jContainer ? this.jContainer : this.jContainer = $(this.container); var mousePosition = this.getMouseAxis(evt.target); if (evt.type === 'click') { select(mousePosition[0], mousePosition[1], selector); return; } var handler = evt.type === 'mouseover' ? 'addClass' : 'removeClass'; if (selector.mode === 'row' && mousePosition[0] > -1) { modRow(container, mousePosition[0], handler, 'mouseover'); return; } if (selector.mode === 'column' && mousePosition[0] === -1 && mousePosition[1] > -1) { modColumn(container, mousePosition[1], handler, 'mouseover'); return; } if (selector.mode === 'cell' && mousePosition[0] > -1 && mousePosition[1] > -1) { modCell(container, mousePosition[0], mousePosition[1], handler, 'mouseover'); return; } } /** * 处理选中 * * @param {number} row 行号 * @param {number} column 列号 * @param {Object} selector 选择插件实例 */ function select(row, column, selector) { var type = null; var index = null; var data = null; // 列选择 if (selector.mode === 'column' && row === -1 && column > -1) { type = 'selectColumn'; index = column; data = 'selectedColumns'; } // 行选择 if (selector.mode === 'row' && row > -1) { type = 'selectRow'; index = row; data = 'selectedRows'; } // 单元格 if (selector.mode === 'cell' && row > -1 && column > -1) { type = 'selectCell'; index = row + '-' + column; data = 'selectedCells'; } // 执行选择 if (type == null) { return; } if (selector.select === 'multiple') { selector[type](index, !(selector[data].indexOf(index) > -1)); return; } if (selector[data].length < 1) { selector[type](index, true); return; } if (selector[data][0] === index) { selector[type](index, false); return; } selector[type](selector[data][0], false); selector[type](index, true); return; } /** * 修改某行所有单元格样式 * * @param {Object} container ui跟容器的jQuery对象 * @param {number} row 行号 * @param {string} handler 操作方法,'addClass'或'removeClass' * @param {string} classname 待修改样式 */ function modRow(container, row, handler, classname) { container.find('.tr').filter('[data-ui-row="' + row + '"]')[handler](classname); } /** * 修改某列所有单元格样式 * * @param {Object} container ui跟容器的jQuery对象 * @param {number} column 列号 * @param {string} handler 操作方法,'addClass'或'removeClass' * @param {string} classname 待修改样式 */ function modColumn(container, column, handler, classname) { container.find('[data-ui-column="' + column + '"]')[handler](classname); } /** * 修改某个单元格样式 * * @param {Object} container ui跟容器的jQuery对象 * @param {number} row 行号 * @param {number} column 列号 * @param {string} handler 操作方法,'addClass'或'removeClass' * @param {string} classname 待修改样式 */ function modCell(container, row, column, handler, classname) { container.find('[data-ui-row="' + row + '"][data-ui-column="' + column + '"]')[handler](classname); } /** * 清空已选择数据 */ function clearSelected() { this.selectedRows = []; this.selectedColumns = []; this.selectedCells = []; } }); |