简体   繁体   English

单击一行时,如何将“选定”类添加到表的一行?

[英]How can I add a “selected” class to just one row of a table when a row is clicked?

I am using the following code: 我使用以下代码:

$("#dataTable tbody").on("click", "tr", function (event) {
    if (!$(this).hasClass('row_selected')) {
        $(oTable.fnSettings().aoData).each(function () {
            $(this.nTr).removeClass('row_selected');
        });
        $(this).addClass('row_selected');
        gridClickHandler($(this));
    }
});

When a row is clicked then if the row is already selected nothing happens. 单击一行时,如果已选择该行,则不会发生任何操作。 If not then all the rows have the class removed and the current row has the row_selected class added. 如果没有,那么所有行都删除了类,并且当前行添加了row_selected类。

However this is slow as my tables have many rows. 但是这很慢,因为我的表有很多行。 It does not look good with the current delay. 目前的延迟看起来不太好。 What I thought of was moving the addClass to the start. 我想到的是将addClass移动到开头。 But if I do that the the .each loop removes it. 但是,如果我这样做,.each循环将删除它。

Is there a way I could make this work more efficiently (faster response)? 有没有办法让我的工作更有效率(更快的响应)?

<table id-"dataTable">
  <tbody>
    <tr>
      <td>January</td>
      <td>$100</td>
    </tr>
    <tr>
      <td>February</td>
      <td>$80</td>
    </tr>
  </tbody>
</table> 

Here's a sample 这是一个样本

$('table').on('click', 'tr', function() {

    var row = $(this);                 //cache the row

    if(!row.hasClass('selected'){
        row.addClass('selected')       //add class to clicked row
            .siblings()                //get the other rows
            .removeClass('selected');  //remove their classes
        gridClickHandler(row);
    }
});​

The advantage of using .on() is that it binds only one event handler to the parent (in this case, table ) for the children (the tr ). 使用.on()的优点是它只为子节点( tr )将一个事件处理程序绑定到父节点(在本例tabletable )。 Using .click() on each row means there is one handler for each tr element which is an overhead. 在每一行上使用.click()意味着每个 tr元素都有一个处理程序,它是一个开销。

So for example, if I had a thousand rows, there would be a thousand click handlers when you use .click() as opposed to only click handler on the table to listen for all the tr 's click events when using .on() . 因此,例如,如果我有一千行,当你使用.click()时会有一千个点击处理程序,而不是只使用table上的处理程序来监听使用.on()时所有tr的点击事件。

$("#dataTable tbody tr").click(function () {
    $('#dataTable tbody tr.selected').removeClass('selected');
    $(this).addClass('selected');
    gridClickHandler($(this));
});

Check this jsfiddle , works quite fast even with big tables! 检查这个jsfiddle ,即使有大桌子也能很快工作!

-- edited after comment -- - 评论后编辑 -

Try this:- 尝试这个:-

 $("#dataTable tbody tr").on("click", "tr", function (event) {        
         $("#dataTable tbody tr").removeClass('row_selected');
         $(this).addClass('row_selected');
     }
 });
$("#dataTable").on("click", "tr", function (e) {
    var $this = $(this);
    // this line removes all selected classes from the rows siblings
    $this.siblings().removeClass("row_selected");
    // this line will toggle the selected class,
    // therefore deselecting the row if it has been selected before
    $this.toggleClass("row_selected");
    gridClickHandler($this);
});

Or alternatively cache the previously selected row. 或者,缓存先前选择的行。

(function() {
    var $oldSelected = null;

    $("#dataTable").on("click", "tr", function (e) {
        var $this = $(this);
        // this line removes all selected classes from the rows siblings
        $oldSelected.removeClass("row_selected");
        // this line will toggle the selected class,
        // therefore deselecting the row if it has been selected before
        $oldSelected = $this.addClass("row_selected");
        gridClickHandler($this);
    });

})();

And as a side note, caching jQuery calls - or result of a function call that you need repeatedly for that matter - is always a good idea to save on processing time. 作为旁注,缓存jQuery调用 - 或者你需要反复进行的函数调用的结果 - 总是一个节省处理时间的好主意。

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

相关问题 单击该行时将选定的类添加到表行 - Add selected class to table row when click on that row 如何在另一个页面上显示刚刚被点击的表格行? - How can i display the table row that just got clicked, on another page? Vuejs如何向表中的每一行添加一个类 - Vuejs How can I add a class to each row in the table 单击按钮时如何删除行并添加CSS类 - how to remove row and add css class when button clicked 单击glyphicon时,将类添加到表行;再次单击时,将类删除 - Add class to table row when glyphicon is clicked and remove when clicked again 选择时如何使选定的行 go 到表格顶部 - How can I make the selected row go to the top of the table when is selected 如何将单击/选定的行数据从一个表传递到另一个并排放置的表 - How to pass clicked/selected row data from one table to another table that are placed side by side 我在bootstrap中有一个表 - 如何添加包含所选行的删除按钮的列? - I have a table in bootstrap - how can I add a column that contains remove button for selected row? 当我单击表行时,将显示一个div,它应回显该单击行中的任何一个字段值 - When I click on a table row, a div is displayed and it should echo any one field value from that clicked row 如何获得被点击的表格行的TBODY? - How can I get a clicked table row's TBODY?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM