Boostrap 3中Datatables插件的显示格式自定义 [英] Customization of Display Format for Datatables Plugin in Boostrap 3

查看:13
本文介绍了Boostrap 3中Datatables插件的显示格式自定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的搜索栏移动到 Show entries 的位置旁边,即向左位置.我将此 CSS 样式代码应用于过滤器输入:

.dataTables_filter {宽度:50%;向左飘浮;文本对齐:左;}

它确实向左移动,但没有像预期的那样,只是向左移动了一点,在中心.如下图所示:

知道如何解决这个问题吗?另一种可接受的解决方案是仅交换Show entrySearch 的位置.如果可以,我该怎么做?

关于我的代码,我没有做任何特别的事情,我只是直接从数据表导入了库> API.我所拥有的只是一个用于显示表格的简单 HTML 代码.

解决方案

既然你在使用 Bootstrap,我会解释如何处理与 datatables 插件相关的元素的显示.Datatables 提供了一种配置 dom 的方法,换句话说,它允许您配置如何显示构成表格一部分的不同元素.这些标识如下:

<块引用>

DataTables 中的内置表格控件元素是:

l - 长度改变输入控件
f - 过滤输入
t - 桌子!
i - 表格信息汇总
p - 分页控制
r - 处理显示元素

数据表引导的默认dom配置是下一个:

引导程序 3:

"<'row'<'col-sm-6'l><'col-sm-6'f>>"+<'row'<'col-sm-12'tr>>"+<'row'<'col-sm-5'i<'col-sm-7'p>"

引导程序 4:

<'row'<'col-sm-12 col-md-6'l><'col-sm-12 col-md-6'f>"+<'row'<'col-sm-12'tr>>"+<'row'<'col-sm-12 col-md-5'i<'col-sm-12 col-md-7'p>"

现在,Bootstrap 3dom 设置被转换为以下html 标记:

<div class="col-sm-6">l</div><div class="col-sm-6">f</div>

<div class="row"><div class="col-sm-12">tr</div>

<div class="row"><div class="col-sm-5">i</div><div class="col-sm-7">p</div>

这就是为什么当您在 过滤控件 上使用以下 CSS 样式时,它只会转到页面的中间(注意过滤控件 f 被包裹在一个 col-sm-6 元素中):

.dataTables_filter {宽度:50%;向左飘浮;文本对齐:左;}

好处是您可以使用 dom 选项在 Datatables 初始化时更改此设置.在您的特定情况下,您可以尝试接下来的两个设置,但是,我邀请您玩一下,以找到另一个对您更有说服力的:

设置 1:

<'row'<'col-sm-3'l><'col-sm-3'f><'col-sm-6'>"+<'row'<'col-sm-12'tr>>"+<'row'<'col-sm-5'i<'col-sm-7'p>"

这将被转述到下一个html:

<div class="col-sm-3">l</div><div class="col-sm-3">f</div><div class="col-sm-6"></div>

<div class="row"><div class="col-sm-12">tr</div>

<div class="row"><div class="col-sm-5">i</div><div class="col-sm-7">p</div>

Bootstrap 3 中的示例:

$(document).ready(function(){var domCfg = "<'row'<'col-sm-3'l><'col-sm-3'f><'col-sm-6'>"+<'row'<'col-sm-12'tr>>"+"<'row'<'col-sm-5'i><'col-sm-7'p>>";$('#example').DataTable({dom: domCfg});});

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"><link rel="stylesheet" href="https://cdn.datatables.net/1.10.20/css/dataTables.bootstrap.min.css"><script src="https://code.jquery.com/jquery-3.3.1.js"></script><script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script><script src="https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap.min.js"></script><div class="container-fluid"><table id="example" class="table table-striped table-bordered" style="width:100%"><头><tr><th>姓名</th><th>位置</th><th>办公室</th><th>年龄</th></tr></thead><tr><td>老虎尼克松</td><td>系统架构师</td><td>爱丁堡</td><td>61</td></tr><tr><td>加勒特·温特斯</td><td>会计师</td><td>东京</td><td>63</td></tr><tr><td>阿什顿考克斯</td><td>初级技术作者</td><td>旧金山</td><td>66</td></tr></tbody>

设置 2:

"<'row'<'col-sm-6'<'pull-left'l><'pull-left'f><'col-sm-6'>>>+<'row'<'col-sm-12'tr>>"+<'row'<'col-sm-5'i<'col-sm-7'p>"

这将被转述到下一个html:

<div class="col-sm-6"><div class="pull-left">l</div><div class="pull-left">f</div>

<div class="col-sm-6"></div>

<div class="row"><div class="col-sm-12">tr</div>

<div class="row"><div class="col-sm-5">i</div><div class="col-sm-7">p</div>

Bootstrap 3 中的示例:

$(document).ready(function(){var domCfg = "<'row'<'col-sm-6'<'pull-left'l><'pull-left'f><'col-sm-6'>;"+<'row'<'col-sm-12'tr>>"+"<'row'<'col-sm-5'i><'col-sm-7'p>>";$('#example').DataTable({dom: domCfg});});

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"><link rel="stylesheet" href="https://cdn.datatables.net/1.10.20/css/dataTables.bootstrap.min.css"><script src="https://code.jquery.com/jquery-3.3.1.js"></script><script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script><script src="https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap.min.js"></script><div class="container-fluid"><table id="example" class="table table-striped table-bordered" style="width:100%"><头><tr><th>姓名</th><th>位置</th><th>办公室</th><th>年龄</th></tr></thead><tr><td>老虎尼克松</td><td>系统架构师</td><td>爱丁堡</td><td>61</td></tr><tr><td>加勒特·温特斯</td><td>会计师</td><td>东京</td><td>63</td></tr><tr><td>阿什顿考克斯</td><td>初级技术作者</td><td>旧金山</td><td>66</td></tr></tbody>

最后,如示例所示,要应用自定义设置,只需在插件初始化时在 dom 选项上推送一个值,如下所示:

var domSetup = "<'row'<'col-sm-3'l><'col-sm-3'f><'col-sm-6'>>;"+<'row'<'col-sm-12'tr>>"+<'row'<'col-sm-5'i<'col-sm-7'p>";$('#myTable').dataTable({/* 其他初始化选项 */...,dom":domSetup});

注意,您可以对 dom 选项进行完全显示自定义管理,希望本指南对您有所帮助.

I want to move my search bar just next to the position of Show entries, that is, to the left position. I have this CSS style code applied to the filter input:

.dataTables_filter {
    width: 50%;
    float: left;
    text-align: left;
}

It did move to left but not as expected, it just moved a bit to the left, in the center. Like in the next image:

Any idea how to fix this? Another acceptable solution is to just exchange the position of Show entries and Search. If that is possible, how can I do that?

Regarding my code, I have done nothing special and I only imported the library directly from the datatables API. All I have is just a simple HTML code for displaying a table.

解决方案

Since you are using Bootstrap, I will explain how to work with the display of elements related to the datatables plugin. Datatables provides a way to configuring the dom, in other words, it let you configure how to display the different elements that form part of the table. These are identified like this:

The built-in table control elements in DataTables are:

l - length changing input control
f - filtering input
t - The table!
i - Table information summary
p - pagination control
r - processing display element

And the default dom configuration for Bootstrap on Datatables are the next ones:

Bootstrap 3:

"<'row'<'col-sm-6'l><'col-sm-6'f>>" +
"<'row'<'col-sm-12'tr>>" +
"<'row'<'col-sm-5'i><'col-sm-7'p>>"

Bootstrap 4:

"<'row'<'col-sm-12 col-md-6'l><'col-sm-12 col-md-6'f>>" +
"<'row'<'col-sm-12'tr>>" +
"<'row'<'col-sm-12 col-md-5'i><'col-sm-12 col-md-7'p>>"

Now, the setup of the dom for Bootstrap 3 is then traduced to the following html markup:

<div class="row">
  <div class="col-sm-6">l</div>
  <div class="col-sm-6">f</div>
</div>
<div class="row">
  <div class="col-sm-12">tr</div>
</div>
<div class="row">
  <div class="col-sm-5">i</div>
  <div class="col-sm-7">p</div>
</div>

That is why when you used the following CSS style on the filtering control it just goes to the middle of the page (note the filtering control f is wrapped inside a col-sm-6 element):

.dataTables_filter {
  width: 50%;
  float: left;
  text-align: left;
}

The good thing is that you can change this setup on Datatables initialization using the dom option. In your particular case, you can try out the next two setups, however, I invite you to play a little to found another one more convincing to you::

Setup 1:

"<'row'<'col-sm-3'l><'col-sm-3'f><'col-sm-6'>>" +
"<'row'<'col-sm-12'tr>>" +
"<'row'<'col-sm-5'i><'col-sm-7'p>>"

That will be traduced to next html:

<div class="row">
  <div class="col-sm-3">l</div>
  <div class="col-sm-3">f</div>
  <div class="col-sm-6"></div>
</div>
<div class="row">
  <div class="col-sm-12">tr</div>
</div>
<div class="row">
  <div class="col-sm-5">i</div>
  <div class="col-sm-7">p</div>
</div>

Example in Bootstrap 3:

$(document).ready(function()
{
    var domCfg = "<'row'<'col-sm-3'l><'col-sm-3'f><'col-sm-6'>>" +
                 "<'row'<'col-sm-12'tr>>" +
                 "<'row'<'col-sm-5'i><'col-sm-7'p>>";

    $('#example').DataTable({
        dom: domCfg
    });
});

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.20/css/dataTables.bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap.min.js"></script>

<div class="container-fluid">
<table id="example" class="table table-striped table-bordered" style="width:100%">
  <thead>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Tiger Nixon</td>
      <td>System Architect</td>
      <td>Edinburgh</td>
      <td>61</td>
    </tr>
    <tr>
      <td>Garrett Winters</td>
      <td>Accountant</td>
      <td>Tokyo</td>
      <td>63</td>
    </tr>
    <tr>
      <td>Ashton Cox</td>
      <td>Junior Technical Author</td>
      <td>San Francisco</td>
      <td>66</td>
    </tr>
  </tbody>
</table>
</div>

Setup 2:

"<'row'<'col-sm-6'<'pull-left'l><'pull-left'f>><'col-sm-6'>>" +
"<'row'<'col-sm-12'tr>>" +
"<'row'<'col-sm-5'i><'col-sm-7'p>>"

That will be traduced to next html:

<div class="row">
  <div class="col-sm-6">
    <div class="pull-left">l</div>
    <div class="pull-left">f</div>
  </div>
  <div class="col-sm-6"></div>
</div>
<div class="row">
  <div class="col-sm-12">tr</div>
</div>
<div class="row">
  <div class="col-sm-5">i</div>
  <div class="col-sm-7">p</div>
</div>

Example in Bootstrap 3:

$(document).ready(function()
{
    var domCfg = "<'row'<'col-sm-6'<'pull-left'l><'pull-left'f>><'col-sm-6'>>" +
                 "<'row'<'col-sm-12'tr>>" +
                 "<'row'<'col-sm-5'i><'col-sm-7'p>>";

    $('#example').DataTable({
        dom: domCfg
    });
});

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.20/css/dataTables.bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap.min.js"></script>

<div class="container-fluid">
<table id="example" class="table table-striped table-bordered" style="width:100%">
  <thead>
    <tr>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Tiger Nixon</td>
      <td>System Architect</td>
      <td>Edinburgh</td>
      <td>61</td>
    </tr>
    <tr>
      <td>Garrett Winters</td>
      <td>Accountant</td>
      <td>Tokyo</td>
      <td>63</td>
    </tr>
    <tr>
      <td>Ashton Cox</td>
      <td>Junior Technical Author</td>
      <td>San Francisco</td>
      <td>66</td>
    </tr>
  </tbody>
</table>
</div>

Finally, as shown on the examples, and to apply a custom setup, just push a value on the dom option at the plugin initialization, like this:

var domSetup = "<'row'<'col-sm-3'l><'col-sm-3'f><'col-sm-6'>>" +
               "<'row'<'col-sm-12'tr>>" +
               "<'row'<'col-sm-5'i><'col-sm-7'p>>";

$('#myTable').dataTable({
    /* Other initialization options */
    ...,
    "dom": domSetup
});

Note, you can do a fully display customization managing the dom option, I hope this guide helps you.

这篇关于Boostrap 3中Datatables插件的显示格式自定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆