如何从mongodb加载数据

时间:2018-01-15 15:05:08

标签: mongoose jsgrid

我想在我的Express应用程序中创建一个CRUD表单,供用户执行这些操作。我一直在关注在线文档,但我似乎无法正确地从数据库中获取数据。在线的所有示例似乎都有硬编码的数据示例。我的问题是,如何使用jsgrid从数据库中获取数据?

search.js

$(function() {

  $("#jsGrid").jsGrid({
      height: "70%",
      width: "100%",
      filtering: true,
      inserting: true,
      editing: true,
      sorting: true,
      paging: true,
      autoload: true,
      pageSize: 10,
      pageButtonCount: 5,
      deleteConfirm: "Do you really want to delete client?",
      controller: {
          loadData: function(filter) {
              return $.ajax({
                  type: "GET",
                  url: "/projects",
                  data: filter
                }).then(function(result) {
                    return result.data;
                });
            },
          updateItem: function(item) {
              return $.ajax({
                  type: "PUT",
                  url: "/projects",
                  data: item
              });
          },
          deleteItem: function(item) {
              return $.ajax({
                  type: "DELETE",
                  url: "/projects",
                  data: item
              });
          }
      },
      fields: [
          { name: "title", type: "text", width: 150 },
          { name: "location", type: "text", width: 50 },
          { name: "resources", type: "text", width: 200 },
          { type: "control" }
      ]
  }); 
});

Pug文件:

extends layout

block head
  link(href='https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.1/jsgrid.min.css', rel='stylesheet')
  link(href='https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.1/jsgrid-theme.min.css', rel='stylesheet')

block content
  .main.container
    .row
      .col-md-100.col-md-offset-40
        h1.display-4
        div#jsGrid

block scripts
  script(src='https://cdnjs.cloudflare.com/ajax/libs/jsgrid/1.5.1/jsgrid.min.js')
  script(src='/javascripts/search.js')

index.js(控制器)

// GET all projects
router.get('/projects',mid.requiresLogin, function (req, res, err) {
  Project.find(function (err, docs){ 
      res.render('projects', {title: 'Current Projects - Client', projects: docs});
  });  
});

0 个答案:

没有答案