【问题标题】:Limit number of checkboxes allowed to be selected in a Datatable in javascript (Ruby on Rails)限制允许在 javascript (Ruby on Rails) 中的数据表中选择的复选框数量
【发布时间】:2017-07-18 01:59:37
【问题描述】:

我有一个数据表,其中有一列允许用户在数据表中选择多条记录。

问题是我希望他们只能从此数据表中选择 1 条记录。

理想的解决方案是将选择限制为 1 条记录,并且每次他们选择另一条记录时,它都会从最后一条选择的记录中删除检查。我想知道如何使用 javascript 来实现这一点。谢谢

编辑 - 这是我的桌子

`$('#email_templates').DataTable({
      pageLength: 25,
      responsive: true,
      processing: true,
      serverSide: true,
      autoWidth: false,
      ajax: $('#email_templates').data('source'),
      dom: '<"html5buttons"B>lTfgitp',
      buttons: [
      { extend: 'copy'},
      {extend: 'csv'},
      {extend: 'excel', title: 'Email Templates'},
      {extend: 'pdf', title: 'Email Templates'},
      {extend: 'print',
      customize: function (win){
        $(win.document.body).addClass('white-bg');
        $(win.document.body).css('font-size', '10px');

        $(win.document.body).find('table')
        .addClass('compact')
        .css('font-size', 'inherit');
      }
    }
    ],
    columnDefs: [
    {
      orderable: false,
      targets: [0,1]

    },
    {
     'targets': 0,
     'searchable': false,
     'orderable': false,
     'className': 'dt-body-center',
     render: function (data, type, full, meta){
       return '<input type="checkbox" class="checkbox" name="id[]" value="' + $('<div/>').text(data).html() + '">';
     }
   }
   ],
   columns: [
   { width: "20%" },
   { width: "40%" },
   { width: "40%" }
   ]
 });

  });`

【问题讨论】:

  • 你可以使用单选按钮吗?您将免费获得该行为。你甚至可以style them like checkboxes
  • 这正是我所做的。提交答案,以便我接受! @安德鲁
  • 太棒了,会的!

标签: javascript ruby-on-rails ruby datatable


【解决方案1】:

尝试改用单选按钮。您将免费获得该行为。你甚至可以style them like checkboxes

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    相关资源
    最近更新 更多