当前位置: 首页 > 工具软件 > ngTable > 使用案例 >

MEANjs Angularjs NgTable Mongo

嵇浩然
2023-12-01

最近在使用angularjs的ngTable指令,发现没有比较详细的后端示例,以下是我写的,与大家分享,有问题请指出!
在模块前端控制器中添加如下:

angular.module('audits').controller('AuditsController', ['$scope', 'Audits', 'NgTableParams',
function($scope, Audits, NgTableParams) {
var p = {
    page: 1, // default page 
    count: 20 // the default pageSize
 };
$scope.tableParams = new NgTableParams(p, {
     total:0, 
     counts: [], // can't selected pageSize
     // load data
     getData: function(params) {
         return Audits.get(params.url()).$promise.then(function (data) {
            params.total(data.total);
            return data.results;
    });
            }
        });
  }]);

// ngTable reload function 
$scope.tableParams.reload();

$resource 服务

'use strict';

//audits service used to communicate audits REST endpoints
angular.module('audits').factory('Audits', ['$resource',
    function($resource) {
        return $resource('api/audits/:auditId', { auditId: '@_id'
        }, {
            update: {
            method: 'PUT'
            }
        });
    }
]);

模块后端控制器添加如下代码:

'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
    path = require('path'),
    errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller')),
    audit = mongoose.model('audit'),
    _ = require('lodash');

exports.list = function(req, res) {
    var count = req.query.count || 20;
    var page = req.query.page || 1;
    var start = (page-1)*count;
    var sortData = req.query.sorting ||'time_stamp';     
    var data = {};
    var reqData = req.query,
    p = {};

audit.find(p).sort(sortData).skip(start).limit(count).populate('user', 'displayName').exec(function(err, audits) {
    if (err) {
        return res.status(400).send({
        message: errorHandler.getErrorMessage(err)
        });
    } else {
            data.results = audits;
            audit.count(p, function (err, num) {
                if (err) {
                return res.status(400).send({
                message: errorHandler.getErrorMessage(err)
                });
                } else {
                    data.total = num;
                    res.jsonp(data);
                }
            })
    }
    });
};

自我感觉以上部分,尤其是后端,还有不足之处,请大神指教!

 类似资料:

相关阅读

相关文章

相关问答