admin管理员组文章数量:1430077
I fetched data from Mysql for a table. Each row has 5 cell.
If the 4th cell values of two rows are the same then I want to merge those rows.
I tried using javascript arrays but I couldn't merge the rows. Can anyone share their ideas or send me any sample code. Thanks in advance.
I fetched data from Mysql for a table. Each row has 5 cell.
If the 4th cell values of two rows are the same then I want to merge those rows.
I tried using javascript arrays but I couldn't merge the rows. Can anyone share their ideas or send me any sample code. Thanks in advance.
Share Improve this question edited Aug 25, 2011 at 9:47 Nachshon Schwartz 15.8k20 gold badges63 silver badges99 bronze badges asked Aug 25, 2011 at 9:05 Rosi EvangelinRosi Evangelin 553 gold badges3 silver badges11 bronze badges 1- 2 What do you mean with "merge rows"? Try giving an example: what do you have, and what do you want? – Daan Wilmer Commented Aug 25, 2011 at 9:16
4 Answers
Reset to default 1I was like posting a picture, but because im new and doesnt have reput, it was not uploaded... well, here the link of the photo
If that's what you asking for... I have a kinda long code.. i dont know if it is efficient but its working for me
int itemsize = 5; //number of rows that is expected to be in merged
for(int item=0; i<itemsize ; i++){//this jsp
%>
<tr>
<td><%=value%></td>
<td><%=value%></td>
<%
if(itemsize > 1 && item==0){ //if the itemsize more than 1, and it is the first row, it will put a rowspan that has the value of the itemsize or the number of row to be merged
%>
<td rowspan='<%=itemsize %>'><%=value%></td>
<td rowspan='<%=itemsize %>'><%=value%></td>
<td rowspan='<%=itemsize %>'><%=value%></td>
<%
}else if(itemsize > 1 && item>0){
//on the second loop this will be executed but bcoz we have rowspan, <td> should be eliminated on the succeeding row*
}else{ // but if there is only one row to be displayed, no rowspan is needed, so ordinary <td will be written..
%>
<td><%=value%></td>
<td><%=value%></td>
<td><%=value%></td>
<%
}
%>
</tr>
<%
}
EDITED:
This is my new code. i made a sample using php. try to run it. Sorry for the code, its my first PHP.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
body{ font-family: tahoma, Arial; font-size: 9pt;}
table {border-collapse:collapse; font: tahoma, Arial 9pt;}
table th{font-family: tahoma, Arial, Geneva, sans-serif; font-size:9pt; width:75px; background-color:#999; color:#000; text-transform:uppercase;}
table th, td{ padding:4px 6px; text-transform:capitalize;}
</style>
</head>
<body>
<table cellspacing="0" cellpadding="0" border="1" bordercolor="#000000">
<thead>
<th style="width:10px; text-align:right">Count</th>
<th>Detail 1</th>
<th>Head 2</th>
<th>Head 3</th>
<th>Head 4</th>
<th>Head 5</th>
<th>Head 6</th>
<th>Detail 2</th>
</thead>
<tbody>
<?php
$arr_detail_1 = array("angelo", "philip", "raymond", "jonathan", "pedro");
$arr_detail_2 = array("Cavite", "Laguna", "Quezon", "Japan", "Surigao");
$arr_detail_3 = array("10-dec", "11-Sep", "12-Feb", "22-July", "15-Oct");
$arr_detail_4 = array("Pink", "blue", "Green", "White", "Purple");
$arr_detail_5 = array("Computer", "Door Lock", "Pencil", "Cup", "Jeans");
$a=array("screw");
$b=array("ball","paper","Big","Liquid","id");
$c=array("Sewing");
$d=array("Running","Biking","Swimming");
$e=array("Tree","planting","root","fruits","Rain","Grass");
$f=array("driver");
$g=array("Pen","bag","Brother","eraser","lace");
$h=array("machine");
$i=array("barefoot","with no bike","technique");
$j=array("planting","tree","crops","bearig-trees","Forest","Hopper");
$arr1 = array($a, $b, $c, $d, $e);
$arr2 = array($f, $g, $h, $i, $j);
$rowcount = 1;
for ($i=0; $i<count($arr1); $i++){
// echo "The number is " . $i . "<br />";
$temp_arr_1 = $arr1[$i];
$temp_arr_2 = $arr2[$i];
for($x=0; $x<count($temp_arr_1); $x++){
?>
<tr>
<td align="right"><?php echo $rowcount; ?>.</td>
<td><?php echo $temp_arr_1[$x]; ?></td>
<td><?php echo $temp_arr_2[$x]; ?></td>
<?php
$span_count = count($temp_arr_1);
if($span_count>1 && $x==0){
?>
<td rowspan="<?php echo count($temp_arr_1);?>"><?php echo $arr_detail_1[$i]; ?></td>
<td rowspan="<?php echo count($temp_arr_1);?>"><?php echo $arr_detail_2[$i]; ?></td>
<td rowspan="<?php echo count($temp_arr_1);?>"><?php echo $arr_detail_3[$i]; ?></td>
<td rowspan="<?php echo count($temp_arr_1);?>"><?php echo $arr_detail_4[$i]; ?></td>
<td rowspan="<?php echo count($temp_arr_1);?>"><?php echo $arr_detail_5[$i]; ?></td>
<?php
}else if($span_count>1 && $x>0){
//
}else{
?>
<td><?php echo $arr_detail_1[$i]; ?></td>
<td><?php echo $arr_detail_2[$i]; ?></td>
<td><?php echo $arr_detail_3[$i]; ?></td>
<td><?php echo $arr_detail_4[$i]; ?></td>
<td><?php echo $arr_detail_5[$i]; ?></td>
<?php
}
?>
</tr>
<?php
$rowcount++;
}
}
?>
</tbody>
</table>
</body>
</html>
i did use array to act as dbase... ;] and do the code in way like i do it with JSP so i hope you'll understand. im really new at php. even in java, im not sure if what im doing is correct! i code as long as it run, even if its slow.. but im trying to fix that! good luck to your project evangeline.
A really quick sample if you did want to do it in javascript for some reason:
<html>
<head>
<script src="http://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<table border="2" id="aTable">
<tr>
<td>First</td>
<td>A</td>
</tr>
<tr>
<td>First</td>
<td>B</td>
</tr>
<tr>
<td>Second</td>
<td>V</td>
</tr>
<tr>
<td>Third</td>
<td>D</td>
</tr>
<tr>
<td>Third</td>
<td>E</td>
</tr>
</table>
</body>
<script type="text/javascript">
function MergeCommonRows(table, columnIndexToMerge){
previous = null;
cellToExtend = null;
table.find("td:nth-child("+columnIndexToMerge+")").each(function(){
jthis = $(this);
content = jthis.text()
if(previous == content){
jthis.remove();
if(cellToExtend.attr("rowspan") == undefined){
cellToExtend.attr("rowspan", 2);
}
else{
currentrowspan = parseInt(cellToExtend.attr("rowspan"));
cellToExtend.attr("rowspan", currentrowspan+1);
}
}
else{
previous = content;
cellToExtend = jthis;
}
});
};
MergeCommonRows($("#aTable"), 1);
</script>
</html>
Its not a good idea to use javascript. First of all, some users may have deactivated js, or even not available (eg mobile phone browser).
I suggest to get the data from mysql, loop through them in php (if you use php), copy the data to a new array while doing any necessary merging and then print the new "correct" array.
Is it php that you are using? The sample code you need, has to be server side (eg php).
You should give the last cell in the first row rowspan="2"
and omit the last cell in the next row.
<table>
<tr>
<td>some value</td>
<td>some value</td>
<td>some value</td>
<td>some value</td>
<td rowspan="2">some value</td>
</tr>
<tr>
<td>some value</td>
<td>some value</td>
<td>some value</td>
<td>some value</td>
</tr>
</table>
本文标签: javascriptMerge HTML TABLE ROWS if condition trueStack Overflow
版权声明:本文标题:javascript - Merge HTML TABLE ROWS if condition true - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745549693a2662879.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论