如何通过按Enter键将一个表的突出显示行克隆到另一个表 [英] How to clone the Highlighted Row of one table to another Table by pressing Enter key

查看:49
本文介绍了如何通过按Enter键将一个表的突出显示行克隆到另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an HTML <table> with data. Upon clicking the table rows, I can add the clicked row to another table. But I want to highlight the row with arrow keys and, on pressing the Enter key on the highlighted row, add the data of the highlighted row to the new table.

我该怎么做?

到目前为止我尝试过的事情:

What I have tried so far:

$(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>

这篇关于如何通过按Enter键将一个表的突出显示行克隆到另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆