P
P
Pavel Zhukau2015-10-23 18:10:51
JavaScript
Pavel Zhukau, 2015-10-23 18:10:51

How to merge cells in html table if they are similar?

Good evening everyone. There is a table:
Mon Tue Wed Thu Fri
Day off Day off 10:00-21:00 10:00-21:00
And you need to get:
Mon-Wed Thu-Fri
Day off 10:00-21:00
You can use js, jquery,
Tell me please in which direction to dig?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Inchin ☢, 2015-10-23
@NikesDark

I'm probably exaggerating, but...

<table border>
    <thead>
        <td>ПН</td>
        <td>ВТ</td>
        <td>СР</td>
        <td>ЧТ</td>
        <td>ПТ</td>
        <td>СБ</td>
        <td>ВС</td>
    </thead>
    <tr id="state">
        <td>Выходной</td>
        <td>Выходной</td>
        <td>Выходной</td>
        <td>Выходной</td>
        <td>15-21</td>
        <td>15-21</td>
        <td>16-21</td>
    </tr>
</table>

var 
    $Table = $("<table />"),  
    $Thead = $("<thead />").appendTo($Table), 
    $Row   = $("<tr />").appendTo($Table),
    $_tds  = $("#state td"),  $_heads = $("thead td"),
start, i = 0;

for(; i < $_tds.length; i++){
  start = i; 
  while(
     $_tds.get(i++) && $_tds.eq(i).text() == $_tds.eq(i - 1).text()
  ){}

  $Row.append(
      "<td>" + $_tds.eq(--i).text() + "</td>"
  ); 
  $Thead.append(
      "<td>" + (i == start ? "" :  $_heads.eq(start).text() + "-") + $_heads.eq(i).text() + "</td>"
   );
}

$("table").replaceWith($Table);

jsfiddle.net/In4in/6ruk1j3r

T
titronfan, 2015-10-23
@titronfan

First, build an array with the data you have.
Based on this, you will find out what data is not unique - and cost your HTML.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question