home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / internet / develop / temp / js / zotrtable.txt
Encoding:
Text File  |  2001-07-13  |  3.2 KB  |  122 lines

  1. function ZotrTable(id,rows,cols,border,width,align) {
  2.  this.id = id
  3.  this.rows = rows
  4.  this.cols = cols
  5.  this.border = border
  6.  this.width = width
  7.  this.align = align
  8.  this.buttons = new Array(cols)
  9.  this.numeric = new Array(cols)
  10.  for(var i=0;i<cols;++i) 
  11.   this.buttons[i]=" "
  12.  for(var i=0;i<cols;++i) 
  13.   this.numeric[i]=false
  14.  this.data = new Array(rows)
  15.  for(var i=0;i<rows;++i) {
  16.   this.data[i] = new Array(cols)
  17.   for(var j=0;j<cols;++j)
  18.    this.data[i][j] = " "
  19.  }
  20.  this.display = ZotrTable_display
  21.  this.setButtons = ZotrTable_setButtons
  22.  this.setData = ZotrTable_setData
  23.  this.setNumeric = ZotrTable_setNumeric
  24.  this.sort = ZotrTable_sort
  25. }
  26. function ZotrTable_setData(data) {
  27.  if(data == null) return
  28.  if(data.length > 0) {
  29.   for(var i=0;i<this.rows;++i) {
  30.    if(i>=data.length) break
  31.    if(data[i] != null) {
  32.     var n = data[i].length
  33.     if(n > this.cols) n = this.cols
  34.     for(var j=0;j<n;++j)
  35.      if(data[i][j]!=null) this.data[i][j] = data[i][j]
  36.    }
  37.   }
  38.  }
  39. }
  40. function ZotrTable_setButtons(buttons) {
  41.  if(buttons == null) return
  42.  var n = buttons.length
  43.  if(n > this.cols) n = this.cols
  44.  for(var i=0;i<n;++i)
  45.   if(buttons[i]!=null) this.buttons[i] = buttons[i]
  46. }
  47. function ZotrTable_display() {
  48.  //cookie
  49.  if(document.cookie.length > 0) {
  50.   var search = this.id + "="
  51.   var offset = document.cookie.indexOf(this.id+"=") 
  52.   if(offset != -1) { 
  53.    offset += search.length 
  54.    var end = document.cookie.indexOf(";", offset) 
  55.    if(end == -1) end = document.cookie.length
  56.    this.sort(document.cookie.substring(offset, end))
  57.   }   
  58.  }
  59.  document.writeln('<FORM NAME="tableForm">')
  60.  document.write('<TABLE BORDER="'+this.border+'" WIDTH="')
  61.  document.writeln(this.width+'" ALIGN="'+this.align+'">')
  62.  document.writeln('<TR>')
  63.  // Zobraz buttons
  64.  for(var i=0;i<this.cols;++i) {
  65.   document.write('<TD>')
  66.   document.write('<INPUT TYPE="BUTTON" VALUE="')
  67.   document.write(this.buttons[i])
  68.   document.write('" ONCLICK="ZotrTable_handleColumnButton(\'')
  69.   document.write(this.id+'\','+i+')">')
  70.   document.writeln('</TD>')
  71.  }
  72.  document.writeln('</TR>')
  73.  // Zobraz zotriedene/nezotriedene udaje v tabulke
  74.  for(var i=0;i<this.rows;++i) {
  75.   document.writeln('<TR>')
  76.   for(var j=0;j<this.cols;++j) {
  77.    document.write('<TD>')
  78.     document.write(this.data[i][j])
  79.     document.writeln('</TD>')
  80.   }
  81.   document.writeln('</TR>')
  82.  }
  83.  document.writeln('</TABLE>')
  84.  document.writeln('</FORM>')
  85. }
  86. function ZotrTable_setNumeric(n) {
  87.  this.numeric[n] = true
  88. }
  89. function ZotrTable_sort(n) {
  90.  // Sort rows
  91.  var changes=true
  92.  for(;changes;) {
  93.   changes = false
  94.   for(var i=0;i<this.rows-1;++i) {
  95.    if(this.numeric[n]) {
  96.     var v1 = parseFloat(this.data[i][n])
  97.     if(isNaN(v1)) v1 = 0;
  98.     var v2 = parseFloat(this.data[i+1][n])
  99.     if(isNaN(v2)) v2 = 0;
  100.     if(v1 > v2) {
  101.      changes = true
  102.      var temp = this.data[i]
  103.      this.data[i] = this.data[i+1]
  104.      this.data[i+1] = temp
  105.     }   
  106.    }else{
  107.     if(this.data[i][n] > this.data[i+1][n]) {
  108.      changes = true
  109.      var temp = this.data[i]
  110.      this.data[i] = this.data[i+1]
  111.      this.data[i+1] = temp
  112.     }
  113.    }
  114.   }
  115.  } 
  116. }
  117. function ZotrTable_handleColumnButton(id,n) {
  118.  //cookie a reload
  119.  document.cookie = id + "="+n
  120.  window.location.reload()
  121. }
  122.