table merges rows with the same name according to the first column (merge rows)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled document</title>
<script type="text/javascript" src="jquery-1.6.1.min.js"></script>
<script type="text/javascript">
	$(function(){
		var $table=$(".abc");
		var tagStr="";
		var name="";
		$table.find("tr:gt(0)").each(function(i){
   			var $tr=$(this);
			var $td=$tr.children("td:eq(0)");
			var tdname=$td.text();
			if(name==tdname){
				//represent the same name
				$td.remove();
			}else{
				//
				tagStr=tagStr+(i+1)+",";
			}
			name=tdname;
 		});
		// split the token string	
		var strs= new Array();
		strs=tagStr.substring(0,tagStr.length-1).split(",");
		var count=$table.find("tr:gt(0)").length+1;
		for(i=0;i<strs.length;i++){
		    var s="tr:eq("+strs[i]+")"
			if(i<strs.length-1){
				// get the number of rows to merge
				$table.find(s).children("td:eq(0)").attr("rowspan",(strs[i+1]-strs[i]));	
			}else{
				$table.find(s).children("td:eq(0)").attr("rowspan",(count-strs[i]));
			}
		}
	})
	
</script>
</head>

<body>
	<form action="" method="get">
    <table class="abc" width="676" border="1" align="center">
  <tr>
    <th width="191">姓名</td>
    <th width="235">性别</td>
    <th width="228">爱好</td>
  </tr>
  <tr>
    <td>Zhang San</td>
    <td>男</td>
    <td>Watching a movie</td>
  </tr>
  <tr>
    <td>王五</td>
    <td>男</td>
    <td>Play games</td>
  </tr>
  <tr>
    <td>王五</td>
    <td>女</td>
    <td>Swimming</td>
  </tr>
  <tr>
    <td>Cai Liu</td>
    <td>男</td>
    <td>Playing cards</td>
  </tr>
  <tr>
    <td>Cai Liu</td>
    <td>男</td>
    <td>AAA</td>
  </tr>
  <tr>
    <td>Cai Liu</td>
    <td>男</td>
    <td>BBB</td>
  </tr>
  <tr>
    <td>XXX</td>
    <td>男</td>
    <td>RRR</td>
  </tr>
  <tr>
    <td>XXX</td>
    <td>男</td>
    <td>TTT</td>
  </tr>
  <tr>
    <td>XXX</td>
    <td>男</td>
    <td>YYY</td>
  </tr>
  <tr>
    <td>XXX</td>
    <td>男</td>
    <td>UUU</td>
  </tr>
  <tr>
    <td>XXX</td>
    <td>男</td>
    <td>OOO</td>
  </tr>
</table>

    </form>
</body>
</html>


Attached is the merge specified column method file

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326396730&siteId=291194637