简体   繁体   English

sorttable.js 在服务器上工作,但在内联时不工作

[英]sorttable.js works from server, but not when inlined

I'm creating a tool that generates reports, I'd like to "html-ify" them The reports contain many tables, and this JAVA SCRIPT package would be great我正在创建一个生成报告的工具,我想对它们进行“html 化”报告包含许多表格,这个 JAVA SCRIPT 包会很棒

https://kryogenix.org/code/browser/sorttable/ https://kryogenix.org/code/browser/sorttable/

It works - if and only if - I pull all files from a web server.它有效 - 当且仅当 - 我从网络服务器中提取所有文件。 ie: My code contains:即:我的代码包含:

It does not work if I inline the module and load it from the server.如果我内联模块并从服务器加载它,它就不起作用。

REQUIREMENT: I need these to be standalone files, my "user/victim" needs to click on the filename and view the report file and NOT start a local server, etc.要求:我需要这些是独立文件,我的“用户/受害者”需要单击文件名并查看报告文件,而不是启动本地服务器等。

An background - example: In the embedded software engineering you often have a link map file when you compile your file, I want to make a better HTML version (in addition to the plain text).一个背景——例子:在嵌入式软件工程中你经常在编译你的文件时有一个链接映射文件,我想制作一个更好的HTML版本(除了纯文本)。 It is these types of files the user needs to click on, and review.用户需要点击和查看这些类型的文件。 Long term is some interactive graphs and charts, but (baby steps) initially tables that can be sorted would be fantastic.长期是一些交互式图形和图表,但是(婴儿步骤)最初可以排序的表格会很棒。

PROBLEM 1 What I tried问题 1我尝试了什么

If I inline the file "sorttable.js" like this:如果我像这样内联文件“sorttable.js”:

   <script>  ... body of sortable.js here ... </script>

I get garbage in my browser (and others I've tried), I have seen I must put this in the HEAD, and other places says: body, nothing I do seems to work.我在我的浏览器(以及我尝试过的其他人)中看到垃圾,我看到我必须把它放在 HEAD 中,其他地方说:身体,我所做的一切似乎都不起作用。

PROBLEM 2 Other approach I tried问题 2我尝试过的其他方法

If I try to load the image from a file "next to" the HTML files, in the same way the HTML file might load a ".png" or ".jpg" - those work -but the script does not, meaning:如果我尝试从 HTML 文件“旁边”的文件中加载图像,HTML 文件可能会以相同的方式加载“.png”或“.jpg” - 这些工作 - 但脚本没有,意思是:

   <script src="sorttable.js"></script>

All browsers produce the same garbage.所有浏览器都会产生相同的垃圾。

According to the library page , tables must have the class sortable in order for the library to find those tables and apply the sorting functions.根据库页面,表必须具有sortable类,以便库找到这些表并应用排序功能。 You have to inject code that:您必须注入以下代码:

  1. Inject the sortable script注入可排序脚本
  2. Wait until sortable script is loaded等待可排序脚本加载完毕
  3. Add sortable class to all tablessortable类添加到所有表
  4. Reset sortable.init.done flag and re-init the script重置sortable.init.done标志并重新初始化脚本
(function() {
  // Add sorttable.js to the page
  var po = document.createElement('script');
  po.type = 'text/javascript';
  po.async = true;
  po.src = 'https://kryogenix.org/code/browser/sorttable/sorttable.js';
  var s = document.getElementsByTagName('script')[0];
  s.parentNode.insertBefore(po, s);

  // set a timer to check and see when sorttable.js is loaded
  let timer = setInterval(() => {
    if (window.sorttable) {
      // Clear the timer
      clearInterval(timer);

      // Get all tables in the page
      let tables = document.querySelectorAll('table');

      // Add sortable class to each table
      tables.forEach((table) => table.classList.add('sortable'));

      // Reset sorttable done flag
      window.sorttable.init.done = false;

      // Re-run sorttable init code
      window.sorttable.init();
    }
  }, 200);
})();

You can check this code is working:您可以检查此代码是否有效:

  1. Open a page with some tables inside, for example HTML Tables打开一个包含一些表格的页面,例如HTML 表格
  2. Open console ( F12 on Widnows )打开控制台( Widnows 上的 F12
  3. Paste the above code and hit enter粘贴上面的代码并按回车

You should see the table is now sortable.您应该会看到该表现在是可排序的。

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

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