javascript - 如何通过按 Enter 键将一个表的突出显示行克隆到另一个表

标签 javascript jquery html

我有一个 HTML <table>与数据。单击表行后,我可以将单击的行添加到另一个表中。但我想用箭头键突出显示该行,然后在突出显示的行上按 Enter 键,将突出显示的行的数据添加到新表中。

我怎样才能做到这一点?

到目前为止我已经尝试过:

$(document).ready(function(){
	$('#myTable').focus();
});

function highlight(tableIndex) {
if ((tableIndex + 1) > $('#myTable tbody tr').length) //restart process
tableIndex = 0;

if ($('#myTable tbody tr:eq(' + tableIndex + ')').length > 0) {
$('#myTable tbody tr').removeClass('highlight');

$('#myTable tbody tr:eq(' + tableIndex + ')').addClass('highlight');
}
}

$('#goto_first').click(function() {
highlight(0);
});

$('#goto_prev').click(function() {
highlight($('#myTable tbody tr.highlight').index() - 1);
});

$('#goto_next').click(function() {
highlight($('#myTable tbody tr.highlight').index() + 1);
});

$('#goto_last').click(function() {
highlight($('#myTable tbody tr:last').index());
});

$(document).keydown(function(e) {

switch (e.which) {
case 38:
$('#goto_prev').trigger('click');
break;
case 40:
$('#goto_next').trigger('click');
break;
}

});

$(document).ready(function() {
  var items = [];

  $("#myTable tr").on('click', function(e) {

    var newTr = $(this).closest("tr").clone();
    items.push(newTr);
    newTr.appendTo($("#cloneTable"));
  });
})
<html><head><title>dynamictable</title>      
<style>
table { cursor: pointer; }
.highlight { background-color: lightgreen; }
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
    <table id="myTable" border="1px" style="width: 30%;">
    <thead>
      <tr>
        <th>##</th>
        <th>Name</th>
        <th>code</th>
        <th>unit</th>
        <th>rate</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>1</td>
        <td>aaa</td>
        <td>aa1</td>
        <td>aa</td>
        <td>111</td>
      </tr>
      <tr>
        <td>1</td>
        <td>aaa</td>
        <td>aa1</td>
        <td>aa</td>
        <td>111</td>
      </tr>
    </tbody>
  </table>
  
<br>
<br>
<br>
<input type="button" id="goto_first" value="first" class="btn btn-success">
<input type="button" id="goto_prev" value="prev" class="btn btn-secondary">
<input type="button" id="goto_next" value="next" class="btn btn-secondary">
<input type="button" id="goto_last" value="last" class="btn btn-success">
<br>
<br>
<br>

  <table id="cloneTable" border="1px" style="width: 30%; float :left;"><!--new table to clone data-->
    <thead>
      <tr>
        <th>##</th>
        <th>Name</th>
        <th>code</th>
        <th>unit</th>
        <th>rate</th>
      </tr>
    </thead>
    <tbody>
    </tbody>
  </table>
 </body>
 </html>

最佳答案

给你

// highlight first row default
$("#myTable tbody tr:first-child").addClass("highlight");

document.onkeydown = moveAndAdd;

function moveAndAdd(e) {
  e = e || window.event; 
  if (e.keyCode == "38") {
    // up arrow
    activeRow = $("tr.highlight"); /* get highlighted row */
    activeRow.focus();
      prevRow = activeRow.prev('tr'); /*very previous siblings*/
      if (prevRow.length>0) {
      activeRow.removeClass("highlight"); /*remove highlight from active class */
      prevRow.addClass("highlight"); /* make very prev row highlighted*/
    }
  } else if (e.keyCode == "40") {
    // down arrow
    activeRow = $("tr.highlight"); /* get highlighted row */
    activeRow.focus();
    nextRow = activeRow.next('tr'); /*very previous siblings*/
      if (nextRow.length>0) {
      activeRow.removeClass("highlight");
      nextRow.addClass("highlight");
    }
  }
  else if (e.which == 13 || e.which == 32) { 
        // Enter or Spacebar - edit cell
        e.preventDefault();
         cloneRow = $(".highlight").clone(true); /*clone highlighted row*/
        $("#cloneTable").append(cloneRow.removeClass("highlight")); /*append cloned row but remove class */
    }
}
table {
  color:black;
  background-color:white;
}
.highlight{
  color:White;
  background-color:green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="myTable" border="1px" style="width: 30%;">
    <thead>
      <tr >
        <th>##</th>
        <th>Name</th>
        <th>code</th>
        <th>unit</th>
        <th>rate</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td >1</td>
        <td>aaa</td>
        <td>aa1</td>
        <td>aa</td>
        <td>111</td>
      </tr>
      <tr>
        <td>2</td>
        <td>bbb</td>
        <td>bb2</td>
        <td>bb</td>
        <td>222</td>
      </tr>
       <tr>
        <td>3</td>
        <td>ccc</td>
        <td>cc3</td>
        <td>cc</td>
        <td>333</td>
      </tr>
       <tr>
        <td>4</td>
        <td>ddd</td>
        <td>dd1</td>
        <td>dd</td>
        <td>444</td>
      </tr>
    </tbody>
  </table>
 <table id="cloneTable" border="1px" style="width: 30%; float :left;">
    <thead>
      <tr>
        <th>##</th>
        <th>Name</th>
        <th>code</th>
        <th>unit</th>
        <th>rate</th>
      </tr>
    </thead>
    <tbody>
    </tbody>
  </table>

关于javascript - 如何通过按 Enter 键将一个表的突出显示行克隆到另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49805662/

相关文章:

jquery - 无法在 IE 中使用 jQuery 为 <td> 设置 CSS

html - 如何将下拉框(在表单中)的选定选项的值从 html 传递到 ts 文件,而无需在 Angular 中提交表单

javascript - 动态搜索栏 - 从 ASP.NET MVC5 中的下拉列表中添加元素

javascript - 从嵌套对象中获取数据

javascript - 当我的网站位于 iFrame 内时,Google Ads 不显示

javascript - JS获取滚动实例高度

javascript - 确定一个 Javascript 对象是一个 "complex"对象还是一个字符串

javascript - 使用ajax请求传递js对象

html - 将节内的文本居中

html - 纯用CSS3绘制一个带有悬停效果的L形按钮