Merge table rows with jQuery









up vote
1
down vote

favorite












I have a table that have same values for each person, so I need to merge those rows for that person with same values. In some cases all rows with same value are merging but they are repeated with same values. In some other cases courses with same name are merged and this not right as I mentioned before only same values for one person should merge,
(so sorry for my bad English, don't know how to explain this I hope snippet show you enough)
Any solution for this?






function groupTable($rows, startIndex, total) 
if (total === 0)
return;

var i, currentIndex = startIndex,
count = 1,
lst = ;
var tds = $rows.find('td:eq(' + currentIndex + ')');
var ctrl = $(tds[0]);
lst.push($rows[0]);
for (i = 1; i <= tds.length; i++)
if (ctrl.attr("id") == $(tds[i]).attr("id"))
count++;
$(tds[i]).addClass('deleted');
lst.push($rows[i]);

else
if (count > 1)
ctrl.attr('rowspan', count);
groupTable($(lst), startIndex + 1, total - 1)

count = 1;
lst = ;
ctrl = $(tds[i]);
lst.push($rows[i]);



groupTable($('#myTable tr:has(td)'), 0, 14);
$('#myTable .deleted').remove();

<table class="table table-striped border-top table-bordered" id="myTable">
<thead>
<tr id="th1" style="background:#d3d3d3 ;">
<th>#</th>
<th>Grade</th>
<th>Teacher</th>
<th>Curse</th>
<th colspan="3">Morning</th>
<th colspan="3">Evening</th>
<th colspan="2" style="font-size: 12px;">Weekly hour</th>

<th>sum</th>
<th>-</th>
</tr>
<tr id="th2">
<th></th>
<th></th>
<th></th>
<th></th>
<th>theory</th>
<th>Practical</th>
<th>free</th>
<th>theory</th>
<th>Practical</th>
<th>free</th>
<th>Morning</th>
<th>Evening</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody id="details_view">
<tr id="p">
<td id="id7" class "ids"="" data-id="ids">1</td>
<td id="prof7">Mr</td>
<td id="prof7">John Doe</td>
<td id="prof7" style="font-size: 12px;">Electric</td>
<td id="prof7" data-time="morning_theory" data-crs="Electric" data-prof="John Doe">0</td>
<td id="prof7" data-time="morning_practical" data-crs="Electric" data-prof="John Doe">18</td>
<td id="prof7" data-time="morning_free" data-crs="Electric" data-prof="John Doe">12</td>
<td id="prof7" data-time="evening_theory" data-crs="Electric" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_practical" data-crs="Electric" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_free" data-crs="Electric" data-prof="John Doe">0</td>
<td id="sob7" style="background:#d3d3d3 ;">30</td>
<td id="asr7" style="background:#d3d3d3 ;">0</td>
<td id="prof7">30</td>
<td id="prof7" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id7" class "ids"="" data-id="ids">2</td>
<td id="prof7">Mr</td>
<td id="prof7">John Doe</td>
<td id="prof7" style="font-size: 12px;">course2</td>
<td id="prof7" data-time="morning_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="morning_practical" data-crs="course2" data-prof="John Doe">18</td>
<td id="prof7" data-time="morning_free" data-crs="course2" data-prof="John Doe">12</td>
<td id="prof7" data-time="evening_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_practical" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_free" data-crs="course2" data-prof="John Doe">0</td>
<td id="sob7" style="background:#d3d3d3 ;">30</td>
<td id="asr7" style="background:#d3d3d3 ;">0</td>
<td id="prof7">30</td>
<td id="prof7" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id7" class "ids"="" data-id="ids">3</td>
<td id="prof7">Mr</td>
<td id="prof7">John Doe</td>
<td id="prof7" style="font-size: 12px;">course2</td>
<td id="prof7" data-time="morning_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="morning_practical" data-crs="course2" data-prof="John Doe">18</td>
<td id="prof7" data-time="morning_free" data-crs="course2" data-prof="John Doe">12</td>
<td id="prof7" data-time="evening_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_practical" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_free" data-crs="course2" data-prof="John Doe">0</td>
<td id="sob7" style="background:#d3d3d3 ;">30</td>
<td id="asr7" style="background:#d3d3d3 ;">0</td>
<td id="prof7">۳۰</td>
<td id="prof7" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id30" class "ids"="" data-id="ids">4</td>
<td id="prof30">Mr</td>
<td id="prof30">Robert Anderson</td>
<td id="prof12" style="font-size: 12px;">Course2 </td>
<td id="prof12" data-time="morning_theory" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="morning_practical" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="morning_free" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="evening_theory" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="evening_practical" data-crs="Course2" data-prof="Robert Anderson">6</td>
<td id="prof12" data-time="evening_free" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="sob12" style="background:#d3d3d3 ;">0</td>
<td id="asr12" style="background:#d3d3d3 ;">6</td>
<td id="prof12">6</td>
<td id="prof12" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id6" class "ids"="" data-id="ids">5</td>
<td id="prof6">Mr</td>
<td id="prof6">Jack Hulk</td>
<td id="prof5" style="font-size: 12px;">Course3</td>
<td id="prof5" data-time="morning_theory" data-crs="Course3" data-prof="Jack Hulk">8</td>
<td id="prof5" data-time="morning_practical" data-crs="Course3" data-prof="Jack Hulk">12</td>
<td id="prof5" data-time="morning_free" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_theory" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_practical" data-crs="Course3" data-prof="Jack Hulk">4</td>
<td id="prof5" data-time="evening_free" data-crs="Course3" data-prof="Jack Hulk">18</td>
<td id="sob5" style="background:#d3d3d3 ;">22</td>
<td id="asr5" style="background:#d3d3d3 ;">24</td>
<td id="prof5">46</td>
<td id="prof5" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id6" class "ids"="" data-id="ids">6</td>
<td id="prof6">Mr</td>
<td id="prof6">Jack Hulk</td>
<td id="prof5" style="font-size: 12px;">Course3</td>
<td id="prof5" data-time="morning_theory" data-crs="Course3" data-prof="Jack Hulk">8</td>
<td id="prof5" data-time="morning_practical" data-crs="Course3" data-prof="Jack Hulk">12</td>
<td id="prof5" data-time="morning_free" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_theory" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_practical" data-crs="Course3" data-prof="Jack Hulk">4</td>
<td id="prof5" data-time="evening_free" data-crs="Course3" data-prof="Jack Hulk">18</td>
<td id="sob5" style="background:#d3d3d3 ;">22</td>
<td id="asr5" style="background:#d3d3d3 ;">24</td>
<td id="prof5">46</td>
<td id="prof5" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id6" class "ids"="" data-id="ids">7</td>
<td id="prof6">Mr</td>
<td id="prof6">Jack Hulk</td>
<td id="prof5" style="font-size: 12px;">Course3</td>
<td id="prof5" data-time="morning_theory" data-crs="Course3" data-prof="Jack Hulk">8</td>
<td id="prof5" data-time="morning_practical" data-crs="Course3" data-prof="Jack Hulk">12</td>
<td id="prof5" data-time="morning_free" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_theory" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_practical" data-crs="Course3" data-prof="Jack Hulk">4</td>
<td id="prof5" data-time="evening_free" data-crs="Course3" data-prof="Jack Hulk">18</td>
<td id="sob5" style="background:#d3d3d3 ;">22</td>
<td id="asr5" style="background:#d3d3d3 ;">24</td>
<td id="prof5">46</td>
<td id="prof5" style="width: 160px;"></td>
</tr>
</tbody>
</table>

<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>












share|improve this question









New contributor




John is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • But the snippet example seems to give the right output. Could you instead give a snippet example where it goes wrong? If the snippet output is wrong, please add the table that you expect as output.
    – trincot
    2 days ago











  • This snippet already has wrong result. look at "Mr John Doe" in html code. He has 2 different courses but in results its not shown.
    – John
    2 days ago










  • So you want only rows to be removed when they are completely the same? Or only when some columns are the same?
    – trincot
    2 days ago










  • I want all rows for one person with same value to be merged.
    – John
    yesterday










  • What is "with same value"? Which value(s)? All rows have at least one cell different. For instance, the first cell (the "#" column) is always different in your example. Others have a different value in the "sum" column (like row 2 and 3).... It is not clear what you mean with "with same value".
    – trincot
    yesterday














up vote
1
down vote

favorite












I have a table that have same values for each person, so I need to merge those rows for that person with same values. In some cases all rows with same value are merging but they are repeated with same values. In some other cases courses with same name are merged and this not right as I mentioned before only same values for one person should merge,
(so sorry for my bad English, don't know how to explain this I hope snippet show you enough)
Any solution for this?






function groupTable($rows, startIndex, total) 
if (total === 0)
return;

var i, currentIndex = startIndex,
count = 1,
lst = ;
var tds = $rows.find('td:eq(' + currentIndex + ')');
var ctrl = $(tds[0]);
lst.push($rows[0]);
for (i = 1; i <= tds.length; i++)
if (ctrl.attr("id") == $(tds[i]).attr("id"))
count++;
$(tds[i]).addClass('deleted');
lst.push($rows[i]);

else
if (count > 1)
ctrl.attr('rowspan', count);
groupTable($(lst), startIndex + 1, total - 1)

count = 1;
lst = ;
ctrl = $(tds[i]);
lst.push($rows[i]);



groupTable($('#myTable tr:has(td)'), 0, 14);
$('#myTable .deleted').remove();

<table class="table table-striped border-top table-bordered" id="myTable">
<thead>
<tr id="th1" style="background:#d3d3d3 ;">
<th>#</th>
<th>Grade</th>
<th>Teacher</th>
<th>Curse</th>
<th colspan="3">Morning</th>
<th colspan="3">Evening</th>
<th colspan="2" style="font-size: 12px;">Weekly hour</th>

<th>sum</th>
<th>-</th>
</tr>
<tr id="th2">
<th></th>
<th></th>
<th></th>
<th></th>
<th>theory</th>
<th>Practical</th>
<th>free</th>
<th>theory</th>
<th>Practical</th>
<th>free</th>
<th>Morning</th>
<th>Evening</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody id="details_view">
<tr id="p">
<td id="id7" class "ids"="" data-id="ids">1</td>
<td id="prof7">Mr</td>
<td id="prof7">John Doe</td>
<td id="prof7" style="font-size: 12px;">Electric</td>
<td id="prof7" data-time="morning_theory" data-crs="Electric" data-prof="John Doe">0</td>
<td id="prof7" data-time="morning_practical" data-crs="Electric" data-prof="John Doe">18</td>
<td id="prof7" data-time="morning_free" data-crs="Electric" data-prof="John Doe">12</td>
<td id="prof7" data-time="evening_theory" data-crs="Electric" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_practical" data-crs="Electric" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_free" data-crs="Electric" data-prof="John Doe">0</td>
<td id="sob7" style="background:#d3d3d3 ;">30</td>
<td id="asr7" style="background:#d3d3d3 ;">0</td>
<td id="prof7">30</td>
<td id="prof7" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id7" class "ids"="" data-id="ids">2</td>
<td id="prof7">Mr</td>
<td id="prof7">John Doe</td>
<td id="prof7" style="font-size: 12px;">course2</td>
<td id="prof7" data-time="morning_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="morning_practical" data-crs="course2" data-prof="John Doe">18</td>
<td id="prof7" data-time="morning_free" data-crs="course2" data-prof="John Doe">12</td>
<td id="prof7" data-time="evening_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_practical" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_free" data-crs="course2" data-prof="John Doe">0</td>
<td id="sob7" style="background:#d3d3d3 ;">30</td>
<td id="asr7" style="background:#d3d3d3 ;">0</td>
<td id="prof7">30</td>
<td id="prof7" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id7" class "ids"="" data-id="ids">3</td>
<td id="prof7">Mr</td>
<td id="prof7">John Doe</td>
<td id="prof7" style="font-size: 12px;">course2</td>
<td id="prof7" data-time="morning_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="morning_practical" data-crs="course2" data-prof="John Doe">18</td>
<td id="prof7" data-time="morning_free" data-crs="course2" data-prof="John Doe">12</td>
<td id="prof7" data-time="evening_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_practical" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_free" data-crs="course2" data-prof="John Doe">0</td>
<td id="sob7" style="background:#d3d3d3 ;">30</td>
<td id="asr7" style="background:#d3d3d3 ;">0</td>
<td id="prof7">۳۰</td>
<td id="prof7" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id30" class "ids"="" data-id="ids">4</td>
<td id="prof30">Mr</td>
<td id="prof30">Robert Anderson</td>
<td id="prof12" style="font-size: 12px;">Course2 </td>
<td id="prof12" data-time="morning_theory" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="morning_practical" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="morning_free" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="evening_theory" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="evening_practical" data-crs="Course2" data-prof="Robert Anderson">6</td>
<td id="prof12" data-time="evening_free" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="sob12" style="background:#d3d3d3 ;">0</td>
<td id="asr12" style="background:#d3d3d3 ;">6</td>
<td id="prof12">6</td>
<td id="prof12" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id6" class "ids"="" data-id="ids">5</td>
<td id="prof6">Mr</td>
<td id="prof6">Jack Hulk</td>
<td id="prof5" style="font-size: 12px;">Course3</td>
<td id="prof5" data-time="morning_theory" data-crs="Course3" data-prof="Jack Hulk">8</td>
<td id="prof5" data-time="morning_practical" data-crs="Course3" data-prof="Jack Hulk">12</td>
<td id="prof5" data-time="morning_free" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_theory" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_practical" data-crs="Course3" data-prof="Jack Hulk">4</td>
<td id="prof5" data-time="evening_free" data-crs="Course3" data-prof="Jack Hulk">18</td>
<td id="sob5" style="background:#d3d3d3 ;">22</td>
<td id="asr5" style="background:#d3d3d3 ;">24</td>
<td id="prof5">46</td>
<td id="prof5" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id6" class "ids"="" data-id="ids">6</td>
<td id="prof6">Mr</td>
<td id="prof6">Jack Hulk</td>
<td id="prof5" style="font-size: 12px;">Course3</td>
<td id="prof5" data-time="morning_theory" data-crs="Course3" data-prof="Jack Hulk">8</td>
<td id="prof5" data-time="morning_practical" data-crs="Course3" data-prof="Jack Hulk">12</td>
<td id="prof5" data-time="morning_free" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_theory" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_practical" data-crs="Course3" data-prof="Jack Hulk">4</td>
<td id="prof5" data-time="evening_free" data-crs="Course3" data-prof="Jack Hulk">18</td>
<td id="sob5" style="background:#d3d3d3 ;">22</td>
<td id="asr5" style="background:#d3d3d3 ;">24</td>
<td id="prof5">46</td>
<td id="prof5" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id6" class "ids"="" data-id="ids">7</td>
<td id="prof6">Mr</td>
<td id="prof6">Jack Hulk</td>
<td id="prof5" style="font-size: 12px;">Course3</td>
<td id="prof5" data-time="morning_theory" data-crs="Course3" data-prof="Jack Hulk">8</td>
<td id="prof5" data-time="morning_practical" data-crs="Course3" data-prof="Jack Hulk">12</td>
<td id="prof5" data-time="morning_free" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_theory" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_practical" data-crs="Course3" data-prof="Jack Hulk">4</td>
<td id="prof5" data-time="evening_free" data-crs="Course3" data-prof="Jack Hulk">18</td>
<td id="sob5" style="background:#d3d3d3 ;">22</td>
<td id="asr5" style="background:#d3d3d3 ;">24</td>
<td id="prof5">46</td>
<td id="prof5" style="width: 160px;"></td>
</tr>
</tbody>
</table>

<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>












share|improve this question









New contributor




John is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • But the snippet example seems to give the right output. Could you instead give a snippet example where it goes wrong? If the snippet output is wrong, please add the table that you expect as output.
    – trincot
    2 days ago











  • This snippet already has wrong result. look at "Mr John Doe" in html code. He has 2 different courses but in results its not shown.
    – John
    2 days ago










  • So you want only rows to be removed when they are completely the same? Or only when some columns are the same?
    – trincot
    2 days ago










  • I want all rows for one person with same value to be merged.
    – John
    yesterday










  • What is "with same value"? Which value(s)? All rows have at least one cell different. For instance, the first cell (the "#" column) is always different in your example. Others have a different value in the "sum" column (like row 2 and 3).... It is not clear what you mean with "with same value".
    – trincot
    yesterday












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a table that have same values for each person, so I need to merge those rows for that person with same values. In some cases all rows with same value are merging but they are repeated with same values. In some other cases courses with same name are merged and this not right as I mentioned before only same values for one person should merge,
(so sorry for my bad English, don't know how to explain this I hope snippet show you enough)
Any solution for this?






function groupTable($rows, startIndex, total) 
if (total === 0)
return;

var i, currentIndex = startIndex,
count = 1,
lst = ;
var tds = $rows.find('td:eq(' + currentIndex + ')');
var ctrl = $(tds[0]);
lst.push($rows[0]);
for (i = 1; i <= tds.length; i++)
if (ctrl.attr("id") == $(tds[i]).attr("id"))
count++;
$(tds[i]).addClass('deleted');
lst.push($rows[i]);

else
if (count > 1)
ctrl.attr('rowspan', count);
groupTable($(lst), startIndex + 1, total - 1)

count = 1;
lst = ;
ctrl = $(tds[i]);
lst.push($rows[i]);



groupTable($('#myTable tr:has(td)'), 0, 14);
$('#myTable .deleted').remove();

<table class="table table-striped border-top table-bordered" id="myTable">
<thead>
<tr id="th1" style="background:#d3d3d3 ;">
<th>#</th>
<th>Grade</th>
<th>Teacher</th>
<th>Curse</th>
<th colspan="3">Morning</th>
<th colspan="3">Evening</th>
<th colspan="2" style="font-size: 12px;">Weekly hour</th>

<th>sum</th>
<th>-</th>
</tr>
<tr id="th2">
<th></th>
<th></th>
<th></th>
<th></th>
<th>theory</th>
<th>Practical</th>
<th>free</th>
<th>theory</th>
<th>Practical</th>
<th>free</th>
<th>Morning</th>
<th>Evening</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody id="details_view">
<tr id="p">
<td id="id7" class "ids"="" data-id="ids">1</td>
<td id="prof7">Mr</td>
<td id="prof7">John Doe</td>
<td id="prof7" style="font-size: 12px;">Electric</td>
<td id="prof7" data-time="morning_theory" data-crs="Electric" data-prof="John Doe">0</td>
<td id="prof7" data-time="morning_practical" data-crs="Electric" data-prof="John Doe">18</td>
<td id="prof7" data-time="morning_free" data-crs="Electric" data-prof="John Doe">12</td>
<td id="prof7" data-time="evening_theory" data-crs="Electric" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_practical" data-crs="Electric" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_free" data-crs="Electric" data-prof="John Doe">0</td>
<td id="sob7" style="background:#d3d3d3 ;">30</td>
<td id="asr7" style="background:#d3d3d3 ;">0</td>
<td id="prof7">30</td>
<td id="prof7" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id7" class "ids"="" data-id="ids">2</td>
<td id="prof7">Mr</td>
<td id="prof7">John Doe</td>
<td id="prof7" style="font-size: 12px;">course2</td>
<td id="prof7" data-time="morning_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="morning_practical" data-crs="course2" data-prof="John Doe">18</td>
<td id="prof7" data-time="morning_free" data-crs="course2" data-prof="John Doe">12</td>
<td id="prof7" data-time="evening_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_practical" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_free" data-crs="course2" data-prof="John Doe">0</td>
<td id="sob7" style="background:#d3d3d3 ;">30</td>
<td id="asr7" style="background:#d3d3d3 ;">0</td>
<td id="prof7">30</td>
<td id="prof7" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id7" class "ids"="" data-id="ids">3</td>
<td id="prof7">Mr</td>
<td id="prof7">John Doe</td>
<td id="prof7" style="font-size: 12px;">course2</td>
<td id="prof7" data-time="morning_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="morning_practical" data-crs="course2" data-prof="John Doe">18</td>
<td id="prof7" data-time="morning_free" data-crs="course2" data-prof="John Doe">12</td>
<td id="prof7" data-time="evening_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_practical" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_free" data-crs="course2" data-prof="John Doe">0</td>
<td id="sob7" style="background:#d3d3d3 ;">30</td>
<td id="asr7" style="background:#d3d3d3 ;">0</td>
<td id="prof7">۳۰</td>
<td id="prof7" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id30" class "ids"="" data-id="ids">4</td>
<td id="prof30">Mr</td>
<td id="prof30">Robert Anderson</td>
<td id="prof12" style="font-size: 12px;">Course2 </td>
<td id="prof12" data-time="morning_theory" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="morning_practical" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="morning_free" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="evening_theory" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="evening_practical" data-crs="Course2" data-prof="Robert Anderson">6</td>
<td id="prof12" data-time="evening_free" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="sob12" style="background:#d3d3d3 ;">0</td>
<td id="asr12" style="background:#d3d3d3 ;">6</td>
<td id="prof12">6</td>
<td id="prof12" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id6" class "ids"="" data-id="ids">5</td>
<td id="prof6">Mr</td>
<td id="prof6">Jack Hulk</td>
<td id="prof5" style="font-size: 12px;">Course3</td>
<td id="prof5" data-time="morning_theory" data-crs="Course3" data-prof="Jack Hulk">8</td>
<td id="prof5" data-time="morning_practical" data-crs="Course3" data-prof="Jack Hulk">12</td>
<td id="prof5" data-time="morning_free" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_theory" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_practical" data-crs="Course3" data-prof="Jack Hulk">4</td>
<td id="prof5" data-time="evening_free" data-crs="Course3" data-prof="Jack Hulk">18</td>
<td id="sob5" style="background:#d3d3d3 ;">22</td>
<td id="asr5" style="background:#d3d3d3 ;">24</td>
<td id="prof5">46</td>
<td id="prof5" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id6" class "ids"="" data-id="ids">6</td>
<td id="prof6">Mr</td>
<td id="prof6">Jack Hulk</td>
<td id="prof5" style="font-size: 12px;">Course3</td>
<td id="prof5" data-time="morning_theory" data-crs="Course3" data-prof="Jack Hulk">8</td>
<td id="prof5" data-time="morning_practical" data-crs="Course3" data-prof="Jack Hulk">12</td>
<td id="prof5" data-time="morning_free" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_theory" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_practical" data-crs="Course3" data-prof="Jack Hulk">4</td>
<td id="prof5" data-time="evening_free" data-crs="Course3" data-prof="Jack Hulk">18</td>
<td id="sob5" style="background:#d3d3d3 ;">22</td>
<td id="asr5" style="background:#d3d3d3 ;">24</td>
<td id="prof5">46</td>
<td id="prof5" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id6" class "ids"="" data-id="ids">7</td>
<td id="prof6">Mr</td>
<td id="prof6">Jack Hulk</td>
<td id="prof5" style="font-size: 12px;">Course3</td>
<td id="prof5" data-time="morning_theory" data-crs="Course3" data-prof="Jack Hulk">8</td>
<td id="prof5" data-time="morning_practical" data-crs="Course3" data-prof="Jack Hulk">12</td>
<td id="prof5" data-time="morning_free" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_theory" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_practical" data-crs="Course3" data-prof="Jack Hulk">4</td>
<td id="prof5" data-time="evening_free" data-crs="Course3" data-prof="Jack Hulk">18</td>
<td id="sob5" style="background:#d3d3d3 ;">22</td>
<td id="asr5" style="background:#d3d3d3 ;">24</td>
<td id="prof5">46</td>
<td id="prof5" style="width: 160px;"></td>
</tr>
</tbody>
</table>

<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>












share|improve this question









New contributor




John is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have a table that have same values for each person, so I need to merge those rows for that person with same values. In some cases all rows with same value are merging but they are repeated with same values. In some other cases courses with same name are merged and this not right as I mentioned before only same values for one person should merge,
(so sorry for my bad English, don't know how to explain this I hope snippet show you enough)
Any solution for this?






function groupTable($rows, startIndex, total) 
if (total === 0)
return;

var i, currentIndex = startIndex,
count = 1,
lst = ;
var tds = $rows.find('td:eq(' + currentIndex + ')');
var ctrl = $(tds[0]);
lst.push($rows[0]);
for (i = 1; i <= tds.length; i++)
if (ctrl.attr("id") == $(tds[i]).attr("id"))
count++;
$(tds[i]).addClass('deleted');
lst.push($rows[i]);

else
if (count > 1)
ctrl.attr('rowspan', count);
groupTable($(lst), startIndex + 1, total - 1)

count = 1;
lst = ;
ctrl = $(tds[i]);
lst.push($rows[i]);



groupTable($('#myTable tr:has(td)'), 0, 14);
$('#myTable .deleted').remove();

<table class="table table-striped border-top table-bordered" id="myTable">
<thead>
<tr id="th1" style="background:#d3d3d3 ;">
<th>#</th>
<th>Grade</th>
<th>Teacher</th>
<th>Curse</th>
<th colspan="3">Morning</th>
<th colspan="3">Evening</th>
<th colspan="2" style="font-size: 12px;">Weekly hour</th>

<th>sum</th>
<th>-</th>
</tr>
<tr id="th2">
<th></th>
<th></th>
<th></th>
<th></th>
<th>theory</th>
<th>Practical</th>
<th>free</th>
<th>theory</th>
<th>Practical</th>
<th>free</th>
<th>Morning</th>
<th>Evening</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody id="details_view">
<tr id="p">
<td id="id7" class "ids"="" data-id="ids">1</td>
<td id="prof7">Mr</td>
<td id="prof7">John Doe</td>
<td id="prof7" style="font-size: 12px;">Electric</td>
<td id="prof7" data-time="morning_theory" data-crs="Electric" data-prof="John Doe">0</td>
<td id="prof7" data-time="morning_practical" data-crs="Electric" data-prof="John Doe">18</td>
<td id="prof7" data-time="morning_free" data-crs="Electric" data-prof="John Doe">12</td>
<td id="prof7" data-time="evening_theory" data-crs="Electric" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_practical" data-crs="Electric" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_free" data-crs="Electric" data-prof="John Doe">0</td>
<td id="sob7" style="background:#d3d3d3 ;">30</td>
<td id="asr7" style="background:#d3d3d3 ;">0</td>
<td id="prof7">30</td>
<td id="prof7" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id7" class "ids"="" data-id="ids">2</td>
<td id="prof7">Mr</td>
<td id="prof7">John Doe</td>
<td id="prof7" style="font-size: 12px;">course2</td>
<td id="prof7" data-time="morning_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="morning_practical" data-crs="course2" data-prof="John Doe">18</td>
<td id="prof7" data-time="morning_free" data-crs="course2" data-prof="John Doe">12</td>
<td id="prof7" data-time="evening_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_practical" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_free" data-crs="course2" data-prof="John Doe">0</td>
<td id="sob7" style="background:#d3d3d3 ;">30</td>
<td id="asr7" style="background:#d3d3d3 ;">0</td>
<td id="prof7">30</td>
<td id="prof7" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id7" class "ids"="" data-id="ids">3</td>
<td id="prof7">Mr</td>
<td id="prof7">John Doe</td>
<td id="prof7" style="font-size: 12px;">course2</td>
<td id="prof7" data-time="morning_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="morning_practical" data-crs="course2" data-prof="John Doe">18</td>
<td id="prof7" data-time="morning_free" data-crs="course2" data-prof="John Doe">12</td>
<td id="prof7" data-time="evening_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_practical" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_free" data-crs="course2" data-prof="John Doe">0</td>
<td id="sob7" style="background:#d3d3d3 ;">30</td>
<td id="asr7" style="background:#d3d3d3 ;">0</td>
<td id="prof7">۳۰</td>
<td id="prof7" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id30" class "ids"="" data-id="ids">4</td>
<td id="prof30">Mr</td>
<td id="prof30">Robert Anderson</td>
<td id="prof12" style="font-size: 12px;">Course2 </td>
<td id="prof12" data-time="morning_theory" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="morning_practical" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="morning_free" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="evening_theory" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="evening_practical" data-crs="Course2" data-prof="Robert Anderson">6</td>
<td id="prof12" data-time="evening_free" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="sob12" style="background:#d3d3d3 ;">0</td>
<td id="asr12" style="background:#d3d3d3 ;">6</td>
<td id="prof12">6</td>
<td id="prof12" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id6" class "ids"="" data-id="ids">5</td>
<td id="prof6">Mr</td>
<td id="prof6">Jack Hulk</td>
<td id="prof5" style="font-size: 12px;">Course3</td>
<td id="prof5" data-time="morning_theory" data-crs="Course3" data-prof="Jack Hulk">8</td>
<td id="prof5" data-time="morning_practical" data-crs="Course3" data-prof="Jack Hulk">12</td>
<td id="prof5" data-time="morning_free" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_theory" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_practical" data-crs="Course3" data-prof="Jack Hulk">4</td>
<td id="prof5" data-time="evening_free" data-crs="Course3" data-prof="Jack Hulk">18</td>
<td id="sob5" style="background:#d3d3d3 ;">22</td>
<td id="asr5" style="background:#d3d3d3 ;">24</td>
<td id="prof5">46</td>
<td id="prof5" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id6" class "ids"="" data-id="ids">6</td>
<td id="prof6">Mr</td>
<td id="prof6">Jack Hulk</td>
<td id="prof5" style="font-size: 12px;">Course3</td>
<td id="prof5" data-time="morning_theory" data-crs="Course3" data-prof="Jack Hulk">8</td>
<td id="prof5" data-time="morning_practical" data-crs="Course3" data-prof="Jack Hulk">12</td>
<td id="prof5" data-time="morning_free" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_theory" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_practical" data-crs="Course3" data-prof="Jack Hulk">4</td>
<td id="prof5" data-time="evening_free" data-crs="Course3" data-prof="Jack Hulk">18</td>
<td id="sob5" style="background:#d3d3d3 ;">22</td>
<td id="asr5" style="background:#d3d3d3 ;">24</td>
<td id="prof5">46</td>
<td id="prof5" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id6" class "ids"="" data-id="ids">7</td>
<td id="prof6">Mr</td>
<td id="prof6">Jack Hulk</td>
<td id="prof5" style="font-size: 12px;">Course3</td>
<td id="prof5" data-time="morning_theory" data-crs="Course3" data-prof="Jack Hulk">8</td>
<td id="prof5" data-time="morning_practical" data-crs="Course3" data-prof="Jack Hulk">12</td>
<td id="prof5" data-time="morning_free" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_theory" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_practical" data-crs="Course3" data-prof="Jack Hulk">4</td>
<td id="prof5" data-time="evening_free" data-crs="Course3" data-prof="Jack Hulk">18</td>
<td id="sob5" style="background:#d3d3d3 ;">22</td>
<td id="asr5" style="background:#d3d3d3 ;">24</td>
<td id="prof5">46</td>
<td id="prof5" style="width: 160px;"></td>
</tr>
</tbody>
</table>

<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>








function groupTable($rows, startIndex, total) 
if (total === 0)
return;

var i, currentIndex = startIndex,
count = 1,
lst = ;
var tds = $rows.find('td:eq(' + currentIndex + ')');
var ctrl = $(tds[0]);
lst.push($rows[0]);
for (i = 1; i <= tds.length; i++)
if (ctrl.attr("id") == $(tds[i]).attr("id"))
count++;
$(tds[i]).addClass('deleted');
lst.push($rows[i]);

else
if (count > 1)
ctrl.attr('rowspan', count);
groupTable($(lst), startIndex + 1, total - 1)

count = 1;
lst = ;
ctrl = $(tds[i]);
lst.push($rows[i]);



groupTable($('#myTable tr:has(td)'), 0, 14);
$('#myTable .deleted').remove();

<table class="table table-striped border-top table-bordered" id="myTable">
<thead>
<tr id="th1" style="background:#d3d3d3 ;">
<th>#</th>
<th>Grade</th>
<th>Teacher</th>
<th>Curse</th>
<th colspan="3">Morning</th>
<th colspan="3">Evening</th>
<th colspan="2" style="font-size: 12px;">Weekly hour</th>

<th>sum</th>
<th>-</th>
</tr>
<tr id="th2">
<th></th>
<th></th>
<th></th>
<th></th>
<th>theory</th>
<th>Practical</th>
<th>free</th>
<th>theory</th>
<th>Practical</th>
<th>free</th>
<th>Morning</th>
<th>Evening</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody id="details_view">
<tr id="p">
<td id="id7" class "ids"="" data-id="ids">1</td>
<td id="prof7">Mr</td>
<td id="prof7">John Doe</td>
<td id="prof7" style="font-size: 12px;">Electric</td>
<td id="prof7" data-time="morning_theory" data-crs="Electric" data-prof="John Doe">0</td>
<td id="prof7" data-time="morning_practical" data-crs="Electric" data-prof="John Doe">18</td>
<td id="prof7" data-time="morning_free" data-crs="Electric" data-prof="John Doe">12</td>
<td id="prof7" data-time="evening_theory" data-crs="Electric" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_practical" data-crs="Electric" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_free" data-crs="Electric" data-prof="John Doe">0</td>
<td id="sob7" style="background:#d3d3d3 ;">30</td>
<td id="asr7" style="background:#d3d3d3 ;">0</td>
<td id="prof7">30</td>
<td id="prof7" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id7" class "ids"="" data-id="ids">2</td>
<td id="prof7">Mr</td>
<td id="prof7">John Doe</td>
<td id="prof7" style="font-size: 12px;">course2</td>
<td id="prof7" data-time="morning_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="morning_practical" data-crs="course2" data-prof="John Doe">18</td>
<td id="prof7" data-time="morning_free" data-crs="course2" data-prof="John Doe">12</td>
<td id="prof7" data-time="evening_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_practical" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_free" data-crs="course2" data-prof="John Doe">0</td>
<td id="sob7" style="background:#d3d3d3 ;">30</td>
<td id="asr7" style="background:#d3d3d3 ;">0</td>
<td id="prof7">30</td>
<td id="prof7" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id7" class "ids"="" data-id="ids">3</td>
<td id="prof7">Mr</td>
<td id="prof7">John Doe</td>
<td id="prof7" style="font-size: 12px;">course2</td>
<td id="prof7" data-time="morning_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="morning_practical" data-crs="course2" data-prof="John Doe">18</td>
<td id="prof7" data-time="morning_free" data-crs="course2" data-prof="John Doe">12</td>
<td id="prof7" data-time="evening_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_practical" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_free" data-crs="course2" data-prof="John Doe">0</td>
<td id="sob7" style="background:#d3d3d3 ;">30</td>
<td id="asr7" style="background:#d3d3d3 ;">0</td>
<td id="prof7">۳۰</td>
<td id="prof7" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id30" class "ids"="" data-id="ids">4</td>
<td id="prof30">Mr</td>
<td id="prof30">Robert Anderson</td>
<td id="prof12" style="font-size: 12px;">Course2 </td>
<td id="prof12" data-time="morning_theory" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="morning_practical" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="morning_free" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="evening_theory" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="evening_practical" data-crs="Course2" data-prof="Robert Anderson">6</td>
<td id="prof12" data-time="evening_free" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="sob12" style="background:#d3d3d3 ;">0</td>
<td id="asr12" style="background:#d3d3d3 ;">6</td>
<td id="prof12">6</td>
<td id="prof12" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id6" class "ids"="" data-id="ids">5</td>
<td id="prof6">Mr</td>
<td id="prof6">Jack Hulk</td>
<td id="prof5" style="font-size: 12px;">Course3</td>
<td id="prof5" data-time="morning_theory" data-crs="Course3" data-prof="Jack Hulk">8</td>
<td id="prof5" data-time="morning_practical" data-crs="Course3" data-prof="Jack Hulk">12</td>
<td id="prof5" data-time="morning_free" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_theory" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_practical" data-crs="Course3" data-prof="Jack Hulk">4</td>
<td id="prof5" data-time="evening_free" data-crs="Course3" data-prof="Jack Hulk">18</td>
<td id="sob5" style="background:#d3d3d3 ;">22</td>
<td id="asr5" style="background:#d3d3d3 ;">24</td>
<td id="prof5">46</td>
<td id="prof5" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id6" class "ids"="" data-id="ids">6</td>
<td id="prof6">Mr</td>
<td id="prof6">Jack Hulk</td>
<td id="prof5" style="font-size: 12px;">Course3</td>
<td id="prof5" data-time="morning_theory" data-crs="Course3" data-prof="Jack Hulk">8</td>
<td id="prof5" data-time="morning_practical" data-crs="Course3" data-prof="Jack Hulk">12</td>
<td id="prof5" data-time="morning_free" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_theory" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_practical" data-crs="Course3" data-prof="Jack Hulk">4</td>
<td id="prof5" data-time="evening_free" data-crs="Course3" data-prof="Jack Hulk">18</td>
<td id="sob5" style="background:#d3d3d3 ;">22</td>
<td id="asr5" style="background:#d3d3d3 ;">24</td>
<td id="prof5">46</td>
<td id="prof5" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id6" class "ids"="" data-id="ids">7</td>
<td id="prof6">Mr</td>
<td id="prof6">Jack Hulk</td>
<td id="prof5" style="font-size: 12px;">Course3</td>
<td id="prof5" data-time="morning_theory" data-crs="Course3" data-prof="Jack Hulk">8</td>
<td id="prof5" data-time="morning_practical" data-crs="Course3" data-prof="Jack Hulk">12</td>
<td id="prof5" data-time="morning_free" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_theory" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_practical" data-crs="Course3" data-prof="Jack Hulk">4</td>
<td id="prof5" data-time="evening_free" data-crs="Course3" data-prof="Jack Hulk">18</td>
<td id="sob5" style="background:#d3d3d3 ;">22</td>
<td id="asr5" style="background:#d3d3d3 ;">24</td>
<td id="prof5">46</td>
<td id="prof5" style="width: 160px;"></td>
</tr>
</tbody>
</table>

<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>





function groupTable($rows, startIndex, total) 
if (total === 0)
return;

var i, currentIndex = startIndex,
count = 1,
lst = ;
var tds = $rows.find('td:eq(' + currentIndex + ')');
var ctrl = $(tds[0]);
lst.push($rows[0]);
for (i = 1; i <= tds.length; i++)
if (ctrl.attr("id") == $(tds[i]).attr("id"))
count++;
$(tds[i]).addClass('deleted');
lst.push($rows[i]);

else
if (count > 1)
ctrl.attr('rowspan', count);
groupTable($(lst), startIndex + 1, total - 1)

count = 1;
lst = ;
ctrl = $(tds[i]);
lst.push($rows[i]);



groupTable($('#myTable tr:has(td)'), 0, 14);
$('#myTable .deleted').remove();

<table class="table table-striped border-top table-bordered" id="myTable">
<thead>
<tr id="th1" style="background:#d3d3d3 ;">
<th>#</th>
<th>Grade</th>
<th>Teacher</th>
<th>Curse</th>
<th colspan="3">Morning</th>
<th colspan="3">Evening</th>
<th colspan="2" style="font-size: 12px;">Weekly hour</th>

<th>sum</th>
<th>-</th>
</tr>
<tr id="th2">
<th></th>
<th></th>
<th></th>
<th></th>
<th>theory</th>
<th>Practical</th>
<th>free</th>
<th>theory</th>
<th>Practical</th>
<th>free</th>
<th>Morning</th>
<th>Evening</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody id="details_view">
<tr id="p">
<td id="id7" class "ids"="" data-id="ids">1</td>
<td id="prof7">Mr</td>
<td id="prof7">John Doe</td>
<td id="prof7" style="font-size: 12px;">Electric</td>
<td id="prof7" data-time="morning_theory" data-crs="Electric" data-prof="John Doe">0</td>
<td id="prof7" data-time="morning_practical" data-crs="Electric" data-prof="John Doe">18</td>
<td id="prof7" data-time="morning_free" data-crs="Electric" data-prof="John Doe">12</td>
<td id="prof7" data-time="evening_theory" data-crs="Electric" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_practical" data-crs="Electric" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_free" data-crs="Electric" data-prof="John Doe">0</td>
<td id="sob7" style="background:#d3d3d3 ;">30</td>
<td id="asr7" style="background:#d3d3d3 ;">0</td>
<td id="prof7">30</td>
<td id="prof7" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id7" class "ids"="" data-id="ids">2</td>
<td id="prof7">Mr</td>
<td id="prof7">John Doe</td>
<td id="prof7" style="font-size: 12px;">course2</td>
<td id="prof7" data-time="morning_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="morning_practical" data-crs="course2" data-prof="John Doe">18</td>
<td id="prof7" data-time="morning_free" data-crs="course2" data-prof="John Doe">12</td>
<td id="prof7" data-time="evening_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_practical" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_free" data-crs="course2" data-prof="John Doe">0</td>
<td id="sob7" style="background:#d3d3d3 ;">30</td>
<td id="asr7" style="background:#d3d3d3 ;">0</td>
<td id="prof7">30</td>
<td id="prof7" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id7" class "ids"="" data-id="ids">3</td>
<td id="prof7">Mr</td>
<td id="prof7">John Doe</td>
<td id="prof7" style="font-size: 12px;">course2</td>
<td id="prof7" data-time="morning_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="morning_practical" data-crs="course2" data-prof="John Doe">18</td>
<td id="prof7" data-time="morning_free" data-crs="course2" data-prof="John Doe">12</td>
<td id="prof7" data-time="evening_theory" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_practical" data-crs="course2" data-prof="John Doe">0</td>
<td id="prof7" data-time="evening_free" data-crs="course2" data-prof="John Doe">0</td>
<td id="sob7" style="background:#d3d3d3 ;">30</td>
<td id="asr7" style="background:#d3d3d3 ;">0</td>
<td id="prof7">۳۰</td>
<td id="prof7" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id30" class "ids"="" data-id="ids">4</td>
<td id="prof30">Mr</td>
<td id="prof30">Robert Anderson</td>
<td id="prof12" style="font-size: 12px;">Course2 </td>
<td id="prof12" data-time="morning_theory" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="morning_practical" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="morning_free" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="evening_theory" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="prof12" data-time="evening_practical" data-crs="Course2" data-prof="Robert Anderson">6</td>
<td id="prof12" data-time="evening_free" data-crs="Course2" data-prof="Robert Anderson">0</td>
<td id="sob12" style="background:#d3d3d3 ;">0</td>
<td id="asr12" style="background:#d3d3d3 ;">6</td>
<td id="prof12">6</td>
<td id="prof12" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id6" class "ids"="" data-id="ids">5</td>
<td id="prof6">Mr</td>
<td id="prof6">Jack Hulk</td>
<td id="prof5" style="font-size: 12px;">Course3</td>
<td id="prof5" data-time="morning_theory" data-crs="Course3" data-prof="Jack Hulk">8</td>
<td id="prof5" data-time="morning_practical" data-crs="Course3" data-prof="Jack Hulk">12</td>
<td id="prof5" data-time="morning_free" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_theory" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_practical" data-crs="Course3" data-prof="Jack Hulk">4</td>
<td id="prof5" data-time="evening_free" data-crs="Course3" data-prof="Jack Hulk">18</td>
<td id="sob5" style="background:#d3d3d3 ;">22</td>
<td id="asr5" style="background:#d3d3d3 ;">24</td>
<td id="prof5">46</td>
<td id="prof5" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id6" class "ids"="" data-id="ids">6</td>
<td id="prof6">Mr</td>
<td id="prof6">Jack Hulk</td>
<td id="prof5" style="font-size: 12px;">Course3</td>
<td id="prof5" data-time="morning_theory" data-crs="Course3" data-prof="Jack Hulk">8</td>
<td id="prof5" data-time="morning_practical" data-crs="Course3" data-prof="Jack Hulk">12</td>
<td id="prof5" data-time="morning_free" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_theory" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_practical" data-crs="Course3" data-prof="Jack Hulk">4</td>
<td id="prof5" data-time="evening_free" data-crs="Course3" data-prof="Jack Hulk">18</td>
<td id="sob5" style="background:#d3d3d3 ;">22</td>
<td id="asr5" style="background:#d3d3d3 ;">24</td>
<td id="prof5">46</td>
<td id="prof5" style="width: 160px;"></td>
</tr>
<tr id="p">
<td id="id6" class "ids"="" data-id="ids">7</td>
<td id="prof6">Mr</td>
<td id="prof6">Jack Hulk</td>
<td id="prof5" style="font-size: 12px;">Course3</td>
<td id="prof5" data-time="morning_theory" data-crs="Course3" data-prof="Jack Hulk">8</td>
<td id="prof5" data-time="morning_practical" data-crs="Course3" data-prof="Jack Hulk">12</td>
<td id="prof5" data-time="morning_free" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_theory" data-crs="Course3" data-prof="Jack Hulk">2</td>
<td id="prof5" data-time="evening_practical" data-crs="Course3" data-prof="Jack Hulk">4</td>
<td id="prof5" data-time="evening_free" data-crs="Course3" data-prof="Jack Hulk">18</td>
<td id="sob5" style="background:#d3d3d3 ;">22</td>
<td id="asr5" style="background:#d3d3d3 ;">24</td>
<td id="prof5">46</td>
<td id="prof5" style="width: 160px;"></td>
</tr>
</tbody>
</table>

<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>






javascript jquery html-table






share|improve this question









New contributor




John is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




John is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday









Brian Tompsett - 汤莱恩

4,153133699




4,153133699






New contributor




John is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









John

61




61




New contributor




John is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





John is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






John is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • But the snippet example seems to give the right output. Could you instead give a snippet example where it goes wrong? If the snippet output is wrong, please add the table that you expect as output.
    – trincot
    2 days ago











  • This snippet already has wrong result. look at "Mr John Doe" in html code. He has 2 different courses but in results its not shown.
    – John
    2 days ago










  • So you want only rows to be removed when they are completely the same? Or only when some columns are the same?
    – trincot
    2 days ago










  • I want all rows for one person with same value to be merged.
    – John
    yesterday










  • What is "with same value"? Which value(s)? All rows have at least one cell different. For instance, the first cell (the "#" column) is always different in your example. Others have a different value in the "sum" column (like row 2 and 3).... It is not clear what you mean with "with same value".
    – trincot
    yesterday
















  • But the snippet example seems to give the right output. Could you instead give a snippet example where it goes wrong? If the snippet output is wrong, please add the table that you expect as output.
    – trincot
    2 days ago











  • This snippet already has wrong result. look at "Mr John Doe" in html code. He has 2 different courses but in results its not shown.
    – John
    2 days ago










  • So you want only rows to be removed when they are completely the same? Or only when some columns are the same?
    – trincot
    2 days ago










  • I want all rows for one person with same value to be merged.
    – John
    yesterday










  • What is "with same value"? Which value(s)? All rows have at least one cell different. For instance, the first cell (the "#" column) is always different in your example. Others have a different value in the "sum" column (like row 2 and 3).... It is not clear what you mean with "with same value".
    – trincot
    yesterday















But the snippet example seems to give the right output. Could you instead give a snippet example where it goes wrong? If the snippet output is wrong, please add the table that you expect as output.
– trincot
2 days ago





But the snippet example seems to give the right output. Could you instead give a snippet example where it goes wrong? If the snippet output is wrong, please add the table that you expect as output.
– trincot
2 days ago













This snippet already has wrong result. look at "Mr John Doe" in html code. He has 2 different courses but in results its not shown.
– John
2 days ago




This snippet already has wrong result. look at "Mr John Doe" in html code. He has 2 different courses but in results its not shown.
– John
2 days ago












So you want only rows to be removed when they are completely the same? Or only when some columns are the same?
– trincot
2 days ago




So you want only rows to be removed when they are completely the same? Or only when some columns are the same?
– trincot
2 days ago












I want all rows for one person with same value to be merged.
– John
yesterday




I want all rows for one person with same value to be merged.
– John
yesterday












What is "with same value"? Which value(s)? All rows have at least one cell different. For instance, the first cell (the "#" column) is always different in your example. Others have a different value in the "sum" column (like row 2 and 3).... It is not clear what you mean with "with same value".
– trincot
yesterday




What is "with same value"? Which value(s)? All rows have at least one cell different. For instance, the first cell (the "#" column) is always different in your example. Others have a different value in the "sum" column (like row 2 and 3).... It is not clear what you mean with "with same value".
– trincot
yesterday

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






John is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234528%2fmerge-table-rows-with-jquery%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes








John is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















John is a new contributor. Be nice, and check out our Code of Conduct.












John is a new contributor. Be nice, and check out our Code of Conduct.











John is a new contributor. Be nice, and check out our Code of Conduct.













 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234528%2fmerge-table-rows-with-jquery%23new-answer', 'question_page');

);

Post as a guest














































































這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto