简体   繁体   English

选择表格单元格作为组

[英]Select table cell as a group

In this table, I want to select multiple cells as a group. 在此表中,我想选择多个单元格作为一个组。 You can select a target cell by clicking on it, but I also want to click and drag to cover multiple cells as a group. 您可以通过单击来选择目标单元格,但我也想单击并拖动以将多个单元格作为一个组覆盖。

Is that possible to achieve that without any plugins (just use JQuery)? 是否有可能在没有任何插件的情况下实现这一目标(仅使用JQuery)?

This is what I want to achieve: 这是我要实现的目标:

在此处输入图片说明

The array content will be: ["3-2", "3-3", "4-2", "4-3", "5-2", "5-3"] 数组内容将为: ["3-2", "3-3", "4-2", "4-3", "5-2", "5-3"]

Here is what I try so far: 到目前为止,这是我尝试的方法:

 var group = []; var columnIndex = 7, rowIndex = 10; var $tableContainer = $('#tablecontainer'), $table = $tableContainer.append('<table border="1" id="table1"></table>'), $thead = $table.find('table').append('<thead></thead>'), $tbody = $table.find('table').append('<tbody></tbody>'); var $columnNumber = ""; var $tdNumber = ""; for (var col = 1; col <= columnIndex; col++) { $columnNumber += "<th>Column " + col + "</th>"; $tdNumber += "<td style='text-align: center; vertical-align: middle; color:red'></td>"; } $thead = $thead.append('<tr><th>0</th>' + $columnNumber + '</tr>'); for (var row = 1; row <= rowIndex; row++) { $tbody = $tbody.append('<tr><td>Row ' + row + '</td>' + $tdNumber + '</tr>'); } $('#table1 > tbody > tr > td').hover(function() { var colIndex = $(this).parent().children().index($(this)); var rowIndex = $(this).parent().parent().children().index($(this).parent()); $(this).attr("title", 'Row: ' + rowIndex + ', Column: ' + colIndex); // console.log('Row: ' + rowIndex + ', Column: ' + colIndex); }); $('#table1 > tbody > tr > td').click(function() { var colIndex = $(this).parent().children().index($(this)); var rowIndex = $(this).parent().parent().children().index($(this).parent()); group.push(rowIndex + "-" + colIndex); console.log(group); $(this).css("background-color", "red"); // console.log('Row: ' + rowIndex + ', Column: ' + colIndex); }); 
 #table1>tbody>tr>td:hover { background: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="tablecontainer"></div> 

Any help would be appreciated! 任何帮助,将不胜感激!

The functionality you want to achieve is possible by using jquery mouseover function. 您可以通过使用jquery mouseover函数来实现您想要的功能。 I made a fiddle to get the desired output as you expect. 我做了一个小提琴以获得期望的输出。

Fiddle 小提琴

Hope this helps you in solving the issue. 希望这可以帮助您解决问题。

-Help :) -救命 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM