eve_mf eve_mf - 3 months ago 15
HTML Question

responsive data table html

I have a 5 column table, that on mobile, it needs to repeat each header for each row.

I am not having any luck looking around internet (probably because I am using the wrong words to search for it "repeat table headers for each row html")... I found one solution, which I have lost already, but it was so much code, and too complicated to understand. I am looking for some advice/help about how should I build the structure or maybe I need to do two markups completely different? I have also seen some plugins, but I am not allowed to use them (I am on a junior position and they want me to think about how to solve problems by myself).

I can't give a lot of information.... as I don't really know how to do it, but I guess it must be a way for no repeating the headers on html for every row... right?

Any help?

Thank you

html:

<table class="myOrders col-xs-12" border="1">
<thead>
<tr class="headers col-xs-12">
<th class="col-sm-3 col-x-7">ORDER PLACED</th>
<th class="col-sm-2 col-x-7">ORDER ID</th>
<th class="col-sm-2 col-x-7">ORDER REF</th>
<th class="col-sm-3 col-x-7">ORDER AMOUNT</th>
<th class="col-sm-2 col-x-7">STATUS</th>
</tr>
</thead>
<tbody>
<tr class="order col-xs-12">
<td class="datePlaced col-sm-3 col-x-7"><span>2016-05-12</span><span>15.13.56</span></td>
<td class="ident col-sm-2 col-x-7">AW 1234-165</td>
<td class="ref col-sm-2 col-x-7">SMRF123</td>
<td class="amount col-sm-3 col-x-7">£23.33</td>
<td class="status col-sm-2 col-x-7">Pending</td>
</tr>
<tr class="order col-xs-12">
<td class="datePlaced col-sm-3 col-x-7"><span>2016-05-12</span><span>15.13.56</span></td>
<td class="ident col-sm-2 col-x-7">AW 1234-165</td>
<td class="ref col-sm-2 col-x-7">SMRF123</td>
<td class="amount col-sm-3 col-x-7">£23.33</td>
<td class="status col-sm-2 col-x-7">Pending</td>
</tr>
<tr class="order col-xs-12">
<td class="datePlaced col-sm-3 col-x-7"><span>2016-05-12</span><span>15.13.56</span></td>
<td class="ident col-sm-2 col-x-7">AW 1234-165</td>
<td class="ref col-sm-2 col-x-7">SMRF123</td>
<td class="amount col-sm-3 col-x-7">£23.33</td>
<td class="status col-sm-2 col-x-7">Pending</td>
</tr>
</tbody>
</table>


css:

table td[class*="col-"],
table th[class*="col-"] {
float: left;
}


And this is the desired behaviour:
enter image description here

https://jsfiddle.net/zzxac8ew/

Answer

Use data-th with TD

@media screen and (max-width: 640px) {
    table#customDataTable caption {
        background-image: none;
    }

    table#customDataTable thead {
        display: none;
    }

    table#customDataTable tbody td {
        display: block;
        padding: .6rem;
    }

    table#customDataTable tbody tr td:first-child {
        background: #666;
        color: #fff;
    }

        table#customDataTable tbody tr td:first-child a {
            color: #fff;
        }

        table#customDataTable tbody tr td:first-child:before {
            color: rgb(225,181,71);
        }

    table#customDataTable tbody td:before {
        content: attr(data-th);
        font-weight: bold;
        display: inline-block;
        width: 10rem;
    }

    table#customDataTable tr th:last-child, table#customDataTable tr td:last-child {
        max-width: 100% !important;
        min-width: 100px !important;
        width: 100% !important;
    }
}
       <table id="customDataTable" class="myOrders col-xs-12" border="1">
         <thead>
         <tr class="headers col-xs-12">
           <th class="col-sm-3 col-x-7">ORDER PLACED</th>
           <th class="col-sm-2 col-x-7">ORDER ID</th>
           <th class="col-sm-2 col-x-7">ORDER REF</th>
           <th class="col-sm-3 col-x-7">ORDER AMOUNT</th>
           <th class="col-sm-2 col-x-7">STATUS</th>
         </tr>
         </thead>
         <tbody>
         <tr class="order col-xs-12">
	    <td data-th="ORDER PLACED" class="datePlaced col-sm-3 col-x-7"><span>2016-05-12</span><span>15.13.56</span></td>
           <td data-th="ORDER ID" class="ident col-sm-2 col-x-7">AW 1234-165</td>
           <td data-th="ORDER REF" class="ref col-sm-2 col-x-7">SMRF123</td>
           <td data-th="ORDER AMOUNT" class="amount col-sm-3 col-x-7">£23.33</td>
           <td data-th="STATUS" class="status col-sm-2 col-x-7">Pending</td>
	</tr>	
             <tr class="order col-xs-12">
	    <td data-th="ORDER PLACED" class="datePlaced col-sm-3 col-x-7"><span>2016-05-12</span><span>15.13.56</span></td>
           <td data-th="ORDER ID" class="ident col-sm-2 col-x-7">AW 1234-165</td>
           <td data-th="ORDER REF" class="ref col-sm-2 col-x-7">SMRF123</td>
           <td data-th="ORDER AMOUNT" class="amount col-sm-3 col-x-7">£23.33</td>
           <td data-th="STATUS" class="status col-sm-2 col-x-7">Pending</td>
	</tr>	
  <tr class="order col-xs-12">
	    <td data-th="ORDER PLACED" class="datePlaced col-sm-3 col-x-7"><span>2016-05-12</span><span>15.13.56</span></td>
           <td data-th="ORDER ID" class="ident col-sm-2 col-x-7">AW 1234-165</td>
           <td data-th="ORDER REF" class="ref col-sm-2 col-x-7">SMRF123</td>
           <td data-th="ORDER AMOUNT" class="amount col-sm-3 col-x-7">£23.33</td>
           <td data-th="STATUS" class="status col-sm-2 col-x-7">Pending</td>
	</tr>	
         </tbody>
</table>

https://jsfiddle.net/zzxac8ew/1/