简体   繁体   English

具有冻结(可滚动)的垂直和水平行/列的HTML表

[英]HTML Table with frozen (scrollable) vertical and horizontal rows/columns

I am trying to create an HTML table from json. 我正在尝试从json创建HTML表。 I am able to generate the exact format that I need based on a sample codepen that I found. 我能够根据发现的示例代码笔生成所需的确切格式。 The javascript that fires and seems to create an object that is called which modifies the css for the containing div. 触发并似乎创建一个对象的JavaScript,该对象将修改包含div的css。 The HTML (generated from JSON) when used as static data works 100%. 当HTML(从JSON生成)用作静态数据时,可以100%使用。

Working example - http://www.cocopine.co.za/code/index.html 工作示例-http: //www.cocopine.co.za/code/index.html

My problem is when I generate the HTML from Javascript reading the JSON and building the table, the javascript seems to fire too quickly and do nothing to the scrolling of the table div. 我的问题是,当我从读取JSON并构建表的Javascript生成HTML时,JavaScript似乎触发得太快,并且对表div的滚动没有任何作用。 I am very new to Javascript and the code in the JS file needs modification and is a bit beyond my level at the moment. 我是Java的新手,因此需要修改JS文件中的代码,并且此刻超出了我的水平。

Error Javascript generated page - http://www.cocopine.co.za/code/grid1.html 错误的Javascript生成页面-http: //www.cocopine.co.za/code/grid1.html

In summary : grid1.html should behave the same way as index.html when scrolling (vertical and horizontal) 总结:grid1.html在滚动(垂直和水平)时的行为应与index.html相同。

The Javascript that is not working on the generated HTML is the following: 在生成的HTML上不起作用的Javascript如下:

(function() {
  var demo, fixedTable;
  fixedTable = function(el) {
    var $body, $header, $sidebar;
    $body = $(el).find('.fixedTable-body');
    $sidebar = $(el).find('.fixedTable-sidebar table');
    $header = $(el).find('.fixedTable-header table');
    return $($body).scroll(function() {
      $($sidebar).css('margin-top', -$($body).scrollTop());
      return $($header).css('margin-left', -$($body).scrollLeft());
    });
  };

  demo = new fixedTable($('#demo'));

}).call(this);

I have managed to get this working by running the code within the jquery callback function for the json file process. 我设法通过在json文件过程的jquery回调函数中运行代码来使此工作正常。

I am not too familiar with the asynchronous events of jquery but obviously the json call was happening and then moving on to the next bit of code with the processing of the JSON and HTML happening in the background and only completing after the js code has already finished. 我对jquery的异步事件不太熟悉,但是很明显,正在进行json调用,然后继续处理下一个代码,并在后台处理JSON和HTML,并且仅在js代码完成后才完成。

In short. 简而言之。 The js code was added in after the adding together of all the tables and divs. 在将所有表和div加在一起之后,添加了js代码。

...
sideTable.appendChild(tbody);
                aside.appendChild(sideTable);
                div.appendChild(aside);
                divBody.appendChild(tableBody)
                div.appendChild(divBody);
                element.appendChild(div);                   

                var demo, fixedTable;
                  fixedTable = function(el) {
                    var $body, $header, $sidebar;
                    $body = $(el).find('.fixedTable-body');
                    $sidebar = $(el).find('.fixedTable-sidebar table');
                    $header = $(el).find('.fixedTable-header table');
                    return $($body).scroll(function() {
                      $($sidebar).css('margin-top', -$($body).scrollTop());
                      return $($header).css('margin-left', -$($body).scrollLeft());
                    });
                  };

                  demo = new fixedTable($('#demo'));

                }

             });

}    

...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM