将表格单元格拆分为HTML格式的两列

时间:2013-10-01 11:57:12

标签: html css html-table

我有下表:

<table border="1">
  <tr>
    <th scope="col">Header</th>
    <th scope="col">Header</th>
    <th scope="col">Header</th>
  </tr>
  <tr>
    <th scope="row">&nbsp;</th>
    <td>&nbsp;</td>
    <td>Split this one into two columns</td>
  </tr>
</table>

我希望将包含“将此分成两列”的单元格拆分为两个单元格/列。我该怎么做?

Fiddle

8 个答案:

答案 0 :(得分:34)

喜欢这个http://jsfiddle.net/8ha9e/1/

colspan="2"添加到第3个<th>,然后在第二行中添加4个<td>

<table border="1">
  <tr>
    <th scope="col">Header</th>
    <th scope="col">Header</th>
    <th scope="col" colspan="2">Header</th>
  </tr>
  <tr>
    <th scope="row">&nbsp;</th>
    <td>&nbsp;</td>
    <!-- The following two cells will appear under the same header -->
    <td>Col 1</td>
    <td>Col 2</td>
  </tr>
</table>

答案 1 :(得分:13)

您有两种选择。

  1. 在标题中使用额外的列,并在标题中使用<colspan>来拉伸两列或更多列的单元格。
  2. <table>的{​​{1}}内插入一个包含2列的td

答案 2 :(得分:13)

我来到这里是因为我遇到了类似的问题。

@MrMisterMan的答案,以及其他人,真的很有帮助,但边界正在击败我的游戏。所以,我做了一些研究,发现使用 rowspan

这就是我的所作所为,我想这可能有助于其他人面对类似的事情。

&#13;
&#13;
<table style="width: 100%; margin-top: 10px; font-size: 0.8em;" border="1px">
    <tr align="center" >
        <th style="padding:2.5px; width: 10%;" rowspan="2">Item No</th>
        <th style="padding:2.5px; width: 55%;" rowspan="2">DESCRIPTION</th>
        <th style="padding:2.5px;" rowspan="2">Quantity</th>
        <th style="padding:2.5px;" colspan="2">Rate per Item</th>
        <th style="padding:2.5px;" colspan="2">AMOUNT</th>
    </tr>
    <tr>
        <th>Rs.</th>
        <th>P.</th>
        <th>Rs.</th>
        <th>P.</th>
    </tr>
</table>
&#13;
&#13;
&#13;

答案 3 :(得分:3)

是你在找什么?

<table border="1">
<tr>
 <th scope="col">Header</th>
 <th scope="col">Header</th>
 <th scope="col" colspan="2">Header</th>
</tr>
<tr>
 <th scope="row">&nbsp;</th>
 <td>&nbsp;</td>
 <td>Split this one</td>
 <td>into two columns</td>
</tr>
</table>  

答案 4 :(得分:2)

将要分割的<td>更改为如下所示:

<td><table><tr><td>split 1</td><td>split 2</td></tr></table></td> 

答案 5 :(得分:2)

使用此示例,您可以使用colspan属性

进行拆分
<TABLE BORDER>
     <TR>
         <TD>Item 1</TD>
         <TD>Item 1</TD>
         <TD COLSPAN=2>Item 2</TD>
    </TR>
    <TR>
         <TD>Item 3</TD>
         <TD>Item 3</TD>
         <TD>Item 4</TD>
         <TD>Item 5</TD>
    </TR>
</TABLE>

http://hypermedia.univ-paris8.fr/jean/internet/ex_table.html的更多示例。

答案 6 :(得分:0)

请尝试以下方式。

<table>
  <tr>
    <th>Month</th>
    <th>Savings</th>
  </tr>
  <tr>
    <td>January</td>
    <td>$100</td>
  </tr>
  <tr>
    <td>February</td>
    <td>$80</td>
  </tr>
  <tr>
    <td colspan="2">Sum: $180</td>
  </tr>
</table>

答案 7 :(得分:0)

https://jsfiddle.net/SyedFayaz/ud0mpgoh/7/

import random
import binpacking

# DISTRIBUTES A DICTIONARY OF LENGTHS (KEY-VALUE = ID-LENGTH PAIR) TO A MINIMAL NUMBER IF BINS WHICH CAN HAVE DIFFERENT VOLUME.
def packing2dOptimizer(b, bin_bucket):

    # OPTIMIZED BINS WILL BE STORED HERE
    bin_list = []

    bin_bucket.append(0)
    bin_bucket.sort(reverse=True)

    # LIST OF DIFFERENCES BETWEEN BIN SIZES
    difference = [0]
    if len(bin_bucket) > 1:
        for index in range(1, len(bin_bucket)):
            difference.append(bin_bucket[0] - bin_bucket[index])

    # FIND THE EXACT BIN WIDTH AS PER USAGE OF PACKINGS
    def checkFinalSize(total):
        bin_width = 0
        for i in bin_bucket: 
            if total <= i: bin_width = i
            else: break
        return bin_width

    # PACK ALL THE VALUES IN BINS WITH MAXIMUM SIZE
    packer_list = binpacking.to_constant_volume(b,bin_bucket[0])

    # CREATE A DETAILED DATA OF BINPACKING
    counter = 0
    for packer in packer_list:
        bin_usage = sum(packer.values())
        bin_width = checkFinalSize(bin_usage)
        bin_waste = bin_width - bin_usage
        bin_data = {
            'bin_id': counter,
            'bin_width': bin_width,
            'bin_usage': bin_usage,
            'bin_waste': bin_waste,
            'pack_data': packer
        }
        counter += 1
        bin_list.append(bin_data)

    # PRINT THE LIST
    area_used = 0
    area_covered = 0
    area_waste = 0
    for i in bin_list: 
        print("Bin", i['bin_width'],":",i['pack_data'], i['bin_waste'])
        area_used += i['bin_width']
        area_covered += sum(i['pack_data'].values())
        area_waste +=  i['bin_waste']
    print()
    print("Area Used:", area_used)
    print("Area Covered:",  area_covered)
    print("Area Waste:",  area_waste)
    print()

    # TAKES TWO PACKED BINS AND THE AVAILABLE SIZES OF ALL BINS AS ARGUMENT
    def optimizer(small_bin, big_bin):
        dict1 = small_bin['pack_data']
        dict2 = big_bin['pack_data']

        # CREATE A NEW DICTIONARY OF DATA OF THE BOTH PACKED BINS
        new_dict = dict1.copy()
        new_dict.update(dict2)
        max_val = max(list(new_dict.values()))

        # FIND A SUITABLE BIN WITH LOWEST SIZE AND WASTAGE
        for size in bin_bucket:
            if size >= max_val:
                new_bins = binpacking.to_constant_volume(new_dict,size)    
                if len(new_bins) <= 2:
                    final_bins = new_bins

        # UPDATE BOTH PACKED BIN VALUES AND RETURNS
        big_bin['pack_data'] = final_bins[0]
        small_bin['pack_data'] = final_bins[1]
        return small_bin, big_bin

    # OPTIMIZE THE BINS TO MINIMIZE THE WASTE
    bin_bucket.sort(reverse=True)
    for small_bin in bin_list:
        if small_bin['bin_width'] == bin_bucket[-2] and small_bin['bin_waste'] > 0:
            for big_bin in bin_list:
                if big_bin['bin_width'] > small_bin['bin_width']:
                    pack_data = list(big_bin['pack_data'].values())
                    for length in pack_data:
                        if length <= small_bin['bin_waste']:
                            # CALL THE OPTIMIZER
                            small_bin, big_bin = optimizer(small_bin, big_bin)
                            
                            # UPDATE THE BIN DATA
                            bin_usage = sum(small_bin['pack_data'].values())
                            bin_width = checkFinalSize(bin_usage)
                            small_bin['bin_width'] = bin_width
                            small_bin['bin_usage'] = bin_usage
                            small_bin['bin_waste'] = bin_width - bin_usage

                            bin_usage = sum(big_bin['pack_data'].values())
                            bin_width = checkFinalSize(bin_usage)
                            big_bin['bin_width'] = bin_width
                            big_bin['bin_usage'] = bin_usage
                            big_bin['bin_waste'] = bin_width - bin_usage

                            break

    # SORT THE OPTIMIZED BINS BY BIN SIZE
    bin_list = sorted(bin_list, key = lambda i: i['bin_width'], reverse = True)

    # PRINT THE LIST
    area_used = 0
    area_covered = 0
    area_waste = 0
    for i in bin_list: 
        print("Bin", i['bin_width'],":",i['pack_data'], i['bin_waste'])
        area_used += i['bin_width']
        area_covered += sum(i['pack_data'].values())
        area_waste +=  i['bin_waste']
    print()
    print("Area Used:", area_used)
    print("Area Covered:",  area_covered)
    print("Area Waste:",  area_waste)

    return bin_list

def main():
    
    # LIST OF DATA THAT WILL BE SELECTED RANDOMLY
    random_data = [ i for i in range(100, 3200,100) ]

    # DIFFERENT SIZE OF VALUES WITH UNIQUE ID ASSOCIATED WITH IT
    b = {
        'a': random.choice(random_data), 
        'b': random.choice(random_data), 
        'c': random.choice(random_data), 
        'd': random.choice(random_data),
        'e': random.choice(random_data),
        'f': random.choice(random_data),
        'g': random.choice(random_data),
        'h': random.choice(random_data),
        'i': random.choice(random_data),
        'j': random.choice(random_data),
        'k': random.choice(random_data),
        'l': random.choice(random_data),
        'm': random.choice(random_data),
        'n': random.choice(random_data),
        'o': random.choice(random_data),
        'p': random.choice(random_data),
        'q': random.choice(random_data),
        'r': random.choice(random_data),
        's': random.choice(random_data),
        't': random.choice(random_data),
        'u': random.choice(random_data),
        'v': random.choice(random_data),
        'w': random.choice(random_data),
        'x': random.choice(random_data),
        'y': random.choice(random_data),
        'z': random.choice(random_data),
    }

    # LIST OF DIFFERENT SIZES OF BINS
    bin_bucket = [2400, 2700, 3100]

    bin_list = packing2dOptimizer(b, bin_bucket)

main()