gpt4 book ai didi

javascript - 在 Bootstrap 中组合固定宽度列和可变宽度列

转载 作者:行者123 更新时间:2023-11-28 06:45:01 25 4
gpt4 key购买 nike

我的演示代码:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Document</title>
<!-- 新 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">

<style type="text/css">
table.one{
table-layout: fixed;
}
</style>
</head>

<body>
<div class="container-fluid">
<div class="row">
<div class="col-md-3">
<h2>here is my navbar</h2>
<h3>25% width of the container</h3>
</div>
<div class="col-md-9">
<h2>here is my table</h2>
<h3>75% width of the container</h3>
<table id="example" class="table table-bordered table-striped table-hover table-condensed one" width="100%">
<thead>
<tr>
<th width="60"/>Name</th>
<th>Position</th>
<th>Office</th>
<th>Extn</th>
<th width="120">Start Date</th>
<th>Salary</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
<script src="http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
<script src="http://cdn.bootcss.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script type="text/javascript" src="http://cdn.datatables.net/1.10.9/js/jquery.dataTables.min.js"></script>
<script type="text/javascript">
var dataSet = [
[ "Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25", "$320,800" ],
[ "Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25", "$170,750" ],
[ "Ashton Cox", "Junior Technical Author", "San Francisco", "1562", "2009/01/12", "$86,000" ],
[ "Cedric Kelly", "Senior Javascript Developer", "Edinburgh", "6224", "2012/03/29", "$433,060" ],
[ "Airi Satou", "Accountant", "Tokyo", "5407", "2008/11/28", "$162,700" ],
[ "Brielle Williamson", "Integration Specialist", "New York", "4804", "2012/12/02", "$372,000" ],
[ "Herrod Chandler", "Sales Assistant", "San Francisco", "9608", "2012/08/06", "$137,500" ],
[ "Rhona Davidson", "Integration Specialist", "Tokyo", "6200", "2010/10/14", "$327,900" ],
[ "Colleen Hurst", "Javascript Developer", "San Francisco", "2360", "2009/09/15", "$205,500" ],
[ "Sonya Frost", "Software Engineer", "Edinburgh", "1667", "2008/12/13", "$103,600" ],
[ "Jena Gaines", "Office Manager", "London", "3814", "2008/12/19", "$90,560" ],
[ "Quinn Flynn", "Support Lead", "Edinburgh", "9497", "2013/03/03", "$342,000" ],
[ "Charde Marshall", "Regional Director", "San Francisco", "6741", "2008/10/16", "$470,600" ],
[ "Haley Kennedy", "Senior Marketing Designer", "London", "3597", "2012/12/18", "$313,500" ],
[ "Tatyana Fitzpatrick", "Regional Director", "London", "1965", "2010/03/17", "$385,750" ],
[ "Michael Silva", "Marketing Designer", "London", "1581", "2012/11/27", "$198,500" ],
[ "Paul Byrd", "Chief Financial Officer (CFO)", "New York", "3059", "2010/06/09", "$725,000" ],
[ "Gloria Little", "Systems Administrator", "New York", "1721", "2009/04/10", "$237,500" ],
[ "Bradley Greer", "Software Engineer", "London", "2558", "2012/10/13", "$132,000" ],
[ "Dai Rios", "Personnel Lead", "Edinburgh", "2290", "2012/09/26", "$217,500" ],
[ "Jenette Caldwell", "Development Lead", "New York", "1937", "2011/09/03", "$345,000" ],
[ "Yuri Berry", "Chief Marketing Officer (CMO)", "New York", "6154", "2009/06/25", "$675,000" ],
[ "Caesar Vance", "Pre-Sales Support", "New York", "8330", "2011/12/12", "$106,450" ],
[ "Doris Wilder", "Sales Assistant", "Sidney", "3023", "2010/09/20", "$85,600" ],
[ "Angelica Ramos", "Chief Executive Officer (CEO)", "London", "5797", "2009/10/09", "$1,200,000" ],
[ "Gavin Joyce", "Developer", "Edinburgh", "8822", "2010/12/22", "$92,575" ],
[ "Jennifer Chang", "Regional Director", "Singapore", "9239", "2010/11/14", "$357,650" ],
[ "Brenden Wagner", "Software Engineer", "San Francisco", "1314", "2011/06/07", "$206,850" ],
[ "Fiona Green", "Chief Operating Officer (COO)", "San Francisco", "2947", "2010/03/11", "$850,000" ],
[ "Shou Itou", "Regional Marketing", "Tokyo", "8899", "2011/08/14", "$163,000" ],
[ "Michelle House", "Integration Specialist", "Sidney", "2769", "2011/06/02", "$95,400" ],
[ "Suki Burks", "Developer", "London", "6832", "2009/10/22", "$114,500" ],
[ "Prescott Bartlett", "Technical Author", "London", "3606", "2011/05/07", "$145,000" ],
[ "Gavin Cortez", "Team Leader", "San Francisco", "2860", "2008/10/26", "$235,500" ],
[ "Martena Mccray", "Post-Sales support", "Edinburgh", "8240", "2011/03/09", "$324,050" ],
[ "Unity Butler", "Marketing Designer", "San Francisco", "5384", "2009/12/09", "$85,675" ]
];

$(document).ready(function() {
$('#example').DataTable( {
data: dataSet/*,
columns: [
{ title: "Name" },
{ title: "Position" },
{ title: "Office" },
{ title: "Extn." },
{ title: "Start date" },
{ title: "Salary" }
]*/
} );
} );
</script>
</body>
</html>

如何制作 HTML 两列,其中第一列宽度占容器宽度的 25%,第二列宽度占容器宽度的 75%,并用固定宽度列和可变宽度列填充它?

我的愿望:

第一步:当我隐藏第一个(div)列时,第二个(div)列填满整个容器(宽度= 100%)。

第二步:第二个(div)中固定列的表格不应改变宽度,那些可变列会改变。

所以,我使用了 jQuery:

$(".col-sm-3").hide(950); // Hide the nav
$(".col-sm-9").animate({width:"100%"},1000);
// Change the second column's width to 100%

但是当我设置第二列中的固定列时,它们的宽度会改变:

<th style="width:60px">Name</th>
<th>Position</th>
<th>Office</th>
<th>Extn</th>
<th style="width:120px">Start Date</th>
<th>Salary</th>

我尝试了很多方法来解决这个问题,例如:

  1. 将 col 和 colgroup 添加到我的表中。我发现列已修复,但我的数据未显示。

    姓名 位置 办公室 分机号 开始日期 薪水
  2. 我看到了很多例子:

示例 1:Combining fixed and variable width columns in one table 。示例2: Combining fixed and variable width columns in one table示例 3:Table with fixed height and header and colum width with Bootstrap 3示例 4:Fixed width table columns in Bootstrap

我都试过了,下面是一个:

<div class="wrapper">
<table class="table-main">
<tr class="">
<td class="td-quarter2"> </td>
<td class="second"> </td>
<td class="third"> </td>
<td class="fluid">
<table class="table-wrapped">
<tr>
<td class="td-quarter1"> </td>
<td class="td-quarter2"> </td>
<td class="td-quarter3"> </td>
<td class="td-quarter4"> </td>
</tr>
</table>
<table class="table-wrapped1">
<tr>
<td class="td-quarter1"> </td>
</tr>
</table>
</td>
</tr>
</table>
</div>​

但是,当我使用 datatable.js 时,它在我的项目中效果不佳。 http://datatables.net/ 。你可以看我的演示代码。

  • 尝试表格布局,使用“固定属性”。
  • 喜欢:

    <style type="text/css">
    table.one{
    table-layout: fixed;
    }
    </style>
    <table id="example" class="table table-bordered table-striped table-hover table-condensed one" >
    <thead>
    <tr>
    <th width="60"/>Name</th>
    <th>Position</th>
    <th>Office</th>
    <th>Extn</th>
    <th width="120">Start Date</th>
    <th>Salary</th>
    </tr>
    </thead>
    </table>

    这次我使用时无法设置第二列填满整个容器

    $(".col-md-9")animate({width:"100%"},1000);

    表格的宽度不变。

    最佳答案

    这是我的解决方案:

    第 1 步。我发现在演示jsfiddle.net/mev90nLt/14中使用我的项目的静态数据效果很好,所以我删除了我项目的所有css和js,使用演示的。效果很好。

    因此,在 datatable() 中添加属性,如下所示,可以设置您想要的固定列:

    $('#example').DataTable( {
    data: dataSet,
    autoWidth: false,
    "columns": [
    { "width": "60px" },
    null,
    null,
    { "width": "155px" },
    null,
    { "width": "60px" },
    ]
    } );

    第 2 步。我使用 datatable.js 的动态数据库,显示两个问题。第一个是:数据表初始化一次后就不能再更改了,查看这里http://datatables.net/manual/tech-notes/3 。所以我添加了 detroy() 来解决动态数据问题。

     var table = $('#coursetable').DataTable();
    tabale.detroy();
    table = $('#coursetable').DataTable({data:dataset});

    此后,我的专栏无法再次修复。

    第3步。当您使用 destroy() 函数更新数据表的数据时,如下所示:

    var table = $('#coursetable').DataTable();

    要初始化一个空白数据表,该数据表将采用一些默认属性,例如自动宽度。

    解决方案是:当您执行初始化时,第一个数据表应该采用属性,例如宽度。

     <script>
    var table = $('#coursetable').DataTable( {
    paging: false,
    searching: false,
    "bSort" : false,
    autoWidth: false,
    "columns": [
    { "width": "30px" },
    null,
    null,
    { "width": "150px" },
    null,
    { "width": "70px" },
    ]

    } );
    </script>

    然后,当您动态更改数据表的数据时,首先使用 destroy() 函数,然后再次设置这些属性,如下所示:

    function setDataTable(){
    table.destroy();
    table = $('#coursetable').DataTable( {
    data: eval(datatable),
    searching: true,
    "bSort" : false,
    autoWidth: false,
    "columns": [
    { "width": "30px" },
    null,
    null,
    { "width": "150px" },
    null,
    { "width": "70px" },
    ]
    } );
    }
    }

    成功了,我的问题解决了。

    关于javascript - 在 Bootstrap 中组合固定宽度列和可变宽度列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33475161/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com