最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
node.js基于mongodb的搜索分页示例
时间:2017-03-01 编辑:简简单单 来源:一聚教程网
mongodb模糊查询并分页
1.建立数据库
代码如下:
代码如下 | 复制代码 |
varmongoose = require('mongoose'); varshortid = require('shortid'); varSchema = mongoose.Schema;
varIndexDataSchema =newSchema({ _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}
};
varIndexData = mongoose.model('IndexData', IndexDataSchema);
module.exports = IndexData; |
页面布局 这里只需要搜索框和搜索按钮,再点击按钮时,执行search()方法并发送请求
代码如下:
代码如下 | 复制代码 |
varpaginObj;
//设置每页显示页码 varpageSize = 20;
//设置当前页码为1 varcurrentPage = 1;
varcondition = {'city': currentCityId,'name':''} jQuery(document).ready(function() { refresh(); });
//获取查找条件 functiongetCondition() { varname = $('#txtSearch').val(); if(name && name.trim()) { // {'$regex': name, '$options': 'i'}}为模糊查询固定语法,name为参数 condition = {'city': currentCityId,'name': {'$regex': name,'$options':'i'}}; } else{ condition = {'city': currentCityId} } returncondition; }
//刷新页面 functionrefresh() {
//查找内容 $.get('/Manage/list/projects', { 'pageSize': pageSize, 'currentPage': currentPage, 'condition': getCondition() },function(result) { appendData(result.data); })
//查找个数 $.get('/Manage/listCount/projects', {'condition': condition},function(result) { paginObj =newDataPagin(document.querySelector('.projects-list'), result.count, { 'pageSize': pageSize, 'changePageFun': rquestPageData }); }) }
//重新分页 functionrquestPageData(currentPage, callback) { $.get('/Manage/list/projects', { 'pageSize': pageSize, 'currentPage': currentPage, 'condition': getCondition() },function(result) { appendData(result.data); }) if(callback) { callback(); } }
//改变页码,显示相应的内容 functionchangePage(paginObj, index) { paginObj.setPageNumber(index); }
functionappendData(data) { //debugger; varlist = $('.projects-list').children('tbody'); list.html(''); // 页面显示模板 for(vari = 0; i < data.length; i++) {
......... //此部分自己定义
}) }
//点击搜索按钮执行该方法 functionsearch() { currentPage = 1; refresh(); }
|
2.到数据库查找并返回相应内容
代码如下 | 复制代码 |
varformidable = require("formidable"); varcommon = require('./common'); varpath = require("path"); varfs = require('fs'); varpath = require('path'); varguid = require('guid'); varshortid = require('shortid');
varAuctionHouse = require('./db/IndexData');
varfuns = { getList:function(collectionName, req, res, next) { varmainObj = transformCollctionName(collectionName); if(!mainObj) { next(); } varoptions = req.query; varsort = options.sort || mainObj.defaultSort; varpageSize = options.pageSize || mainObj.defaultOptions.pageSize; varcurrentPage = options.currentPage || 1; varcondition = options.condition || {}
//此部分为查找条件 mainObj.find(condition).sort(sort).skip((currentPage - 1) * pageSize).limit(pageSize).exec(function(err, docs) { if(err) { next(err); } returnres.json(common.returnData(true, docs)); }) }, countList:function(collectionName, req, res, next) { varmainObj = transformCollctionName(collectionName); if(!mainObj) { next(); } varcondition = req.query.condition || {} mainObj.find(condition).count().exec(function(err, docs) { if(err) { next(err); } returnres.json({'count': docs}); }) }, HandleEvent:function(collectionName, actionsName, req, res, next) { varmainFuns = getCollctionFuns(collectionName) if(!mainFuns) { next(); } varfun = mainFuns[actionsName]; if(!fun) { next(); } fun(req, res, next); } |
-
上一个: 利用JS实现文字的聚合动画效果
相关文章
- extjs4中Ext.toolbar.Paging分页工具栏控件 12-23
- js特效支持键盘和鼠标滚轮翻页的分页效果 09-08
- jsp分页 07-10
- 目前最好的JSP分页技术 01-12
- HTML简单购物数量小程序代码展示 10-31
- html canvas实现弹幕功能 10-31