Saturday, 15 September 2012

javascript - How to get min and max number from selected rows of table? -



javascript - How to get min and max number from selected rows of table? -

this refers previous question.

how highlight/color multiple rows on selection?

<table id="toppings" border="1" cellpadding="2"> <tr id="id1"> <td>3</td> <td>row12</td> <td>row13</td> </tr> <tr id="id2"> <td>12</td> <td>row22</td> <td>row23</td> </tr> <tr id="id3"> <td>15</td> <td>row32</td> <td>row33</td> </tr> <tr id="id4"> <td>22</td> <td>row42</td> <td>row43</td> </tr> <tr id="id5"> <td>23</td> <td>row52</td> <td>row53</td> </tr> <tr id="id6"> <td>55</td> <td>row62</td> <td>row63</td> </tr> </table>

javascript code:

//get list of rows in table var table = document.getelementbyid("toppings"); var rows = table.getelementsbytagname("tr"); var selectedrow; //row callback; reset selected row , select new 1 function selectrow(row) { if (selectedrow !== undefined) { selectedrow.style.background = "#d8da3d"; } selectedrow = row; selectedrow.style.background = "white"; } //attach callback rows (var = 0; < rows.length; i++) { var idx = i; rows[idx].addeventlistener("click", function(){selectrow(rows[idx])}); }

but time have added event table row selection , trying min , max value selected rows (first column). above table, if select middle 4 rows, should min = 12 , max = 23. how can implemented.

you can have 2 functions. show getminvalueexample().

function getminvalueexample(rows){ var minvalue = null; (var = 0; < rows.length; i++){ var firsttd = rows[i].getelementsbytagname('td')[0]; var currentvalue = parseint(firsttd.innerhtml); if(minvalue == null || minvalue > currentvalue) minvalue = currentvalue; } homecoming minvalue; }

(not test can contain type errors should idea) if phone call after you've declared rows returns min value. , if phone call 1 max value

function getmaxvalueexample(rows){ var maxvalue = null; (var = 0; < rows.length; i++){ var firsttd = rows[i].getelementsbytagname('td')[0]; var currentvalue = parseint(firsttd.innerhtml); if(maxvalue == null || maxvalue < currentvalue) maxvalue = currentvalue; } homecoming maxvalue; }

javascript table

No comments:

Post a Comment