【问题标题】:Use Tabulator with ExpressJS in NodeJS在 NodeJS 中使用 Tabulator 和 ExpressJS
【发布时间】:2020-09-24 16:06:55
【问题描述】:

我正在尝试安装 Tabulator (http://tabulator.info/docs/4.0/quickstart) 的默认示例。 我在我的 NodeJS 项目中安装了 Tabulator。

我的 index.js 文件中的路由定义如下所示:

app.get("/", (req, res) => {
    var table = new tabulator("#example-table", {
        height:205,
        layout:"fitColumns", //fit columns to width of table (optional)
        columns:[ 
            {title:"Name", field:"name", width:150},
            {title:"Age", field:"age", align:"left", formatter:"progress"},
            {title:"Favourite Color", field:"col"},
            {title:"Date Of Birth", field:"dob", sorter:"date", align:"center"},
        ],
        rowClick:function(e, row){
            alert("Row " + row.getData().id + " Clicked!!!!");
        },
      });
  
      var tabledata = [
      {id:1, name:"Oli Bob", age:"12", col:"red", dob:""},
      {id:2, name:"Mary May", age:"1", col:"blue", dob:"14/05/1982"},
      {id:3, name:"Christine Lobowski", age:"42", col:"green", dob:"22/05/1982"},
      {id:4, name:"Brendon Philips", age:"125", col:"orange", dob:"01/08/1980"},
      {id:5, name:"Margret Marmajuke", age:"16", col:"yellow", dob:"31/01/1999"},
      ];
    res.render("index", { title: "Home"});
});

然后我在 index.pug 中添加了一个 div:

div.example-table

但是,我收到以下错误:

ReferenceError: document is not defined
    at Tabulator.initializeElement (C:\Users\SESA509216\Box\Perso\Scripts\LMS\Audit-Trail\node_modules\tabulator-tables\dist\js\tabulator.js:9223:19)
    at new Tabulator (C:\Users\SESA509216\Box\Perso\Scripts\LMS\Audit-Trail\node_modules\tabulator-tables\dist\js\tabulator.js:8612:12)
    at app.get (C:\Users\SESA509216\Box\Perso\Scripts\LMS\Audit-Trail\index.js:37:17)
    at Layer.handle [as handle_request] (C:\Users\SESA509216\Box\Perso\Scripts\LMS\Audit-Trail\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\SESA509216\Box\Perso\Scripts\LMS\Audit-Trail\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\SESA509216\Box\Perso\Scripts\LMS\Audit-Trail\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\SESA509216\Box\Perso\Scripts\LMS\Audit-Trail\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\SESA509216\Box\Perso\Scripts\LMS\Audit-Trail\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\SESA509216\Box\Perso\Scripts\LMS\Audit-Trail\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\SESA509216\Box\Perso\Scripts\LMS\Audit-Trail\node_modules\express\lib\router\index.js:275:10)

我猜,Tabulator 应该以某种方式在客户端初始化。但我不知道怎么...

【问题讨论】:

  • 您是否尝试将 Tabulator javascript 包含在节点中,而不是将其包含在页面上的 script 标记中?
  • 如何在页面中包含它?如果我尝试这样做: var Tabulator = require('tabulator-tables');我收到错误消息“require is not a function”...

标签: node.js express pug tabulator


【解决方案1】:

我解决了我的问题。

我将 tabulator-tables css & js 路径加入到 index.js 中的应用配置中:

app.use(express.static(path.join(__dirname, "node_modules/tabulator-tables/dist/css")));
app.use(express.static(path.join(__dirname, "node_modules/tabulator-tables/dist/js")));

然后,我在前端导入了制表符 css 和 js 文件:

link(rel="stylesheet" href="tabulator.min.css")
script(type='text/javascript' src="tabulator.min.js")

最后,我将示例脚本复制到我的页面中:

div.example-table(id="example-table")
    script.  
      //create Tabulator on DOM element with id "example-table"
      var table = new Tabulator("#example-table", {
        height:205, // set height of table (in CSS or here), this enables the Virtual DOM and improves render speed dramatically (can be any valid css height value)
        layout:"fitColumns", //fit columns to width of table (optional)
        columns:[ //Define Table Columns
          {title:"Name", field:"name", width:150},
          {title:"Age", field:"age", align:"left", formatter:"progress"},
          {title:"Favourite Color", field:"col"},
          {title:"Date Of Birth", field:"dob", sorter:"date", align:"center"},
        ],
        rowClick:function(e, row){ //trigger an alert message when the row is clicked
          alert("Row " + row.getData().id + " Clicked!!!!");
        },
      });
      
      //define some sample data
      var tabledata = [
        {id:1, name:"Oli Bob", age:"12", col:"red", dob:""},
        {id:2, name:"Mary May", age:"1", col:"blue", dob:"14/05/1982"},
        {id:3, name:"Christine Lobowski", age:"42", col:"green", dob:"22/05/1982"},
        {id:4, name:"Brendon Philips", age:"125", col:"orange", dob:"01/08/1980"},
        {id:5, name:"Margret Marmajuke", age:"16", col:"yellow", dob:"31/01/1999"},
      ];
      
      //load sample data into the table
      table.setData(tabledata);

非常感谢您的帮助!

【讨论】:

    【解决方案2】:

    看起来您正在尝试在控制器的响应中使用 Tabulator。

    Tabulator 设计为在客户端运行,而不是在服务器端运行。您需要返回一些带有脚本标签的 HTML,然后在 DOM 节点上构建表格。

    您还使用小写“T”实例化制表符,它应该是new Tabulator

    我建议您需要做的是创建一个 HTML 页面来显示表格,例如 http://tabulator.info/basic/4.8 的源代码就可以了,尽管您还需要提供源 JS 和 CSS 文件才能使演示工作,或者,您可以将本地文件路径更改为 CDN Links 以进行快速演示。

    然后您需要使用 ExpressJS 提供此页面,使用 sendFile 函数和 html 文件的路径

    // viewed at http://localhost:8080
    app.get('/', function(req, res) {
        res.sendFile(path.join(__dirname + '/index.html'));
    });
    

    【讨论】:

    • 感谢您的回答。如果 Tabulator 不能直接在 NodeJS 中使用,那为什么可以安装和导入呢?
    • @FrédéricUhrweiller NPM 也用于客户端 JavaScript 库。通常的做法是在服务器端导入它们,以便它们可以与任何其他客户端库和自定义 javascript 捆绑到一个文件中,然后再发送到客户端。
    • 除了前面的答案是主要原因之外,Tabulator 还可以用于具有用户界面的节点应用程序,例如电子框架
    【解决方案3】:

    要将表数据从节点传递到 pug 文件中的脚本,请执行以下操作:

    // node index.js
    let data = [
        {id : 1, name : 'Adam', zip : '11101'},
        {id : 2, name : 'Bob', zip : '95134'}
    ];
    res.render('table.pug', { table_data: data });
    
    // table.pug
    div.example-table(id="example-table")
          script.
            var table = new Tabulator("#example-table", {
              columns:[
                {title:"Name", field:"name"},
                {title:"ZIP", field:"zip"}
              ]
            });
    
            //load sample data into the table
            table.setData(!{JSON.stringify(table_data)});
    

    【讨论】:

      猜你喜欢
      • 2013-12-13
      • 2012-09-09
      • 2014-05-06
      • 1970-01-01
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      • 2013-12-24
      • 1970-01-01
      相关资源
      最近更新 更多