Advertisement
thetenfold

Untitled

Aug 7th, 2013
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style type="text/css">
  5. #data tr.normal td {
  6.     color: #235A81;
  7.     background-color: white;
  8. }
  9. #data tr.highlighted td {
  10.     color: #FFFFFF;
  11.     background-color: #235A81;
  12. }
  13.  
  14.  
  15.  
  16.  
  17. </style>
  18. <script type='text/javascript'>
  19. function test() {
  20.  
  21. var table = document.getElementById("data");
  22. var thead = table.getElementsByTagName("thead")[0];
  23. var tbody = table.getElementsByTagName("tbody")[0];
  24. var ishigh
  25.  
  26.  
  27. tbody.onclick = function (e) {
  28.   e = e || window.event;
  29.   var td = e.target || e.srcElement
  30.   var row = td.parentNode;
  31.  
  32.   if (ishigh&&ishigh!=row){
  33.     ishigh.className='';
  34.   }
  35.   row.className = row.className==="highlighted" ? "" : "highlighted";
  36.   ishigh=row;
  37. }
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. document.onkeydown = function(e){
  46.     e = e || event;
  47.     var code = e.keyCode, rowslim = table.rows.length - 2, newhigh;
  48.     if(code === 38){ //up arraow
  49.         newhigh = rowindex(ishigh) - 2;
  50.         if(!ishigh || newhigh < 0){return GoTo('data', rowslim);}
  51.         return GoTo('data', newhigh);
  52.     } else if (code === 40){ //down arrow
  53.         newhigh = rowindex(ishigh);
  54.         if(!ishigh || newhigh > rowslim){return GoTo('data', 0);}
  55.         return GoTo('data', newhigh);
  56.     }
  57. }
  58.  
  59. function GoTo(id,nu){
  60.   var obj=document.getElementById(id),
  61.       trs=obj.getElementsByTagName('TR');
  62.   nu = nu + 1;
  63.   if (trs[nu]){
  64.     if (ishigh&&ishigh!=trs[nu]){
  65.       ishigh.className='';
  66.     }
  67.     trs[nu].className = trs[nu].className=="highlighted" ? "" : "highlighted";
  68.     ishigh=trs[nu];
  69.    }
  70. }
  71.  
  72. function rowindex(row){
  73.     var rows = table.rows, i = rows.length;
  74.     while(--i > -1){
  75.         if(rows[i] === row){return i;}
  76.     }
  77. }
  78.  
  79. }//end of nested function
  80.  
  81. function highlight_record() {
  82.     var text = document.getElementById('record').value.trim(),
  83.         td = document.evaluate('//table[@id="data"]/tbody/tr/td[1]/.[contains(., "' + text + '")]', document, null, 9, null).singleNodeValue,
  84.         allTr = document.evaluate('//table[@id="data"]/tbody/tr', document, null, 6, null), i;
  85.  
  86.     // un-highlight all TRs
  87.     for (i = 0; i < allTr.snapshotLength; i += 1) {
  88.         allTr.snapshotItem(i).className = '';
  89.     }
  90.  
  91.     // highlight specific TR if the input was valid
  92.     if (td) {
  93.         td.parentNode.className = 'highlighted';
  94.     }
  95. }
  96.  
  97.  
  98. </script>
  99.  
  100.  
  101. </head>
  102. <body onload="test()">
  103.   <table style="cursor: default;" id="data" cellspacing="1" border="1">
  104.     <thead>
  105.         <tr>
  106.             <th>Record #</th>
  107.             <th>first name</th>
  108.             <th>last name</th>
  109.             <th>age</th>
  110.             <th>total</th>
  111.             <th>discount</th>
  112.             <th>diff</th>
  113.         </tr>
  114.     </thead>
  115.     <tbody>
  116.         <tr>
  117.             <td>5</td>
  118.             <td>peter</td>
  119.             <td>parker</td>
  120.             <td>28</td>
  121.             <td>9.99</td>
  122.             <td>20.3%</td>
  123.             <td>+3</td>
  124.         </tr>
  125.         <tr>
  126.             <td>3</td>
  127.             <td>john</td>
  128.             <td>hood</td>
  129.             <td>33</td>
  130.             <td>19.99</td>
  131.             <td>25.1%</td>
  132.             <td>-7</td>
  133.         </tr>
  134.         <tr>
  135.             <td>1</td>
  136.             <td>clark</td>
  137.             <td>kent</td>
  138.             <td>18</td>
  139.             <td>15.89</td>
  140.             <td>44.2%</td>
  141.             <td>-15</td>
  142.         </tr>
  143.         <tr>
  144.             <td>4</td>
  145.             <td>bruce</td>
  146.             <td>almighty</td>
  147.             <td>45</td>
  148.             <td>153.19</td>
  149.             <td>44%</td>
  150.             <td>+19</td>
  151.         </tr>
  152.         <tr>
  153.             <td>2</td>
  154.             <td>benjamin</td>
  155.             <td>evans</td>
  156.             <td>56</td>
  157.             <td>153.19</td>
  158.             <td>23%</td>
  159.             <td>+9</td>
  160.         </tr>
  161.     </tbody>
  162. </table>
  163. <br>
  164.  
  165. Example: type the record's number in the input box and then select it:
  166. <input type="text" id="record" />
  167. <input type="button" id="record-highlight" onclick="highlight_record();" value="Highlight" />
  168.  
  169. </body>
  170.  
  171.  
  172. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement