Browse Source

定额库搜索分页,滚动条到底分页搜索

zhongzewei 6 years ago
parent
commit
53d754b52a

+ 1 - 1
modules/complementary_ration_lib/controllers/searchController.js

@@ -20,7 +20,7 @@ class SearchController extends BaseController{
 
     findRation(req, res){
         let data = JSON.parse(req.body.data);
-        searchDao.findRation(req.session.sessionUser.id, req.session.sessionCompilation._id, data.rationRepId, data.keyword, function (err, data) {
+        searchDao.findRation(req.session.sessionUser.id, req.session.sessionCompilation._id, data.rationRepId, data.keyword, data.skip, function (err, data) {
             callback(req, res, err, '', data);
         });
     }

+ 40 - 23
modules/complementary_ration_lib/models/searchModel.js

@@ -62,7 +62,13 @@ class SearchDao{
         return ration;
     }
 
-    async findRation(userId, compilationId, rationRepId, keyword, callback){
+    //@param {Object}skip({std: Number, comple: Number})
+    async findRation(userId, compilationId, rationRepId, keyword, skip, callback){
+        //每次限制结果数
+        const limit = 50;
+        //结果数
+        let resultCount = 0,
+            rst = {data: [], count: null};
         try{
             //是否需要查找补充定额
             let findCompleRtion = rationRepId.length > 0 && rationRepId.includes(compleRationLib) ? true : false;
@@ -78,16 +84,6 @@ class SearchDao{
                     '$or': [{'isDeleted': {"$exists":false}}, {'isDeleted': null}, {'isDeleted': false}, {deleteInfo: null}]
                 }]
             };
-
-            let stdGljIds = [],
-                comGljIds = [];
-            let stdRations = rationRepId.length === 0 ? [] : await stdRationModel.find(filter);
-            for(let i = 0, len = stdRations.length; i < len; i++){
-                stdRations[i]._doc.type = 'std';
-                for(let glj of stdRations[i].rationGljList){
-                    stdGljIds.push(glj.gljId);
-                }
-            }
             let compleFilter = {
                 userId: userId,
                 compilationId: compilationId,
@@ -97,18 +93,39 @@ class SearchDao{
                     '$or': [{deleteInfo: null}, {'deleteInfo.deleted': false}]
                 }]
             };
-            let compleRations = findCompleRtion ? await compleRationModel.find(compleFilter) : [];
-            for(let i = 0, len = compleRations.length; i <len; i++){
-                compleRations[i]._doc.type = 'complementary';
-                for(let glj of compleRations[i].rationGljList){
-                    if(glj.type === 'std'){
-                        stdGljIds.push(glj.gljId);
-                    }
-                    else {
-                        comGljIds.push(glj.gljId);
+            //结果数
+            if (skip && skip.std === 0 && skip.comple === 0) {
+                resultCount += rationRepId.length === 0 ? 0 : await stdRationModel.find(filter).count();
+                resultCount += findCompleRtion ? await compleRationModel.find(compleFilter).count() : 0;
+                rst.count = resultCount;
+            }
+            //搜索定额
+            let stdGljIds = [],
+                comGljIds = [];
+            let stdRations = rationRepId.length === 0 ? [] : await stdRationModel.find(filter).sort({code: 1}).skip(skip.std).limit(limit);
+            for(let i = 0, len = stdRations.length; i < len; i++){
+                stdRations[i]._doc.type = 'std';
+                for(let glj of stdRations[i].rationGljList){
+                    stdGljIds.push(glj.gljId);
+                }
+            }
+            let compleRations = [];
+            let residueLimit = limit - stdRations.length;
+            if (residueLimit > 0) {
+                compleRations = findCompleRtion ? await compleRationModel.find(compleFilter).sort({code: 1}).skip(skip.comple).limit(residueLimit) : [];
+                for(let i = 0, len = compleRations.length; i <len; i++){
+                    compleRations[i]._doc.type = 'complementary';
+                    for(let glj of compleRations[i].rationGljList){
+                        if(glj.type === 'std'){
+                            stdGljIds.push(glj.gljId);
+                        }
+                        else {
+                            comGljIds.push(glj.gljId);
+                        }
                     }
                 }
             }
+
             //设置悬浮信息
             stdGljIds = Array.from(new Set(stdGljIds));
             comGljIds = Array.from(new Set(comGljIds));
@@ -178,11 +195,11 @@ class SearchDao{
                 }
                 ration._doc.hint = hintsArr.join('<br>');
             }
-
-
-            callback(0, stdRations.concat(compleRations));
+            rst.data = stdRations.concat(compleRations);
+            callback(0, rst);
         }
         catch(err){
+            console.log(err);
             callback(err, null);
         }
     }

+ 61 - 16
web/building_saas/main/js/views/std_ration_lib.js

@@ -5,6 +5,7 @@
 /*var rationChapterSpread, sectionRationsSpread;*/
 
 var rationLibObj = {
+    searchLimit: 50,
     libType: {complementary: 0, std: 1},
     compleRationLibId: 'compleRationLib',
     doAfterGetRationTree: null, //获取章节树回调
@@ -214,6 +215,19 @@ var rationLibObj = {
             });
         }
     },
+    //滚动条到底部加载
+    onRationSpreadTopRowChanged: function (sender, args) {
+        let me = rationLibObj;
+        if(me.searching) {
+            return;
+        }
+        let bottomRow = args.sheet.getViewportBottomRow(1),
+            rowCount = args.sheet.getRowCount();
+        //滚到了底部
+        if (bottomRow + 1 - me.sectionRationsSetting.emptyRows === rowCount - me.sectionRationsSetting.emptyRows) {
+            seachRation();
+        }
+    },
     loadStdRationContextMenu: function () {
         let rationSpread = rationLibObj.sectionRationsSpread, rationSheet = rationSpread.getActiveSheet(),  rationModel = projectObj.project.Ration;;
         $.contextMenu({
@@ -449,6 +463,25 @@ var rationLibObj = {
         let defaultLib = _.find(ration_lib,{'isDefault':true});
         let libID = defaultLib?defaultLib.id:ration_lib[0].id;
         return parseInt(libID);
+    },
+    //@param {Array}datas(resultCache) @return {Object}
+    //搜索skip信息,不能被每页搜索数整除,则说明上次搜索已经搜索完整
+    getSearchSkip: function (datas) {
+        if (datas.length % this.searchLimit !== 0) {
+            return null;
+        }
+        let skip = {std: 0, comple: 0};
+        if (!datas || !Array.isArray(datas) || datas.length === 0) {
+            return skip;
+        }
+        for (let data of datas) {
+            if (data.type === 'std') {
+                skip.std++;
+            } else {
+                skip.comple++;
+            }
+        }
+        return skip;
     }
 };
 
@@ -489,7 +522,7 @@ $('#rationSearchKeyword').keyup(function () {
     let keyword = $('#rationSearchKeyword').val();
     if(keyword === ''){
         if($('#rationSearchResult').is(':visible')){
-            rationLibObj.resultCache = null;
+            rationLibObj.resultCache = [];
             $('#rationSearchResult').hide();
             $(".main-data-side-search", $('#rationSearchResult')).height(0);
             switchRationSearchMode(0);
@@ -501,6 +534,7 @@ $('#rationSearchKeyword').keyup(function () {
 
 //变换搜索本定额、全部定额状态
 function switchRationSearchMode(mode) {
+    rationLibObj.resultCache = [];
     //搜索本定额
     if(mode === 0){
         $('#curRationLib').removeClass('btn-light');
@@ -534,11 +568,16 @@ $('#allRationLibs').click(function () {
 });
 
 //搜索
-$('#rationSearch').click(function () {
+function seachRation(){
+    let skip = rationLibObj.getSearchSkip(rationLibObj.resultCache);
+    if (!skip) {
+        return;
+    }
+    rationLibObj.searching = true;
     var keyword = $('#rationSearchKeyword').val();
     if(keyword === ''){
         if($('#rationSearchResult').is(':visible')){
-            rationLibObj.resultCache = null;
+            rationLibObj.resultCache = [];
             $('#rationSearchResult').hide();
             $(".main-data-side-search", $('#rationSearchResult')).height(0);
             autoFlashHeight();
@@ -610,33 +649,29 @@ $('#rationSearch').click(function () {
         });
     };
     var showResult = function (result) {
-        rationLibObj.resultCache = result;
         if(!rationLibObj.resultSpread){
             let resultSpread = SheetDataHelper.createNewSpread($('.main-data-side-search')[0]);
             rationLibObj.resultSpread = resultSpread;
             bindContextmenuOpr(resultSpread.getActiveSheet());
             SheetDataHelper.loadSheetHeader(rationLibObj.sectionRationsSetting, resultSpread.getActiveSheet());
             resultSpread.bind(GC.Spread.Sheets.Events.CellDoubleClick, rationLibObj.onRationSpreadCellDoubleClick);
+            resultSpread.bind(GC.Spread.Sheets.Events.TopRowChanged, rationLibObj.onRationSpreadTopRowChanged);
         }else {
             rationLibObj.resultSpread.refresh();
         }
         SheetDataHelper.loadSheetData(rationLibObj.sectionRationsSetting, rationLibObj.resultSpread.getActiveSheet(), result);
         rationLibObj.setTagForHint(rationLibObj.resultSpread.getActiveSheet(), result);
+        rationLibObj.resultCache = result;
     };
     $.bootstrapLoading.start();
-    CommonAjax.post('/complementaryRation/api/findRation', {'user_id': userID, 'rationRepId': rationLibIDs, 'keyword': keyword}, function (result) {
-        //sort
-        result.sort(function (a, b) {
-            let rst = 0;
-            if(a.code > b.code) rst = 1;
-            else if(a.code < b.code) rst = -1;
-            return rst;
-        });
+    CommonAjax.post('/complementaryRation/api/findRation', {'user_id': userID, 'rationRepId': rationLibIDs, 'keyword': keyword, skip: skip}, function (result) {
         var resultObj = $('#rationSearchResult');
-        $('#rationSearchCount').text(`搜索结果:${result.length.toString()}`);
-        $('a', result).unbind('click');
+        if (result.count !== null) {
+            $('#rationSearchCount').text(`搜索结果:${result.count}`);
+        }
+        $('a', resultObj).unbind('click');
         $('a', resultObj).bind('click', function () {
-            rationLibObj.resultCache = null;
+            rationLibObj.resultCache = [];
             switchRationSearchMode(0);
             resultObj.hide();
             $(".main-data-side-search", resultObj).height(0);
@@ -645,9 +680,19 @@ $('#rationSearch').click(function () {
         });
         resultObj.show();
         $(".main-data-side-search", resultObj).height($(window).height() - $(".header").height() - $(".toolsbar").height() - 64);
-        showResult(result);
+        showResult(rationLibObj.resultCache.concat(result.data));
+        rationLibObj.searching = false;
+        //以防一开始就需要加载后面的分页数据
+        if (result.data.length > 0) {
+            rationLibObj.onRationSpreadTopRowChanged({}, {sheet: rationLibObj.resultSpread.getActiveSheet()});
+        }
         $.bootstrapLoading.end();
     }, function () {
+        rationLibObj.searching = false;
         $.bootstrapLoading.end();
     });
+}
+$('#rationSearch').click(function () {
+    rationLibObj.resultCache = [];
+    seachRation();
 });