简体   繁体   English

选择时如何使选定的行 go 到表格顶部

[英]How can I make the selected row go to the top of the table when is selected

I am just interested on moving the selected row to the top of the table when the checkbox is checked当复选框被选中时,我只是对将所选行移动到表格顶部感兴趣

the only thing that I achieved was moving the checkbox to the top of the table, but when i unchecked the check boxes they stayed at the top我唯一实现的就是将复选框移到表格的顶部,但是当我取消选中复选框时,它们停留在顶部

this is the code in just one file, the javascript code is at the very bottom of the file这只是一个文件中的代码,javascript 代码在文件的最底部

    <!DOCTYPE html>
    <html>

    <head>
      <title>HTML Table Row Up And Down</title>
      <meta charset="windows-1252">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <style>
        table {
          border: 1px solid black;
          border-collapse: collapse;
        }

        #table {
          list-style-type: none;
          margin: 0;
          padding: 0;
          width: 60%;
        }

        #table tr {
          margin: 3px;
          padding: 0.4em;
          font-size: 1.4em;
          height: 18px;
        }

        thead tr th {
          background-color: #66e9ff;
          font-size: 20px;
          border: 2px solid black;
          border-collapse: collapse;
        }

        tbody td {
          min-width: 100px;
          border: 1px solid black;
        }

        tbody td:first-child {
          text-align: center;
        }

        tbody tr.checked td {
          background-color: #ffbbbb;
          color: dark;
        }

        tbody tr:hover {
          background-color: yellow;
        }

        #feedback {
          font-size: 1.4em;
        }

        table .ui-selecting {
          background: #FECA40;
        }

        table .ui-selected {
          background: #928bff;
          color: white;
        }
      </style>
    </head>

    <body>

      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <table id="Table">
        <thead>
          <tr>
            <th><input type="checkbox" class="check_all" value="Check All"></th>
            <th>Header 1</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td><input type="checkbox"></td>
            <td>Label 1</td>
          </tr>
          <tr>
            <td><input type="checkbox"></td>
            <td>Label 2</td>
          </tr>
          <tr>
            <td><input type="checkbox"></td>
            <td>Label 3</td>
          </tr>
          <tr>
            <td><input type="checkbox"></td>
            <td>Label 4</td>
          </tr>
        </tbody>
      </table>

      <button>&ShortUpArrow;</button>
      <button>&ShortDownArrow;</button>

      <script>
        $(function() {

          function setCheck(o, c) {
            o.prop("checked", c);
            if (c) {
              //The closest() method returns the first ancestor of the selected element.
              o.closest("tr").addClass("checked");
            } else {
              o.closest("tr").removeClass("checked");
            }
          }

          $("tbody tr").on("click", function(e) {
            var chk = $("[type='checkbox']", this);
            setCheck(chk, !$(this).hasClass("checked"));

//moved the checkboxes to the top------------------------        
//        var $tbody = $("table");
//        $tbody.prepend( $tbody.find(chk) );
          });
        });

      </script>
    </body>

    </ht

ml>

You are looking for.prependTo您正在寻找.prependTo

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script> $("td").click(function () { $(this).parent().prependTo("#mytable"); }); </script> <table id="mytable"> <tr> <td>row 1</td> </tr> <tr> <td>row 2</td> </tr> <tr> <td>row 3</td> </tr> </table>

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

相关问题 如何将列表框中的选定项目滚动到顶部? - How can I make selected item in listbox scroll to the top? 单击一行时,如何将“选定”类添加到表的一行? - How can I add a “selected” class to just one row of a table when a row is clicked? 在 materia-ui 表中。 当我单击按钮展开行时,它会展开所有行。 我怎样才能使只有选定的行会扩大? - In materia-ui table. When I click the button to expand the row, it expands all rows. How can i make only selected row will expand? 选中复选框时如何突出显示表格行? 角 7 - How do I highlight a table row when checkbox is selected? Angular 7 如何在表格中连续突出显示选定的 td? - How can I highlight selected tds in a row in my table? 不确定如何删除表中的(突出显示/选定的行) - Unsure as to how to go about deleting the (highlighted/selected row) in table 选中表格中的复选框后,如何包装表格的行? - How to wrap a row of the table when a checkbox in it is selected? 如何在jquery中的选定行的顶部追加新的表行 - How to append new table rows on top of selected row in jquery 如何检测何时选择了行并且在jqgrid中选择了非行? - How can I detect when a row is selected and non of rows is selected at jqgrid? 未选择文本区域时如何显示占位符? - How can I make my placeholder show when the textarea is not selected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM