简体   繁体   English

Symfony 5 + Webpack Encore + 数据表:未显示按钮

[英]Symfony 5 + Webpack Encore + Datatables: buttons not being displayed

This is my second failed attempt to use a library to export a simple table to any kind of document (Excel, PDF, etc.).这是我第二次尝试使用库将简单表格导出到任何类型的文档(Excel、PDF 等),但失败了。

This time I am using DataTables .这次我使用DataTables The table is being displayed and everything seems good, but no button for exporting, copying or anything is displayed.该表正在显示,一切似乎都很好,但没有显示用于导出、复制或任何内容的按钮。

As you can see there are no export buttons anywhere.如您所见,任何地方都没有导出按钮。 There are also no warnings or errors in the developer console.开发者控制台中也没有警告或错误。

These are the steps I followed in order to use DataTables in my Symfony 5 project:这些是我在 Symfony 5 项目中使用 DataTables 所遵循的步骤:

  1. Add the CDN links to the base template from the download page with the following options:使用以下选项将 CDN 链接从下载页面添加到基本模板:

    • Styling Framework : Bootstrap 4样式框架:Bootstrap 4
    • Packages :
      • DataTables数据表
    • Extensions :扩展
      • Buttons, HTML5 Export, JSZip, pdfmake按钮,HTML5 导出,JSZip,pdfmake
      • Print view打印视图
      • Responsive响应式
  2. Instantiate the DataTable to create the table.实例化 DataTable 以创建表。 This is how the final JS looks like in the developer console:这是最终 JS 在开发者控制台中的样子:


$(document).ready(function() {
    $('.table').DataTable({
        ajax: {
            url: "/api/inventory_items.json",
            dataSrc: ''
        },
        buttons: ['excel'],
        columns: [{
                data: 'center.name',
                title: 'Centro',
                defaultContent: 'N/A',
            },
            {
                data: 'program.name',
                title: 'Programa',
                defaultContent: 'N/A',
            },
            {
                data: 'description',
                title: 'Descripción',
                defaultContent: 'N/A',
            },
            {
                data: 'comment',
                title: 'Comentario',
                defaultContent: 'N/A',
            },
            {
                data: 'item_condition',
                title: 'Condición',
                defaultContent: 'N/A',
            },
            {
                data: 'age',
                title: 'Antigüedad',
                defaultContent: 'N/A',
            },
            {
                data: 'purchase_price',
                title: 'Precio de compra',
                defaultContent: 'N/A',
            },
            {
                data: 'purchase_currency',
                title: 'Moneda de compra',
                defaultContent: 'N/A',
            },
            {
                data: 'sn',
                title: 'S/N',
                defaultContent: 'N/A',
            },
            {
                data: 'physical_location',
                title: 'Ubicación Física',
                defaultContent: 'N/A',
            },
            {
                data: 'date',
                title: 'Fecha',
                defaultContent: 'N/A',
            },
            {
                data: 'ref',
                title: 'Documento de referencia',
                defaultContent: 'N/A',
            },
            {
                data: 'status',
                title: 'Estado',
                defaultContent: 'N/A',
            },
            {
                data: 'os',
                title: 'Sistema Operativo',
                defaultContent: 'N/A',
            },
            {
                data: 'cpu',
                title: 'Procesador',
                defaultContent: 'N/A',
            },
            {
                data: 'type',
                title: 'Tipo de procesador',
                defaultContent: 'N/A',
            },
            {
                data: 'hdd',
                title: 'Disco duro',
                defaultContent: 'N/A',
            },
            {
                data: 'ram',
                title: 'Memoria RAM',
                defaultContent: 'N/A',
            }
        ],
        responsive: true,
        language: {
            url: "//cdn.datatables.net/plug-ins/1.10.21/i18n/Spanish.json"
        },
    })
})

This is how my Twig table base template looks like:这是我的 Twig 表基本模板的样子:

{% extends 'base.html.twig' %}

{% set bodyTitle %}
    {% block setBodyTitle %}{% endblock %}
{% endset %}

{% block title %}
    {{ parent() }} | {{ bodyTitle }}
{% endblock %}

{% block body %}
    <div class="container{% block fluid%}{% endblock%} mt-3">
        <div class="row">
            <div class="col">
                <h1>{{ bodyTitle }} <a class="btn btn-outline-success" href="{% block bodyCreateUrl %}{% endblock %}">➕</a></h1>
            </div>
        </div>
        <div class="row">
            <div class="col">
                <table class="table"></table>
            </div>
        </div>
    </div>
{% endblock %}

{% block stylesheets %}
    {{ parent() }}
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/bs4/jszip-2.5.0/dt-1.10.21/b-1.6.2/b-colvis-1.6.2/b-html5-1.6.2/b-print-1.6.2/cr-1.5.2/r-2.2.5/datatables.min.css"/>
{% endblock %}

{% block javascripts %}
    {{ parent() }}
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/v/bs4/jszip-2.5.0/dt-1.10.21/b-1.6.2/b-colvis-1.6.2/b-html5-1.6.2/b-print-1.6.2/sp-1.1.1/datatables.min.js"></script>


    <script>
        $(document).ready(function() {
            $('.table').DataTable({
                ajax: {
                    url: "/api/{% block jsApi %}{% endblock %}.json",
                    dataSrc: ''
                },
                buttons: [ 'excel' ],
                columns: [{% block jsTableColumns %}{% endblock %}],
                responsive: true,
                language: {
                    url: "//cdn.datatables.net/plug-ins/1.10.21/i18n/Spanish.json"
                },
            })
        })
    </script>
{% endblock %}

Despite everything I did was on my Symfony project I have tried to replicate the frontend on a snippet:尽管我所做的一切都是在我的 Symfony 项目上,但我试图在一个片段上复制前端:

 var fakeApiData = [{ id: 1, program: { id: 1, name: 'Some place' }, description: 'fgdfg', comment: 'dfgdfg', item_condition: 'New', location: { id: 2, name: 'Some place' }, fund_source: { id: 1, name: 'Donation' }, age: '1', purchase_price: 1200, purchase_currency: 'US$', physical_location: 'Somewhere', date: '2020-07-05T00:00:00+02:00', status: 'ok', os: 'Windows 10', ram: 4, cpu: 'I3 3.2GHz', type: 'x64', hdd: '320', center: { id: 1, name: 'Some place' } }]; $(document).ready(function() { $('.table').DataTable({ data: fakeApiData, buttons: ['excel', 'copy' ], columns: [{ data: 'center.name', title: 'Centro', defaultContent: 'N/A', }, { data: 'program.name', title: 'Programa', defaultContent: 'N/A', }, { data: 'description', title: 'Descripción', defaultContent: 'N/A', }, { data: 'comment', title: 'Comentario', defaultContent: 'N/A', }, { data: 'item_condition', title: 'Condición', defaultContent: 'N/A', }, { data: 'age', title: 'Antigüedad', defaultContent: 'N/A', }, { data: 'purchase_price', title: 'Precio de compra', defaultContent: 'N/A', }, { data: 'purchase_currency', title: 'Moneda de compra', defaultContent: 'N/A', }, { data: 'sn', title: 'S/N', defaultContent: 'N/A', }, { data: 'physical_location', title: 'Ubicación Física', defaultContent: 'N/A', }, { data: 'date', title: 'Fecha', defaultContent: 'N/A', }, { data: 'ref', title: 'Documento de referencia', defaultContent: 'N/A', }, { data: 'status', title: 'Estado', defaultContent: 'N/A', }, { data: 'os', title: 'Sistema Operativo', defaultContent: 'N/A', }, { data: 'cpu', title: 'Procesador', defaultContent: 'N/A', }, { data: 'type', title: 'Tipo de procesador', defaultContent: 'N/A', }, { data: 'hdd', title: 'Disco duro', defaultContent: 'N/A', }, { data: 'ram', title: 'Memoria RAM', defaultContent: 'N/A', } ], responsive: true, language: { url: "//cdn.datatables.net/plug-ins/1.10.21/i18n/Spanish.json" }, }) })
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/> <link href="https://cdn.datatables.net/v/bs4/jszip-2.5.0/dt-1.10.21/b-1.6.2/b-html5-1.6.2/b-print-1.6.2/r-2.2.5/datatables.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js"></script> <script src="https://cdn.datatables.net/v/bs4/jszip-2.5.0/dt-1.10.21/b-1.6.2/b-html5-1.6.2/b-print-1.6.2/r-2.2.5/datatables.min.js"></script> <div class="row"> <div class="col"> <table class="table"></table> </div> </div>

In the snippet, the left button that expands information regarding "Centro" column is not present in my table, as you can appreciate...在片段中,我的表格中没有展开有关“Centro”列信息的左侧按钮,您可以欣赏...

I have changed DataTables option ajax for data to "call" some fake data (similar to my server) in order to make a test.我已更改 DataTables 选项ajax以“调用”一些假data (类似于我的服务器)以进行测试。

But anyway, here the export buttons are not being displayed either.但无论如何,这里也没有显示导出按钮。 What am I missing?我错过了什么?

You are instantiating DataTables twice it has to be instantiated only once您正在实例化 DataTables 两次,它只需要实例化一次

add this code inside your datatables function it will allow you to copy/csv/pdf...在您的数据表 function 中添加此代码,它将允许您复制/csv/pdf ...

$('.table').DataTable({
    dom: 'B<"clear">lfrtip',
    buttons: {
        name: 'primary',
       buttons: [ 'copy', 'csv', 'excel', 'pdf' ]
        }}       
    ); 

here is a jsfiddle of a working example这是一个工作示例的jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM