Reason is that in your insertAfter selctor you provide tbody
and which will return all of them and insert after all of the available tbody's, instead just append it to the current tbody i.e this
.
Change
$("<tfoot><tr><td colspan=" + columnCount + " class=\"view_more_accordian\" style=\"text-align:center;\">View more</td></tr></tfoot>")
.insertAfter("tbody");
to
$("<tfoot><tr><td colspan=" + columnCount + " class=\"view_more_accordian\" style=\"text-align:center;\">View more</td></tr></tfoot>")
.insertAfter(this);
Also you can avoid using the first .each
(based on your shown code) instead you can just do
$(".accordion-group table tbody").each(function () {
...
});