这里有新鲜出炉的 7 天学会 Node.js,程序狗速度看过来!
Node.js 是一个基于 Chrome JavaScript 运行时建立的一个平台, 用来方便地搭建快速的 易于扩展的网络应用 · Node.js 借助事件驱动, 非阻塞 I/O 模型变得轻量和高效, 非常适合 运行在分布式设备 的 数据密集型 的实时应用
本篇文章主要介绍了 node.js 基于 mongodb 的搜索分页示例,mongodb 分页很简单,这里整理了详细的代码, 有需要的小伙伴可以参考下。
mongodb 模糊查询并分页
1. 建立数据库
代码如下:
- var mongoose = require('mongoose');
- var shortid = require('shortid');
- var Schema = mongoose.Schema;
- var IndexDataSchema = new Schema({
- _id: {
- type: String,
- unique: true,
- 'default': shortid.generate
- },
- type: String,
- city: String,
- name: string,
- value: [{
- name: String,
- value: String
- }],
- create: {
- type: Date,
- default:
- Date.now
- },
- expand: String
- });
- IndexDataSchema.statics = {
- defaultSort: {
- 'create': 1
- },
- defaultOptions: {
- 'pageSize': 0
- }
- };
- var IndexData = mongoose.model('IndexData', IndexDataSchema);
- module.exports = IndexData;
页面布局 这里只需要搜索框和搜索按钮,再点击按钮时,执行 search() 方法并发送请求
代码如下:
- <div class="searchPart">
- <input type="text" class="form-control" id="txtSearch" placeholder="请输入项目名称">
- <button class="btn btn-success search_btn" onclick="search()">
- 搜索
- </button>
- </div>
- <script>
- var paginObj;
- //设置每页显示页码
- var pageSize = 20;
- //设置当前页码为1
- var currentPage = 1;
- var condition = {
- 'city': currentCityId,
- 'name': ''
- }
- jQuery(document).ready(function() {
- refresh();
- });
- //获取查找条件
- function getCondition() {
- var name = $('#txtSearch').val();
- if (name && name.trim()) {
- // {'$regex': name, '$options': 'i'}}为模糊查询固定语法,name为参数
- condition = {
- 'city': currentCityId,
- 'name': {
- '$regex': name,
- '$options': 'i'
- }
- };
- } else {
- condition = {
- 'city': currentCityId
- }
- }
- return condition;
- }
- //刷新页面
- function refresh() {
- //查找内容
- $.get('/Manage/list/projects', {
- 'pageSize': pageSize,
- 'currentPage': currentPage,
- 'condition': getCondition()
- },
- function(result) {
- appendData(result.data);
- })
- //查找个数
- $.get('/Manage/listCount/projects', {
- 'condition': condition
- },
- function(result) {
- paginObj = new DataPagin(document.querySelector('.projects-list'), result.count, {
- 'pageSize': pageSize,
- 'changePageFun': rquestPageData
- });
- })
- }
- //重新分页
- function rquestPageData(currentPage, callback) {
- $.get('/Manage/list/projects', {
- 'pageSize': pageSize,
- 'currentPage': currentPage,
- 'condition': getCondition()
- },
- function(result) {
- appendData(result.data);
- }) if (callback) {
- callback();
- }
- }
- //改变页码,显示相应的内容
- function changePage(paginObj, index) {
- paginObj.setPageNumber(index);
- }
- function appendData(data) {
- //debugger;
- var list = $('.projects-list').children('tbody');
- list.html('');
- // 页面显示模板
- for (var i = 0; i < data.length; i++) {
- .........
- //此部分自己定义
- })
- }
- //点击搜索按钮执行该方法
- function search() {
- currentPage = 1;
- refresh();
- }
- </script>
到数据库查找并返回相应内容
- var formidable = require("formidable");
- var common = require('./common');
- var path = require("path");
- var fs = require('fs');
- var path = require('path');
- var guid = require('guid');
- var shortid = require('shortid');
- var AuctionHouse = require('./db/IndexData');
- var funs = {
- getList: function (collectionName, req, res, next) {
- var mainObj = transformCollctionName(collectionName);
- if (!mainObj) {
- next();
- }
- var options = req.query;
- var sort = options.sort || mainObj.defaultSort;
- var pageSize = options.pageSize || mainObj.defaultOptions.pageSize;
- var currentPage = options.currentPage || 1;
- var condition = options.condition || {}
- //此部分为查找条件
- mainObj.find(condition).sort(sort).skip((currentPage - 1) * pageSize).limit(pageSize).exec(function (err, docs) {
- if (err) {
- next(err);
- }
- return res.json(common.returnData(true, docs));
- })
- },
- countList: function (collectionName, req, res, next) {
- var mainObj = transformCollctionName(collectionName);
- if (!mainObj) {
- next();
- }
- var condition = req.query.condition || {}
- mainObj.find(condition).count().exec(function (err, docs) {
- if (err) {
- next(err);
- }
- return res.json({'count': docs});
- })
- },
- HandleEvent: function (collectionName, actionsName, req, res, next) {
- var mainFuns = getCollctionFuns(collectionName)
- if (!mainFuns) {
- next();
- }
- var fun = mainFuns[actionsName];
- if (!fun) {
- next();
- }
- fun(req, res, next);
- }
来源: http://www.phperz.com/article/17/0507/328213.html