代码之家  ›  专栏  ›  技术社区  ›  shifu

在表体上插入值后,将表体与表头对齐

  •  2
  • shifu  · 技术社区  · 8 年前

    问题:

    • 正文的内容与表中每一列的标题不对齐。

    代码:

    var test_insert1 = '<tr>' +
      '<td  class="td-trad-9">762261</td>' +
      '<td  class="td-trad-7">1.16176</td>' +
      '<td class="td-trad-8">1.1618</td>' +
      '<td class="td-trad-1">2018-08-08</td>' +
      '<td class="td-trad-2">03:32</td>' +
      '<td class="td-trad-3">This is a test long value</td>' +
      '<td class="td-trad-4">1.00</td>' +
      '<td class="td-trad-5">Up</td>' +
      '<td class="td-trad-6">0.80</td>' +
      '</tr>'+
      '<tr>' +
      '<td  class="td-trad-9">456985</td>' +
      '<td  class="td-trad-7">1.65476</td>' +
      '<td class="td-trad-8">1984218</td>' +
      '<td class="td-trad-1">2018-08-08</td>' +
      '<td class="td-trad-2">03:32</td>' +
      '<td class="td-trad-3">This is a test value</td>' +
      '<td class="td-trad-4">10000</td>' +
      '<td class="td-trad-5">Up</td>' +
      '<td class="td-trad-6">1.00</td>' +
      '</tr>';
    
    $('#add').click(function() {
      $('#new_table_test_body').html(test_insert1);
    });
    #new_table_test {
      width: auto;
      table-layout: fixed;
      border-collapse: collapse;
      /* color:white; */
      text-align: center;
      vertical-align: middle;
    }
    
    #new_table_test tbody::-webkit-scrollbar {
      width: 0px;
      /* remove scrollbar space */
      background: transparent;
      /* optional: just make scrollbar invisible */
    }
    
    #new_table_test tbody {
      display: block;
      width: 100%;
      overflow: auto;
      height: 100px;
    }
    
    #new_table_test thead tr {
      display: block;
    }
    
    #new_table_test thead {
      background: black;
      color: #fff;
    }
    
    #new_table_test th,
    #new_table_test td {
      padding: 5px;
      text-align: left;
      width: auto;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table id="new_table_test">
      <thead id="new_table_test_header">
        <tr>
          <th>Header 1</th>
          <th>Header Two</th>
          <th>Head Three</th>
          <th>H4</th>
          <th>Header 5</th>
          <th>Heads 6</th>
          <th>Header seven</th>
          <th>Header 8</th>
          <th>Header Nine</th>
        </tr>
      </thead>
      <tbody id="new_table_test_body"></tbody>
    </table>
    
    <button id="add"> Add Details </button>

    --编辑--

    附加:

    条件:

    • 可以滚动表格体
    • 标题不受滚动影响(固定)
    • 宽度应根据内容自动调整

    这里还有一个最新的小提琴,我很担心: http://jsfiddle.net/9sjyvb5w/50

    3 回复  |  直到 8 年前
        1
  •  1
  •   shifu    8 年前

    更改此:

    #new_table_test {
      width: auto;
      table-layout: fixed;
    }
    

    对此:

    #new_table_test {
      width: 1000px;
      table-layout: auto;
    }
    

    你也应该移除 display: block; 从 #new_table_test tbody 和 #new_table_test thead tr .

    有更多信息 here 关于 table-layout 财产。

    jsffiddle

    jsfiddle with scroll

    jsfiddle with fixed header

        2
  •  1
  •   Abhishek Kumar    8 年前

    虽然@bahman的回答看起来不错,但同时我尝试了一些不同的方法 flex property .

    方法: 在加载数据之前,我已经将 <thead> ,但按一下but on,我已经隐藏了 <主题ad> 只有 <tbody> 对于包含头值的第一行可见,并相应地设置第一行的样式以匹配头。

    方法问题:
    需要更多的操作,更复杂的as需要添加第一行作为标题。

    var test_insert1 = 
    '<tr>' +
      '<td  class="td-trad-9">Header 1</td>' +
      '<td  class="td-trad-7">Header Two</td>' +
      '<td class="td-trad-8">Head Three</td>' +
      '<td class="td-trad-1">H4</td>' +
      '<td class="td-trad-2">Header 5</td>' +
      '<td class="td-trad-3">Header 6</td>' +
      '<td class="td-trad-6">Head 45</td>' +
      '</tr>'+'<tr>' +
      '<td  class="td-trad-9">762261</td>' +
      '<td  class="td-trad-7">1.16176</td>' +
      '<td class="td-trad-8">1.1618</td>' +
      '<td class="td-trad-1">2018-08-08</td>' +
      '<td class="td-trad-2">03:32</td>' +
      '<td class="td-trad-3">This is a test long value</td>' +
      '<td class="td-trad-6">0.80</td>' +
      '</tr>'+
      '<tr>' +
      '<td  class="td-trad-9">456985</td>' +
      '<td  class="td-trad-7">1.65476</td>' +
      '<td class="td-trad-8">1984218</td>' +
      '<td class="td-trad-1">2018-08-08</td>' +
      '<td class="td-trad-2">03:32</td>' +
      '<td class="td-trad-3">This is a test value</td>' +
      '<td class="td-trad-6">1.00</td>' +
      '</tr>';
    
    $('#add').click(function() {
      $('#new_table_test_body').html(test_insert1);
      $('#new_table_test_header').hide();
    });
    #new_table_test {
      width: auto;
      table-layout: fixed;
      border-collapse: collapse;
      /* color:white; */
      text-align: center;
      vertical-align: middle;
      display:flex;
      flex-direction: column;
      justify-content: center;
    }
    #new_table_test_header,
    #new_table_test_body {
      display:flex;
      flex-direction: row;
    }
    
    #new_table_test_body > tr:first-child {
    background: black;
        color: white;
        font-weight: bold;
    }
    #new_table_test tbody::-webkit-scrollbar {
      width: 0px;
      /* remove scrollbar space */
      background: transparent;
      /* optional: just make scrollbar invisible */
    }
    
    #new_table_test tbody {
      display: block;
      width: 100%;
      overflow: auto;
      
    }
    
    #new_table_test thead tr {
      display: block;
    }
    
    #new_table_test thead {
      background: black;
      color: #fff;
    }
    
    #new_table_test th,
    #new_table_test td {
      padding: 5px;
      text-align: left;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table id="new_table_test">
      <thead id="new_table_test_header">
        <tr>
          <th>Header 1</th>
          <th>Header Two</th>
          <th>Head Three</th>
          <th>H4</th>
          <th>Header 5</th>
          <th>Heads 6</th>
          <th>Head 45</th>
        </tr>
      </thead>
      <tbody id="new_table_test_body"></tbody>
    </table>
    
    <button id="add"> Add Details </button>
        3
  •  0
  •   Vicky Maharjan    8 年前

    将以下内容添加到表css(#new_table_test)中:

    #new_table_test{
        display: inline-block;
        white-space: nowrap; //prevents word wrapping
        ....
    }
    

    http://jsfiddle.net/aLevx0tn/