简体   繁体   English

jQuery dataTable在bootstrap模式中溢出

[英]jQuery dataTable overflows in bootstrap modal

I have a table inside a bootstrap modal. 我在bootstrap模式中有一个表。 The content that I populate inside the dataTable cells is quite large, and instead of wrapping the text to fit the table inside the modal, it overflows the modal, like it is unable to pick up the modal width and wrap the text. 我在dataTable单元格中填充的内容非常大,而不是包装文本以适应模态内的表格,它会溢出模态,就像它无法获取模态宽度并包装文本一样。

Screenshot: 截图:

在此输入图像描述

I have tried various solutions, such as specifying wrap CSS as well as specifying the table width (in % and px) and setting the width property on the table (in % and px) but there is absolutely no change to the table. 我尝试了各种解决方案,例如指定包装CSS以及指定表格宽度(以%和px为单位)和在表格上设置width属性(以%和px为单位),但表格绝对没有变化。 Any advice on how to correct this issue would be greatly appreciated. 如何纠正这个问题的任何建议将不胜感激。

Code Extract 1 (Modal): 代码提取1(模态):

<!-- List Questions Modal -->
<div class="modal fade" id="questionsModal" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog" style="width: 95%">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4>Questions</h4>
        </div>
        <div class="modal-body">
            <div class="row">
            <div class="responsive-table">
                <table width="900px" class="table table-bordered" style="margin-bottom:2em; width: 900px;" id="questionsTable">
                    <thead>
                        <tr>
                            <th >Code</th>
                            <th>Title</th>
                            <th>Instruction</th>
                            <th>Extract</th>
                            <th>class="text-center">Active</th>
                            <th class="text-center">Edit</th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

Code Extract 2 (dataTable population): 代码提取2(dataTable人口):

function showQuestions(quiz_id)
{
$('#questions_quiz_id').val('quiz_id');
window.questionsTable.fnClearTable();
$.post('{{ url("getGameQuestions") }}', {quiz_id : quiz_id})
.done(function(data)
{
    if (typeof(data.error) != 'undefined')
    {
        $('#error-msg').html(data.error);
        $('#page-error').fadeIn(300).delay(2000).fadeOut(500);
    }
    else
    {
        for(var d in data)
        {
            var question = data[d]; 
            var active = (question.active == undefined || question.active == false) ? "Inactive" : "Active";

            var ai = window.questionsTable.fnAddData([
                question.code,
                question.title,
                question.instruction,
                question.extract,
                active,
                '<i class="icon-pencil" style="cursor:pointer; color:#E48A07;" title="Edit" onclick="setQuestion('+question.id+')"></i>'
            ]);
            var oSettings = window.questionsTable.fnSettings();
            var addedRow = oSettings.aoData[ai[0]].nTr;
            addedRow.cells.item(2).setAttribute('width','29%');
            addedRow.cells.item(4).className = "text-center";
            addedRow.cells.item(4).className = "text-center";
            addedRow.cells.item(5).className = "text-center";
        }
        $('#questionsModal').modal('show');
    }
});
}

Try removing 尝试删除

<div class="responsive-table"> it might be causing responsive widths in the modal to fail. <div class="responsive-table">它可能导致模态中的响应宽度失败。

You could also try specifying a max-width on the table . 您也可以尝试在表格上指定最大宽度

This might be slightly related although it was a problem with Bootstrap 2.3.2 and it looks like you are using 3. 虽然Bootstrap 2.3.2存在问题但看起来你使用的是3,但这可能会略有相关。

http://www.bootply.com/88364 http://www.bootply.com/88364

This question is asked here datatables does not becomes responsive on bootstrap3 modal dialog 这里提出这个问题, datatables在bootstrap3模态对话框中没有响应

I would suggest you enable the Datatables Responsive extension, ditch the wrapper and use this snippet of code instead. 我建议你启用Datatables Responsive扩展,放弃包装并使用这段代码代替。

//once the modal has been shown
$('#yourModal').on('shown.bs.modal', function() {
           //Get the datatable which has previously been initialized
            var dataTable= $('#yourResponsiveDataTableInModal').DataTable();
            //recalculate the dimensions
            dataTable.columns.adjust().responsive.recalc();

        });

After trying so many solutions i found one who works for me! 在尝试了这么多解决方案后,我找到了一个为我工作的人!

Just put the table inside a div and define the CSS inside the code. 只需将表放在div中并在代码中定义CSS即可。

<div style="overflow:auto;">
<table>
      .....
</table>
</div>

I hope this help!! 我希望这有帮助!!

Remove the <div class="row"> and check. 删除<div class="row">并检查。 Am also experienced same problem,after i removed the div, i got it perfectly. 我也遇到了同样的问题,在我删除了div后,我完全理解了它。

After trying so many solutions i found a solution. 在尝试了这么多解决方案后,我找到了一个解

Just put the table inside a div and define the CSS inside the code. 只需将表放在div中并在代码中定义CSS即可。

<div style="overflow:auto;">
<table>
      .....
</table>
</div>

I hope this help!! 我希望这有帮助!!

<div class="modal-body">
    <div class="row table-responsive">
        <div class="col-sm-12">
            <table id="example" class="table table-striped table-bordered table-hover">
                <thead>
                    <tr>
                        <th class="site_name">
                            Feedback
                        </th>
                        <th>
                            Ticket Id
                        </th>
                        <th>
                            Date of Creation
                        </th>
                        <th>
                            Classification
                        </th>
                        <th>
                            Topic
                        </th>
                        <th></th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>
</div>

initialize the datatable like that initialize the datatable when open the modal.first time when we open the modal i==1 second thime i==2 so datatble cant reinitalize again and again does not give any alert box like datatable already initalize 初始化数据表就像初始化数据表时打开modal.first时我们打开模态i == 1秒thime i == 2所以datatble不能再次启动再次没有给出像datatable已经初始化的任何警告框

var i=1;
$('#myModal').on('shown.bs.modal', function (e) {
if(i==1){
     var oTable=$('#questionsTable').DataTable();
 }
 i++;
});  

Just insert your table inside a div with "table-responsive" class: 只需将表格插入带有“表格响应”类的div中:

<div class="table-responsive">
  //Your table here
</div>

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

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