简体   繁体   English

使用Jquery Datatables在表内的表上显示数据时出现问题

[英]Issue displaying data on a table inside a table using Jquery Datatables

Am working on an Laravel application whereby am fetching data from the backend, on the frontend am displaying it dynamically on a table using Javascript which works fine. 我正在一个Laravel应用程序上工作,该应用程序从后端获取数据,而在前端则使用效果很好的Javascript在表上动态显示数据。 But when I check on the browser all the data appears on a horizontal manner. 但是当我在浏览器上检查所有数据时,它们都是水平显示的。 Since the data is large, I would like to display it in a vertical manner inside Jquery datatables (so that the user can choose how many inputs he wants displayed per page). 由于数据很大,因此我想在Jquery数据表中以垂直方式显示它(以便用户可以选择他希望每页显示多少个输入)。

Javascript code JavaScript代码

<script>
<?php if(isset($ag)){ ?>
       var data;
        $( document ).ready(function() {
            data = {!! json_encode($ag) !!};
        });
        $(document).on("mouseenter", "a", function() {
            var policies = '';
            var agentId = $(this).attr('id');
            for(var i = 0; i < data.length; i++) {
                if(agentId == data[i]['agent_no']) {
                    for(var j = 0; j < data[i]['policies'].length; j++){
                        policies += '<td>' + data[i]['policies'][j] + '</td>' + '<br>';
                    }
                }
            }
            //console.log(policies);
            $('#summary-table tbody tr').html(policies);
        });
    <?php } ?>
</script>

Table that am displaying the data dynamically 动态显示数据的表

<table class="table table-hover mg-b-0 tx-11" id="summary-table">
    <thead>
    <tr>
        <th>POLICIES</th>
    </tr>
    </thead>
    <tbody>
        <tr> <!-- Add policies dynamically via AJAX --></tr> 
    </tbody>
</table>

Your table has only one column, so either you set the Table head to the number of column you will be displaying, or you adjust the colspan attribute. 您的表只有一列,因此您可以将Table head设置为将要显示的列数,或者调整colspan属性。

<script>
<?php if(isset($ag)){ ?>
    var data;
    $( document ).ready(function() {
        data = {!! json_encode($ag) !!};
    });
    $(document).on("mouseenter", "a", function() {
        var policies = '';
        var agentId = $(this).attr('id');
        for(var i = 0; i < data.length; i++) {
            if(agentId == data[i]['agent_no']) {
                for(var j = 0; j < data[i]['policies'].length; j++){
                    policies += '<td>' + data[i]['policies'][j] + '</td>' + '<br>';
                }
            }
        }
        //console.log(policies);
        $('#summaru-table thead th').attr('colspan', data.length); //adjust the colspan
        $('#summary-table tbody tr').html(policies);
    });
<?php } ?>
</script>

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

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