DataTable Responsive 在 Bootstrap 5 中删除列。

huangapple go评论63阅读模式
英文:

DataTable Responsive deletes columns with Bootstrap 5

问题

我想使用DataTable 1.13.4Bootstrap 5.2.3使表格具有响应性,但是当我在移动设备上测试我的表格时(使用Firefox,而不是真实设备),我的表格包含一个小的水平滚动条,但移除了列。

就像在DataTable的官方文档中所解释的,我使用Yarn安装了两个依赖项:

  • datatables.net-bs5(用于Bootstrap 5的DataTable)
  • datatables.net-responsive-bs5(用于启用Bootstrap 5响应式的插件)

我知道我需要在声明表格之前添加Bootstrap 5的table-responsive类,但我想要固定分页和筛选,所以当我实例化DataTable对象时,我将dom选项构建到我的JavaScript中(dom: 'f<"table-responsive"t>...')。

点击此处在桌面屏幕上查看结果
点击此处在移动屏幕上查看结果

水平滚动条在截图上不可见,但在桌面和移动屏幕上都存在。它只是非常小(大约5/10像素),不允许在移动屏幕上看到桌面屏幕上存在的Description列。

我的JavaScript文件:

import {Controller} from '@hotwired/stimulus';

import DataTable from "datatables.net-bs5";
import "datatables.net-responsive-bs5";

export default class extends Controller {
    connect() {
        new DataTable('#tableMenu', {
            pageLength: 5,
            responsive: true,
            dom: 'f<"table-responsive"t><"row"<"col-sm-12 col-lg-6 mb-3 mb-lg-0"i><"col-sm-12 col-lg-6 d-lg-flex justify-content-lg-end"p>>'
        });
    }
}

我的HTML文件:

<table id="tableMenu" class="table table-striped">
    <thead>
    <tr>
        <th></th>
        <th>Titre</th>
        <th>Prix</th>
        <th>Description</th>
    </tr>
    </thead>
    <tbody>
    {% for menu in menus %}
        <tr>
            <td>
                <a href="{{ path('app.admin.menu.edit', {'id': menu.id}) }}" class="btn btn-primary">
                    <i class="fa fa-pen" style="font-size: 10px;"></i>
                </a>
                <button type="button"
                        id="delete-menu"
                        data-delete-url="{{ path('api.menu.delete', {'id': menu.id}) }}"
                        class="btn btn-danger">
                    <i class="fa fa-xmark" style="font-size: 10px;"></i>
                </button>
            </td>
            <td>{{ menu.title }}</td>
            <td>{{ menu.price / 100 }}€</td>
            <td>{{ menu.description }}</td>
        </tr>
    {% endfor %}
    <tfoot>
    <tr>
        <th></th>
        <th>Titre</th>
        <th>Prix</th>
        <th>Description</th>
    </tr>
    </tfoot>
</table>

感谢大家的帮助! 🎉

英文:

I want to make a table responsive with DataTable 1.13.4 and Bootstrap 5.2.3 but when I test my table on mobile device (with Firefox, not on a real device) my table contains a small horizontal scrollbar but removes columns.

Like explained in the official documentation of DataTable, I have install two depedencies with Yarn :

  • datatables.net-bs5 (DataTable for Bootstrap 5)
  • datatables.net-responsive-bs5 (Plugin to enable responsive for Bootstrap 5)

I know I need to add the Bootstrap 5 table-responsive class before declaring my table, but I want to fixed the pagination and the filter, so I've built the dom option into my JavaScript when I instantiate the DataTable object (dom: &#39;f&lt;&quot;table-responsive&quot;t&gt;...)

Click here to see the result on desktop screen
Click here to see the result on mobile screen

The horizontal scroll bar is not visible on screenshots, but is present on desktop and mobile screen. It's just very small (like 5/10 pixels) and doesn't allow to see the Description column on mobile screen which is present in the desktop screen.

My JavaScript file :

import {Controller} from &#39;@hotwired/stimulus&#39;;

import DataTable from &quot;datatables.net-bs5&quot;;
import &quot;datatables.net-responsive-bs5&quot;;

export default class extends Controller {
    connect() {
        new DataTable(&#39;#tableMenu&#39;, {
            pageLength: 5,
            responsive: true,
            dom: &#39;f&lt;&quot;table-responsive&quot;t&gt;&lt;&quot;row&quot;&lt;&quot;col-sm-12 col-lg-6 mb-3 mb-lg-0&quot;i&gt;&lt;&quot;col-sm-12 col-lg-6 d-lg-flex justify-content-lg-end&quot;p&gt;&gt;&#39;
        });
    }
}

My HTML file :

&lt;table id=&quot;tableMenu&quot; class=&quot;table table-striped&quot;&gt;
    &lt;thead&gt;
    &lt;tr&gt;
        &lt;th&gt;&lt;/th&gt;
        &lt;th&gt;Titre&lt;/th&gt;
        &lt;th&gt;Prix&lt;/th&gt;
        &lt;th&gt;Description&lt;/th&gt;
    &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
    {% for menu in menus %}
        &lt;tr&gt;
            &lt;td&gt;
                &lt;a href=&quot;{{ path(&#39;app.admin.menu.edit&#39;, {&#39;id&#39;: menu.id}) }}&quot; class=&quot;btn btn-primary&quot;&gt;
                    &lt;i class=&quot;fa fa-pen&quot; style=&quot;font-size: 10px;&quot;&gt;&lt;/i&gt;
                &lt;/a&gt;
                &lt;button type=&quot;button&quot;
                        id=&quot;delete-menu&quot;
                        data-delete-url=&quot;{{ path(&#39;api.menu.delete&#39;, {&#39;id&#39;: menu.id}) }}&quot;
                        class=&quot;btn btn-danger&quot;&gt;
                    &lt;i class=&quot;fa fa-xmark&quot; style=&quot;font-size: 10px;&quot;&gt;&lt;/i&gt;
                &lt;/button&gt;
            &lt;/td&gt;
            &lt;td&gt;{{ menu.title }}&lt;/td&gt;
            &lt;td&gt;{{ menu.price / 100 }}€&lt;/td&gt;
            &lt;td&gt;{{ menu.description }}&lt;/td&gt;
        &lt;/tr&gt;
    {% endfor %}
    &lt;tfoot&gt;
    &lt;tr&gt;
        &lt;th&gt;&lt;/th&gt;
        &lt;th&gt;Titre&lt;/th&gt;
        &lt;th&gt;Prix&lt;/th&gt;
        &lt;th&gt;Description&lt;/th&gt;
    &lt;/tr&gt;
    &lt;/tfoot&gt;
&lt;/table&gt;

Thanks to all for your help ! 🫶

答案1

得分: 1

经过许多研究,我找到了一个解决方案。

我保持了与原始问题相同的代码,只是在我的<tr>属性上添加了两个类:

  • overflow-hidden
  • text-nowrap

因此,我的JavaScript文件没有任何更改
而我的HTML文件如下所示:

<table id="tableMenu" class="table table-striped">
    <thead>
    <tr>
        <th></th>
        <th>Titre</th>
        <th>Prix</th>
        <th>Description</th>
    </tr>
    </thead>
    <tbody>
    {% for menu in menus %}
        <tr class="overflow-hidden text-nowrap">
            <td>
                <a href="{{ path('app.admin.menu.edit', {'id': menu.id}) }}" class="btn btn-primary">
                    <i class="fa fa-pen" style="font-size: 10px;"></i>
                </a>
                <button type="button"
                        id="delete-menu"
                        data-delete-url="{{ path('api.menu.delete', {'id': menu.id}) }}"
                        class="btn btn-danger">
                    <i class="fa fa-xmark" style="font-size: 10px;"></i>
                </button>
            </td>
            <td>{{ menu.title }}</td>
            <td>{{ menu.price / 100 }}€</td>
            <td>{{ menu.description }}</td>
        </tr>
    {% endfor %}
    <tfoot>
    <tr>
        <th></th>
        <th>Titre</th>
        <th>Prix</th>
        <th>Description</th>
    </tr>
    </tfoot>
</table>
英文:

After many research, I have found a solution.

I have keep the same code like the original issue but juste added two classes on my &lt;tr&gt; attribute :

  • overflow-hidden
  • text-nowrap

So, my JavaScript file doesn't has any changes
And my HTML file looks like this :

&lt;table id=&quot;tableMenu&quot; class=&quot;table table-striped&quot;&gt;
    &lt;thead&gt;
    &lt;tr&gt;
        &lt;th&gt;&lt;/th&gt;
        &lt;th&gt;Titre&lt;/th&gt;
        &lt;th&gt;Prix&lt;/th&gt;
        &lt;th&gt;Description&lt;/th&gt;
    &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
    {% for menu in menus %}
        &lt;tr class=&quot;overflow-hidden text-nowrap&quot;&gt;
            &lt;td&gt;
                &lt;a href=&quot;{{ path(&#39;app.admin.menu.edit&#39;, {&#39;id&#39;: menu.id}) }}&quot; class=&quot;btn btn-primary&quot;&gt;
                    &lt;i class=&quot;fa fa-pen&quot; style=&quot;font-size: 10px;&quot;&gt;&lt;/i&gt;
                &lt;/a&gt;
                &lt;button type=&quot;button&quot;
                        id=&quot;delete-menu&quot;
                        data-delete-url=&quot;{{ path(&#39;api.menu.delete&#39;, {&#39;id&#39;: menu.id}) }}&quot;
                        class=&quot;btn btn-danger&quot;&gt;
                    &lt;i class=&quot;fa fa-xmark&quot; style=&quot;font-size: 10px;&quot;&gt;&lt;/i&gt;
                &lt;/button&gt;
            &lt;/td&gt;
            &lt;td&gt;{{ menu.title }}&lt;/td&gt;
            &lt;td&gt;{{ menu.price / 100 }}€&lt;/td&gt;
            &lt;td&gt;{{ menu.description }}&lt;/td&gt;
        &lt;/tr&gt;
    {% endfor %}
    &lt;tfoot&gt;
    &lt;tr&gt;
        &lt;th&gt;&lt;/th&gt;
        &lt;th&gt;Titre&lt;/th&gt;
        &lt;th&gt;Prix&lt;/th&gt;
        &lt;th&gt;Description&lt;/th&gt;
    &lt;/tr&gt;
    &lt;/tfoot&gt;
&lt;/table&gt;

huangapple
  • 本文由 发表于 2023年5月28日 17:25:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定