简体   繁体   English

有没有一种方法可以从多个SQL表中提取一个DataTables表?

[英]Is there a way to pull from multiple SQL tables for one DataTables table?

I use DataTables to present data in a reporting website that I created. 我使用DataTables在我创建的报告网站中显示数据。 I am working on creating a Report Builder where users can select multiple tables and then columns from those tables for a custom report. 我正在创建一个报表生成器,用户可以在其中选择多个表,然后从这些表中选择列以创建自定义报表。

What I want to know is whether there's a way to do this with DataTables. 我想知道的是,是否有一种方法可以使用DataTables做到这一点。 I'm able to get the tables names and column names for the custom report, but I've not been able to figure out how to send it to DataTables. 我可以获取自定义报告的表名和列名,但是还无法弄清楚如何将其发送到DataTables。 I currently use server side processing and send an ajax call using POST to the DataTable. 我目前使用服务器端处理,并使用POSTajax调用发送到DataTable。

I know that I can program in a SQL query based on the selected tables and columns, but I cannot seem to figure out how to have the data sent to DataTables. 我知道我可以根据所选的表和列在SQL查询中进行编程,但似乎无法弄清楚如何将数据发送到DataTables。

Here is how I initialize my DataTables: 这是我初始化数据表的方式:

$(document).ready(function ()
{
    // Setup - add a text input to each footer cell
    $('#DataTable tfoot th').each(function ()           //creates the search bar as the footer
    {
        var title = $(this).text();
        $(this).html('<input type="text" placeholder="Search ' + title + '" />');
    });

    var table = $('#DataTable').DataTable({
        "lengthMenu": [[25, 50, 75, 100, 150, -1], [25, 50, 75, 100, 150, 'All']],
        "dom": '<"top"Bifpl<"clear">>rt<"bottom"ip<"clear">>',
        "buttons": [{
            extend: 'collection',
            text: 'Export',
            buttons: ['export', { extend: 'csv',
                text: 'Export All To CSV',              //Export all to CSV file
                action: function (e, dt, node, config)
                {
                    window.location.href = './ServerSide.php?ExportToCSV=Yes';
                }
            }, 'csv', 'pdf', { extend: 'excel',
                text: 'Export Current Page',            //Export to Excel only the current page and highlight the first row as headers
                exportOptions: {
                    modifier: {
                        page: 'current'
                    }
                },
                customize: function (xlsx)
                {
                    var sheet = xlsx.xl.worksheets['sheet1.xml'];
                    $('row:first c', sheet).attr('s', '7');
                }
            }]
        }
        ],
        "fixedHeader": {                                //Keeps the header and footer visiable at all times
            header: true,
            footer: true
        },
        "select": true,                                 //sets the ability to select rows
        "processing": true,                             //shows the "Processing" when working
        "serverSide": true,                             //sends data to the server for processing
        "ajax": {                                       //where the data is sent and processed
            "url": "./ServerSide.php",
            "type": "POST"
        },
        stateSave: true,                                //Saves the current state of the page
        columnDefs: [{ visible: false, targets: 0}],    //Hides the first column the ID column
        initComplete: function ()                       //sets the search
        {
            var api = this.api();

            // Apply the search
            api.columns().every(function ()
            {
                var that = this;

                $('input', this.footer()).on('keyup change', function (e)
                {
                    if (that.search() !== this.value & e.keyCode == 13) //you have to hit enter for the search to start
                    {
                        that
                          .search(this.value)
                          .draw();
                    }
                });
            });
        }
    });
});

$.fn.dataTable.ext.buttons.export =
{
    className: 'buttons-alert',                         //Adds the "Export all to Excel" button
    id: 'ExportButton',
    text: "Export All To Excel",
    action: function (e, dt, node, config)
    {
        window.location.href = './ServerSide.php?ExportToExcel=Yes';
    }
};

Here is what I can currently get to work: 这是我目前可以开始工作的内容: 在此处输入图片说明

I'm not sure else anyone would need to help me with this, but if I'm missing something let me know and I'll add it. 我不确定其他人是否需要帮助我,但是如果我丢失了某些信息,请告诉我,然后将其添加。

I need all the data from all selected tables and columns to be in one DataTables table presented on the website. 我需要所有选择的表和列中的所有数据都放在网站上显示的一个DataTables表中。 In the image above I'm showing that I've gotten as far as getting the column headers and using aliases for the tables. 在上面的图像中,我显示了我已经获得列标题并为表使用别名。 I'm working through my FilterSort.class.pph file (which is like the ssp.class.php in DataTables) to see if I can get it to present the table. 我正在遍历我的FilterSort.class.pph文件(类似于DataTables中的ssp.class.php ),以查看是否可以显示该表。

I figured it out. 我想到了。 I was still calling the correct DataTables functions, but I wasn't passing the data to the functions. 我仍在调用正确的DataTables函数,但没有将数据传递给这些函数。 I ended up having to update my ServerSide.php file and my FilterSort.class.php file. 我最后不得不更新我的ServerSide.php文件和我的FilterSort.class.php文件。 Those are the ones that all of the other reports use to send data from the server to the screen. 这些是所有其他报表用于将数据从服务器发送到屏幕的报表。 After some trial and error, I got it to work and didn't need to change anything in the code posted in my question. 经过一番尝试和错误之后,我开始使用它,不需要更改问题中发布的代码中的任何内容。

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

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