首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在NodeJS中使用带有ExpressJS的制表器

在NodeJS中使用带有ExpressJS的制表器
EN

Stack Overflow用户
提问于 2020-09-25 00:06:55
回答 3查看 460关注 0票数 0

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

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

代码语言:javascript
复制
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:

代码语言:javascript
复制
div.example-table

但是,我得到了以下错误:

代码语言:javascript
复制
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)

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

EN

回答 3

Stack Overflow用户

发布于 2020-09-28 17:41:39

我解决了我的问题。

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

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
link(rel="stylesheet" href="tabulator.min.css")
script(type='text/javascript' src="tabulator.min.js")

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

代码语言:javascript
复制
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);

非常感谢您的帮助!

票数 1
EN

Stack Overflow用户

发布于 2020-09-25 05:02:24

这看起来像是您试图在控制器上的响应中使用制表器。

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

您还实例化了具有小写"T“的制表器,它应该是new Tabulator

我建议你需要做的是创建一个超文本标记语言页面来显示表格,例如http://tabulator.info/basic/4.8上的源码就可以了,虽然你也需要提供源JS和CSS文件才能让演示程序工作,或者你也可以改变CDN Links的本地文件路径来快速演示。

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

代码语言:javascript
复制
// viewed at http://localhost:8080
app.get('/', function(req, res) {
    res.sendFile(path.join(__dirname + '/index.html'));
});
票数 0
EN

Stack Overflow用户

发布于 2021-09-27 17:07:55

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

代码语言:javascript
复制
// 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)});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64050248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档