简体   繁体   English

jQuery数据表:选择扩展:如何在init上选择第一行以及如何至少选择一行

[英]jquery datatables: Select Extension: How to select first row on init and how to get at least one row selected

I have two questions for jquery datatables : 我对jquery数据表有两个问题:

  1. Is it possible to select the first row automatically when the DataTable is initialized? 初始化DataTable后,是否可以自动选择第一行?
  2. At the moment it is possible, that the user deselect an item by clicking on it. 目前,用户有可能通过单击来取消选择项目。 I want, that at least one row keeps selected. 我想要至少选择一行。 When the user clicks to the selected row, the selection should not be removed. 当用户单击到选定的行时,不应删除选择。

This is my initialization of my DataTable: 这是我对DataTable的初始化:

ar dataTableOption = {"pageLength": 5,
    "pagingType": "simple",
    "info": false,
    "searching": false,
    "select": {
        style: 'single'
    },
    "lengthChange": false,
    "columnDefs": [
        {
            "targets": [0],
            "visible": false,
            "searchable": false
        }
    ]
};

dataTable = $('#dataTable').DataTable(dataTableOption);

Thank you for your help in advance! 提前谢谢你的帮助!

One of the ways: 方式之一:

$(document).ready(function() {
    var table = $('#example').DataTable(); 

    $('#example tbody').on( 'click', 'tr', function () {

        if($(this).hasClass( "selected" )){
          //make sure it can't be unselected if there is just one selected row
          if(table.rows('.selected').data().length > 1){
            $(this).removeClass( "selected" );}
        }
        else{
         $(this).addClass( "selected" );
        }
    } );

    //To pre-select the first row
    $('#example tbody tr:eq(0)').click();
} );

Demo 演示版

@ Harshul Pandav @ Harshul Pandav

Thank you for your post. 谢谢你的文章。 Your proposed solution worked not 100% for me but it helped me a lot for finding a solution for my problems. 您提出的解决方案对我来说不是100%有效,但是它为我的问题找到了解决方案,对我很有帮助。

To select the first line worked this for me: 选择第一行对我来说是可行的:

dataTable.row(':eq(0)', { page: 'current' }).select();

To get at least one line selected I use the user-select event, to remember my last selected column: 要选择至少一行,我使用用户选择事件,以记住我上次选择的列:

dataTable.on( 'user-select', function ( e, dt, type, indexes ) {
      lastSelectedRow = dt.rows( '.selected' )[0][0];
    });

After this, the click event passed through, if no row is selected anymore, the row with index of last selected row is selected: 此后,单击事件将通过,如果不再选择任何行,则选择具有最后选择的行的索引的行:

 dataTable.on( 'click', 'tr', function (evt) { 
if (dataTable.rows( {selected:true} ).any() == false) {
     dataTable.row(lastSelectedRow).select();
  }
)};

For finding out the last selected row the deselect (dt.on( 'deselect', function ( e, dt, type, indexes ) ) event can also be used. 为了找出最后选择的行,还可以使用deselect(dt.on('deselect',function(e,dt,type,index)))事件。

Prevent deselect of already-selected row: 防止取消选择已选择的行:

    myTable.on('user-select', function (e, dt, type, cell, originalEvent) {
        if ($(cell.node()).parent().hasClass('selected')) {
            e.preventDefault();
        }
    });

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

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