gpt4 book ai didi

javascript - 如何在引导表上添加分页和搜索

转载 作者:行者123 更新时间:2023-12-03 02:21:31 25 4
gpt4 key购买 nike

我正在尝试在我的 javascript 中使用以下代码来呈现表格:

$('#actionTable').bootstrapTable({
//Assigning data to table
data: serviceDetails.actions
});
}

这是我在 HTML 上的表格定义

    <table id="actionTable" class="table table-bordered table-striped">
<thead>
<tr>
<th data-field="service_type">Action</th>
<th data-field="resolution_url" data-formatter="LinkFormatter">Endpoint URL</th>
</tr>
</thead>
</table>

表格按预期呈现,我想在顶部添加一个搜索框,在底部添加分页,我们如何才能实现这一点。 bootstrapTable的官方文档有哪些可以引用的?

最佳答案

在 SB Admin 2 Bootstrap 3 主题中,为了显示高级和响应式表格,使用了 DataTables for jQuery 插件。来自 SB Admin 2 网站:

DataTables is a very flexible, advanced tables plugin for jQuery. In SB Admin, we are using a specialized version of DataTables built for Bootstrap 3. We have also customized the table headings to use Font Awesome icons in place of images. For complete documentation on DataTables, visit their website at https://datatables.net/.

SB Admin 2 主题:https://startbootstrap.com/template-overviews/sb-admin-2/
数据表实例:https://blackrockdigital.github.io/startbootstrap-sb-admin-2/pages/tables.html

对于我的项目,我通过以下方式应用了 SB Admin 主题中的 DataTable:

创建<table>width="100%"用于响应行为和可选的一些 CSS 类

<table width="100%" class="table table-striped table-bordered table-hover" id="actionTable">

在 header 中包含 CSS 样式表:dataTables.bootstrap.css 和可选的 dataTables.responsive.css

<!-- DataTables CSS -->
<link href="dataTables.bootstrap.css" rel="stylesheet">

<!-- DataTables Responsive CSS -->
<link href="dataTables.responsive.css" rel="stylesheet">

<body>底部包含JS脚本 block :jquery.dataTables.min.js 和可选的 dataTables.bootstrap.js 和 dataTables.responsive.js:

<script src="jquery.dataTables.min.js"></script>
<script src="dataTables.bootstrap.min.js"></script>
<script src="dataTables.responsive.js"></script>

为 $(document).ready 中的表执行 DataTable 脚本。如果您包含可选脚本和样式表,请将响应设置为 true:

<script>
$(document).ready(function() {
$('#actionTable').DataTable({
responsive: true
});
});
</script>

最后,您应该得到符合您要求的表格:顶部的搜索框和底部的分页+列排序和页面大小选择框。

使用DataTable的缺点是需要一次性加载所有数据,因此如果要显示大数据,可能会减慢渲染速度。可以使用 jQuery.ajax() 制作“服务器端”版本的分页。使用 jQuery 获取所选页面的内容并替换表格内容。

关于javascript - 如何在引导表上添加分页和搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49138065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com