wxgrid.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*!
  2. * =====================================================
  3. * wxgrid v1.1.0
  4. * =====================================================
  5. */
  6. var WXGrid = function () {
  7. this._rowsCount = 0;
  8. this.rows = [];
  9. this._colsCount = 0;
  10. this.cols = [];
  11. // _maxCols:4, //默认最多4列
  12. this._widths = 100; //默认总宽度的大小
  13. }
  14. var Data = function (wxgrid) {
  15. this.add = function (key, arr) {
  16. if (key == "add") {
  17. return false;
  18. }
  19. var arr2 = new Array();
  20. for (var i = 0; i < wxgrid._rowsCount; i++) {
  21. arr2[i] = new Array;
  22. for (var j = 0; j < wxgrid._colsCount; j++) {
  23. var index = i + j + i * (wxgrid._colsCount - 1);
  24. arr2[i][j] = arr[index];
  25. }
  26. }
  27. this[key] = arr2;
  28. return true;
  29. }
  30. }
  31. module.exports = function () {
  32. var _wxgrid = null;
  33. this.rows = null;
  34. this.cols = null;
  35. this.data = null;
  36. //初始化表格,设置几行几列
  37. this.init = function (rowsCount, colsCount) {
  38. _wxgrid = new WXGrid;
  39. _wxgrid._rowsCount = rowsCount;
  40. _wxgrid._colsCount = colsCount;
  41. //设置行信息
  42. for (var i = 0; i < rowsCount; i++) {
  43. _wxgrid.rows.push({
  44. index: i,
  45. height: 0 //默认高0
  46. })
  47. }
  48. //设置列信息
  49. var width = 100 / colsCount / _wxgrid._widths * 100;
  50. for (var i = 0; i < colsCount; i++) {
  51. _wxgrid.cols.push({
  52. index: i,
  53. width: width //默认等宽
  54. })
  55. }
  56. //返回行列信息
  57. this.rows = _wxgrid.rows;
  58. this.cols = _wxgrid.cols;
  59. this.data = new Data(_wxgrid);
  60. }
  61. //设置行高(不设置的话,默认40高度)
  62. //传人height,index设置第index+1行的高度
  63. //只传人height设置所有行的高度
  64. this.setRowsHeight = function (height, index) {
  65. if (index) {
  66. index = parseInt(index);
  67. if (index > 0 && index < _wxgrid.rows.length) {
  68. _wxgrid.rows[index].height = height;
  69. }
  70. }
  71. else {
  72. for (var i = 0; i < _wxgrid.rows.length; i++) {
  73. _wxgrid.rows[i].height = height;
  74. }
  75. }
  76. }
  77. //设置列宽(不设置的话,默认等宽)
  78. //传人width,index设置第index+1列的宽度“权重”
  79. //只传人height设置所有的的宽度“权重”
  80. this.setColsWidth = function (width, index) {
  81. var cols = _wxgrid.cols;
  82. if (index) {
  83. index = parseInt(index);
  84. if (index > 0 && index < cols.length) {
  85. cols[index].width = width;
  86. }
  87. }
  88. else {
  89. for (var i = 0; i < cols.length; i++) {
  90. cols[i].width = width;
  91. }
  92. }
  93. //重新计算宽占比例
  94. _wxgrid._widths = 0;
  95. for (var i = 0; i < _wxgrid.cols.length; i++) {
  96. _wxgrid._widths = _wxgrid._widths + cols[i].width;
  97. }
  98. console.log(_wxgrid._widths)
  99. for (var i = 0; i < _wxgrid.cols.length; i++) {
  100. _wxgrid.cols[i].width = cols[i].width / _wxgrid._widths * 100;
  101. console.log(i + "" + _wxgrid.cols[i].width)
  102. }
  103. }
  104. }