【问题标题】:check uncheck All checkboxes not working for all pages when using datatable使用数据表时,选中取消选中所有复选框不适用于所有页面
【发布时间】:2015-07-08 04:25:04
【问题描述】:

脚本:

$(document).ready(function() {
    $(function(){
        $("#result").dataTable({ 
            "scrollX" : true, 
                "ordering" : true,
                "order": [[ 1, "asc" ]],
                "info" : true, 
                });
        });  
    $("#all").click(function () { 
        if ($("#all").is(':checked')) {
            $(".checkboxclass").each(function () {
                $(this).prop("checked", true);
            }); 
        } else {
            $(".checkboxclass").each(function () {
                $(this).prop("checked", false);
            });
        }
    }); 
});     

HTML:

<table class="tg" id="result" style="width: 100%">
    <thead>
        <tr>
            <th class="tg" style="text-align: center"><input type="checkbox" id="all" 
                onclick="toggle(this);" /></th>
        </tr> 
    </thead>
    <tbody>
        <tr th:each="map : ${listID}">
            <td class="tg bg" align="center"><input type="checkbox" class="checkboxclass" name="check"
                th:id="${map['ID']}" th:value="${map['ID']}" /></td>
        </tr>       
    </tbody>
</table>

选中一个复选框会选择所有但仅限于当前页面。它不适用于分页中的其余页面

任何人都可以帮助解决这个问题。谢谢。

【问题讨论】:

  • 您的意思是它适用于首页但不适用于其他页面?并且您希望它适用于分页中的每一页或所有记录?
  • 它适用于第一页。我想让它适用于所有页面(表中的所有记录)
  • 每一页和所有记录都不一样?
  • 为什么$(document).ready(function() { $(function(){都在里面?对我来说似乎相当不必要。

标签: javascript jquery html checkbox datatable


【解决方案1】:

很可能,您有重复的 ID;如果您有多个具有相同 ID 的元素,例如 id="all",则会导致 HTML 无效,并且您无法预测不同的浏览器会如何处理它。大多数人通常会选择第一个,而忽略其余的元素。

请更改:

 <input type="checkbox" id="all" onclick="toggle(this);" />

收件人:

<input type="checkbox" class="all" ....

这样您的代码将是:

$(".all").on('change', function () {
    $(this).closest('table').find('.checkboxclass').prop('checked', this.checked ); 
}); 

更新

感谢您的澄清;我将保留上述内容,以便您澄清这一点的 cmets 可能仍然相关

您可以通过以下方式解决该问题:

    $(function(){
        var table = $("#result").dataTable({ 
            "scrollX" : true, 
                "ordering" : true,
                "order": [[ 1, "asc" ]],
                "info" : true, 
                });
        });  
        $("#all").on('click', function () { 
            var cells = table.api().cells().nodes();
            $( cells ).find('.checkboxclass').prop('checked', this.checked);
        });
    });
    //source: https://www.datatables.net/forums/discussion/22728/datatables-1-10-select-all-checkbox-and-hidden-rows-pages

请注意 $(document).ready(function() { .... });$(function() { .... }); 是写同一件事的不同方式——无需嵌套。

【讨论】:

  • 感谢您的回复。 id="all" 仅用于 te 表的标题,并且只出现一次。它不会重复。单击 id="all" 时,它将检查表中的所有行。但是与数据表一起使用时,它不会检查其他分页页面中的行
  • 好的,现在我明白你的意思了。让我检查一下文档。
  • @Java_User,如果更新的答案有帮助,请告诉我。
  • 我已经尝试过您的解决方案。我收到错误table is not a function。然后我将#all 的点击事件放置在函数内部。然后我收到错误table.cells is not a function。我无法解决此错误。
  • 应该是var cells = table.api().cells().nodes(); 看到更正再试一次。
【解决方案2】:

这对我有用。

if ($("#all").is(':checked')) { 
  $(".checkboxclass", table.fnGetNodes()).each(function () { 
  $(this).prop("checked", true);
  });     
else {
  $(".checkboxclass", table.fnGetNodes()).each(function () {
  $(this).prop("checked", false); 
  })
  }

【讨论】:

  • 很高兴知道这对您有用。只是为了让你知道你不必使用每个方法——jQuery 在集合内部使用它——以及 if 语句。
  • 好的。感谢您提供额外的有用信息。我以为只有 each 实际上在获取表中的每条记录。
  • 没问题。很高兴能帮上忙。
【解决方案3】:

您已声明单击切换功能,但未使用。请找到代码 sn-p 它有助于解决您的问题。

<table class="tg" id="result" style="width: 100%">
    <thead>
        <tr>
            <th class="tg" style="text-align: center">
                <input type="checkbox" id="all" />
            </th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="tg bg" align="center"><input type="checkbox" class="checkboxclass" name="check" /></td>
            <td class="tg bg" align="center"><input type="checkbox" class="checkboxclass" name="check" /></td>
            <td class="tg bg" align="center"><input type="checkbox" class="checkboxclass" name="check" /></td>
            <td class="tg bg" align="center"><input type="checkbox" class="checkboxclass" name="check" /></td>
        </tr>
    </tbody>
</table>

 $("#all").click(function () {
    if (this.checked) {
        $('.checkboxclass').each(function () {
            this.checked = true;
        })
    }
    else {
        $('.checkboxclass').each(function () {
            this.checked = false;
        })
    }
})

【讨论】:

  • 感谢您的回答。这再次检查所有分页的 page1 中的所有复选框。我想问题出在数据表而不是 jquery 上。因为它无法在其余页面中找到复选框
  • 您希望对每个页面操作进行状态维护吗?
  • 是的。应该保留这些值。
  • 好的。请尝试一下,您必须将最后一个状态存储在本地存储中,它会保持该值直到刷新浏览器
  • 表格将有数千条记录,在第一页显示前 10 条,其余的在下一页使用数据表显示。功能是,当用户点击export按钮时,他们可以检查所有应该从隐藏的分页部分获取记录的记录。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-15
  • 2017-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多