bootstrap responsive table content wrapping

前端 未结 10 1728
南旧
南旧 2020-12-24 11:10

I have HTML similar to this:

    
相关标签:
10条回答
  • 2020-12-24 11:34

    Add your new class "tableresp" with table-responisve class and then add below code in your js file

    $(".tableresp").on('click', '.dropdown-toggle', function(event) {
    
        if ($('.dropdown-menu').length) {
            var elm = $('.dropdown-menu'),
                docHeight = $(document).height(),
                docWidth = $(document).width(),
                btn_offset = $(this).offset(),
                btn_width = $(this).outerWidth(),
                btn_height = $(this).outerHeight(),
                elm_width = elm.outerWidth(),
                elm_height = elm.outerHeight(),
                table_offset = $(".tableresp").offset(),
                table_width = $(".tableresp").width(),
                table_height = $(".tableresp").height(),
    
                tableoffright = table_width + table_offset.left,
                tableoffbottom = table_height + table_offset.top,
                rem_tablewidth = docWidth - tableoffright,
                rem_tableheight = docHeight - tableoffbottom,
                elm_offsetleft = btn_offset.left,
                elm_offsetright = btn_offset.left + btn_width,
                elm_offsettop = btn_offset.top + btn_height,
                btn_offsetbottom = elm_offsettop,
    
                left_edge = (elm_offsetleft - table_offset.left) < elm_width,
                top_edge = btn_offset.top < elm_height,
                right_edge = (table_width - elm_offsetleft) < elm_width,
                bottom_edge = (tableoffbottom - btn_offsetbottom) < elm_height;
    
            console.log(tableoffbottom);
            console.log(btn_offsetbottom);
            console.log(bottom_edge);
            console.log((tableoffbottom - btn_offsetbottom) + "|| " + elm_height);
    
    
            var table_offset_bottom = docHeight - (table_offset.top + table_height);
    
            var touchTableBottom = (btn_offset.top + btn_height + (elm_height * 2)) - table_offset.top;
    
            var bottomedge = touchTableBottom > table_offset_bottom;
    
            if (left_edge) {
                $(this).addClass('left-edge');
            } else {
                $('.dropdown-menu').removeClass('left-edge');
            }
            if (bottom_edge) {
                $(this).parent().addClass('dropup');
            } else {
                $(this).parent().removeClass('dropup');
            }
    
        }
    });
    var table_smallheight = $('.tableresp'),
        positioning = table_smallheight.parent();
    
    if (table_smallheight.height() < 320) {
        positioning.addClass('positioning');
        $('.tableresp .dropdown,.tableresp .adropup').css('position', 'static');
    
    } else {
        positioning.removeClass('positioning');
        $('.tableresp .dropdown,.tableresp .dropup').css('position', 'relative');
    
    }
    
    0 讨论(0)
  • 2020-12-24 11:37

    just simply use as below and it will word wrap any long text within a table . No need to anything else

    <td style="word-wrap: break-word;min-width: 160px;max-width: 160px;">long long comments</td>
    
    0 讨论(0)
  • 2020-12-24 11:37

    The UberNeo response is Ok and i like it because you do not have to modify anything else except the TD. The only point is that you also have to add "white-space:normal" to the style in order to maintain the responsive characteristics of the table, if not, at certain resolutions the wrap is not made and the scroll of the table does not appear.

    style="word-wrap: break-word;min-width: 160px;max-width: 160px;white-space:normal;"
    
    0 讨论(0)
  • 2020-12-24 11:39

    So you can use the following :

    td {
      white-space: normal !important; // To consider whitespace.
    }
    

    If this doesn't work:

    td {
      white-space: normal !important; 
      word-wrap: break-word;  
    }
    table {
      table-layout: fixed;
    }
    
    0 讨论(0)
  • 2020-12-24 11:45

    .table td.abbreviation {
      max-width: 30px;
    }
    .table td.abbreviation p {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    
    }
    <table class="table">
    <tr>
    	<td class="abbreviation"><p>ABC DEF</p></td>
    </tr>
    </table>

    0 讨论(0)
  • 2020-12-24 11:50

    I ran across the same issue you did but the above answers did not solve my issue. The only way I was able to resolve it - was to make a class and use specific widths to trigger the wrapping for my specific use case. As an example, I provided a snippet below - but I found you will need to adjust it for the table in question - since I typically use multiple colspans depending on the layout. The reasoning I believe Bootstrap is failing - is because it removes the wrapping constraints to get a full table for the scrollbars. THe colspan must be tripping it up.

    <style>
    @media (max-width: 768px) { /* use the max to specify at each container level */
        .specifictd {    
            width:360px;  /* adjust to desired wrapping */
            display:table;
            white-space: pre-wrap; /* css-3 */
            white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
            white-space: -pre-wrap; /* Opera 4-6 */
            white-space: -o-pre-wrap; /* Opera 7 */
            word-wrap: break-word; /* Internet Explorer 5.5+ */
        }
    }
    

    I hope this helps

    0 讨论(0)
提交回复
热议问题