国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

node.js基于mongodb的搜索分頁(yè)示例

2019-11-19 17:48:47
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

mongodb模糊查詢并分頁(yè)

1.建立數(shù)據(jù)庫(kù)

代碼如下:

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;

頁(yè)面布局 這里只需要搜索框和搜索按鈕,再點(diǎn)擊按鈕時(shí),執(zhí)行search()方法并發(fā)送請(qǐng)求

代碼如下:

 <div class="searchPart">          <input type="text" class="form-control" id="txtSearch" placeholder="請(qǐng)輸入項(xiàng)目名稱">          <button class="btn btn-success search_btn" onclick="search()">搜索</button>        </div>        <script>  var paginObj;    //設(shè)置每頁(yè)顯示頁(yè)碼  var pageSize = 20;      //設(shè)置當(dāng)前頁(yè)碼為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'}}為模糊查詢固定語(yǔ)法,name為參數(shù)      condition = {'city': currentCityId, 'name': {'$regex': name, '$options': 'i'}};    }    else {      condition = {'city': currentCityId}    }    return condition;  }     //刷新頁(yè)面  function refresh() {    //查找內(nèi)容    $.get('/Manage/list/projects', {      'pageSize': pageSize,      'currentPage': currentPage,      'condition': getCondition()    }, function (result) {      appendData(result.data);    })//查找個(gè)數(shù)    $.get('/Manage/listCount/projects', {'condition': condition}, function (result) {      paginObj = new DataPagin(document.querySelector('.projects-list'), result.count, {        'pageSize': pageSize,        'changePageFun': rquestPageData      });    })  }//重新分頁(yè)  function rquestPageData(currentPage, callback) {    $.get('/Manage/list/projects', {      'pageSize': pageSize,      'currentPage': currentPage,      'condition': getCondition()    }, function (result) {      appendData(result.data);    })    if (callback) {      callback();    }  }//改變頁(yè)碼,顯示相應(yīng)的內(nèi)容  function changePage(paginObj, index) {    paginObj.setPageNumber(index);  }  function appendData(data) {    //debugger;    var list = $('.projects-list').children('tbody');    list.html('');    // 頁(yè)面顯示模板    for (var i = 0; i < data.length; i++) {          .........      //此部分自己定義             })  }    //點(diǎn)擊搜索按鈕執(zhí)行該方法  function search() {    currentPage = 1;    refresh();  }</script>

到數(shù)據(jù)庫(kù)查找并返回相應(yīng)內(nèi)容

 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);  }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 惠州市| 临夏市| 司法| 宽城| 内江市| 闸北区| 盘山县| 沙洋县| 天水市| 海南省| 崇义县| 宁夏| 磐安县| 张家港市| 观塘区| 天峻县| 武定县| 吴旗县| 佛山市| 诏安县| 土默特右旗| 竹溪县| 武宁县| 罗城| 平利县| 邵阳市| 蒲城县| 永定县| 江西省| 浠水县| 周口市| 合山市| 筠连县| 商丘市| 古浪县| 浑源县| 惠州市| 奈曼旗| 宜昌市| 龙州县| 电白县|