2016-10-24 22 views
0

私はJQGridを持っており、グリッド上に[折りたたみ/すべてを展開]ボタンを実装しました。それは動作しますが、それは信じられないほど遅いです。私が使ったコードは、120行だけで5〜6秒かかります。これのパフォーマンスを改善する方法はありますか?前もって感謝します!JQGrid非常にゆっくりと展開/折りたたむ

 function CollapseAll() { 
      $(".ui-icon-circlesmall-minus").trigger("click"); 
      $("#grid_toppager_left").find('.ui-icon-minus').removeClass('ui-icon-minus').addClass("ui-icon-plus"); 
     } 

     function ExpandAll() { 
      $(".ui-icon-circlesmall-plus").trigger("click"); 
      $("#grid_toppager_left").find('.ui-icon-plus').removeClass('ui-icon-plus').addClass("ui-icon-minus"); 
     } 

     var groups = []; 

     $("#grid").jqGrid({ 
        url: '@Url.Action("GetLoanReport", "Report")', 
        datatype: "json", 
        emptyrecords: "0 records found", 
        height: "auto", 
        mtype: 'POST', 
        maxHeight: maxHeight, 
        postData: { startDate: $("#startDate").val(), endDate: $("#endDate").val(), selectedStatuses: selectedStatuses, selectedProductGroups: selectedProductGroups, assignedBranchList: assignedBranchList, assignedToList: assignedToList, createdByList: createdByList, approvedByList: approvedByList, uploadedByList: uploadedByList }, 
        colNames: ['Branch', 'Status', 'Employee', 'Application ID', 'Customer Name', 'CustNo', 'Product Type', 'Description', 'Security Code', 'Final Rate', 'New Money', 'Total'], 
        colModel: [ 
         { name: 'Branch', index: 'Branch', cellattr: function() { return ' title="my custom fixed tooltip for the column"'; } }, 
         { name: 'Status', index: 'Status' }, 
         { name: 'EmplName', index: 'EmplName' }, 
         { name: 'ApplicationID', index: 'ApplicationID', sorttype: 'number', width: 125, sortable: true, formatter: createLink }, 
         { name: 'CustName', index: 'CustName', formatter: custnameFormatter, width: 200, sortable: true }, 
         { name: 'CustNo', index: 'CustNo', hidden: true, sortable: true }, 
         { name: 'ProductType', index: 'ProductType', width: 100, sortable: true, sorttype: "text" }, 
         { name: 'ProdDesc', index: 'ProdDesc', width: 250, sortable: true }, 
         { name: 'SecurityCode', index: 'SecurityCode', width: 125, sortable: true }, 
         { name: 'FinalRate', index: 'FinalRate', width: 75, align: "right", formatter: 'currency', formatoptions: { suffix: '%' }, sorttype: 'currency', sortable: true }, 
         { name: 'NewMoney', index: 'NewMoney', formatter: 'currency', align: "right", sorttype: 'currency', formatoptions: { thousandsSeparator: ",", decimalPlaces: 0, prefix: "$" }, width: 125, sortable: true }, 
         { name: 'TotalNewMoney', index: 'TotalNewMoney', formatter: 'currency', align: "right", sorttype: 'currency', formatoptions: { thousandsSeparator: ",", decimalPlaces: 0, prefix: "$" }, width: 125, sortable: true } 
        ], 
        jsonReader: { 
         repeatitems: false, 
         root: 'rowdata', 
         page: 'currpage', 
         total: 'totalpages', 
         records: 'totalrecords' 
        }, 
        loadComplete: function() { 
         WaitIndicatorClose(); 

         var reportSum = $("#grid").jqGrid('getCol', 'TotalNewMoney', false, 'sum'); 

         $("#gridPager_right").html("<div id='sumTotal'>Number of Applications: " + $("#grid").getGridParam("records") + ", Total: $" + formatMoney(reportSum, false, null, null, null, true, false) + '</div>'); 
         $("#gridPager_right").show(); 

         if (firstLoad == true) { 
          $(".ui-icon-circlesmall-plus, .ui-icon-circlesmall-minus").each(function() { 
           groups.push({ hid: $(this).closest("tr").attr("id"), collapsed: true }); 
          }); 
         } else { 
          $("#grid tr").each(function() { 
           for (var i = 0; i < groups.length; i++) { 
            if ($(this).attr("id") === groups[i].hid) { 
             if (groups[i].collapsed == false) { 
              $("#grid").jqGrid('groupingToggle', groups[i].hid); 
             } 
            } 
           } 
          }); 
         } 

         firstLoad = false; 

         $(".gridghead_0").attr("title", "Created by Branch"); 

         $(".appLink").on("click", function (e) { 
          var appID = e.currentTarget.innerHTML; 

          ConfirmBox("This will redirect you to the Application page. Are you sure?", 
            function() { 
             $.ajaxSetup({ cache: false }); 
             $.ajax({ 
              cache: false, 
              type: "Get", 
              url: "@Url.Action("VerifyAndSetApplicationID", "Application")", 
              data: { "applicationID": appID }, 
              success: function (data) { 
               if (data.Error) { 
                MessageBox(data.Error); 
               } else { 
                if (data.Success) { 
                 InitializeWriteAccess(appID); 
                } else { 
                 MessageBox(data.NotFound); 
                } 
               } 
              }, 
              error: function (xhr, status, error) { 
              }, 
              complete: function() { 
              } 
             }); 
            }, 
            function() { 
            }); 

         }); 
        }, 
        loadError: function() { 
         WaitIndicatorClose(); 
        }, 
        loadui: 'disable', 
        grouping: true, 
        onClickGroup: function (hid, collapsed) { 
         if ($(".ui-icon-circlesmall-plus").length == 0) { 
          $("#grid_toppager_left").find('.ui-icon-plus').removeClass('ui-icon-plus').addClass("ui-icon-minus"); 
         } else { 
          $("#grid_toppager_left").find('.ui-icon-minus').removeClass('ui-icon-minus').addClass("ui-icon-plus"); 
         } 

         for (var i = 0; i < groups.length; i++) { 
          if (groups[i].hid == hid) { 
           groups[i].collapsed = collapsed; 
          } 
         } 

         if (collapsed == true) { 
          $(".ui-icon-circlesmall-minus:hidden").each(function() { 
           for (var i = 0; i < groups.length; i++) { 
            if (groups[i].hid == $(this).closest("tr").attr("id")) { 
             groups[i].collapsed = true; 
            } 
           } 
          }); 
         } 

         $('#grid').trigger('reloadGrid'); 
        }, 
        groupingView: { 
         groupField: ['Branch', 'Status', 'EmplName'], 
         groupText: ['<b>{0}</b> Count: ({1})', '<b>{0}</b> Count: ({1})', '<b>Created by {0}</b> Count: ({1})'], 
         groupSummary: true, 
         groupColumnShow: false, 
         groupSummaryPos: "header", 
         groupCollapse: true 
        }, 
        loadonce: true, 
        rowNum: 10000, 
        showrownumbers: true, 
        toppager: true, 
        shrinkToFit: true, 
        pgbuttons: false, 
        pginput: false, 
        pager: gridPager 
       }); 
       $("#grid").jqGrid('navGrid', '#gridPager', { add: false, edit: false, del: false, find: false, search: false, refresh: false }); 
       $("#grid").jqGrid('navGrid', '#grid_toppager', { add: false, edit: false, del: false, find: false, search: false, refresh: false, width: 1093 }); 
       $("#grid").jqGrid('navButtonAdd', '#grid_toppager_left', { 
        caption: "Expand/Collapse All", 
        buttonicon: "ui-icon-plus", 
        onClickButton: function() { 
         if ($(".ui-icon-circlesmall-plus").length == 0) { 
          CollapseAll(); 
         } else { 
          ExpandAll(); 
         } 

         $('#grid').trigger('reloadGrid'); 
        } 
       }); 
       $("#grid").jqGrid('navButtonAdd', '#gridPager', { 
        caption: "Export to Excel", 
        onClickButton: function() { 
         fnExcelReport(); 
        } 
       }); 
      }); 
+0

商用の[Guiddo jqGrid JS](http://www.jqGrid.com)のどのバージョンからjqGrid([free jqGrid](https://github.com/free-jqgrid/jqGrid) /guriddo.net/?page_id=103334)またはバージョン<= 4.7の古いjqGrid?あなたが投稿するコードの香辛料は、あまりにも少ない情報を与えます。グリッドに120行すべてを既にロードしていますか?グリッドの作成に使用するコードはどこですか?デモはオンラインですか?どのような方法であれ、私はあなたに無料のjqGrid 4.13.4をロードしようと勧めています。 CDN([ここ](https://github.com/free-jqgrid/jqGrid/wiki/Access-free-jqGrid-from-different-CDNs)を参照してください)。 – Oleg

+0

申し訳ありませんが、無料のjqGridを使用しています –

+0

どのバージョンですか?問題を再現するJavaSctriptコードまたはデモはどこにありますか? – Oleg

答えて

0

あなたが投稿していないコードに問題があるとします。

120行のダミーデータとページャの[Expand/Collapse All]ボタンでthe demoを作成しました。現在の(4.13.4)バージョンのfree jqGridを使用します。以前の(4.13.3)バージョンの無料jqGridと同じデモのパフォーマンスは、私のテストでは同じです。あなたはそれを試して、パフォーマンスが良いことを確認することができます。

"Expand/Collapse All"ボタンの少し変更されたコードを使用しましたが、展開/折りたたみのパフォーマンスに違いはありません。

UPDATED
.jqGrid("navButtonAdd", { 
    caption: "Expand/Collapse All", 
    id: "expand_collapse_all", 
    buttonicon: "ui-icon-plus", 
    onClickButton: function() { 
     var gridId = this.id, icons = this.p.treeIcons, 
      $expandCollapseAll = $("#expand_collapse_all"); 
     if ($expandCollapseAll.find('.ui-icon-minus').length > 0) { 
      $expandCollapseAll.find('.ui-icon-minus') 
       .removeClass('ui-icon-minus') 
       .addClass("ui-icon-plus"); 
      $(".tree-minus").trigger("click"); 
     } else { 
      $expandCollapseAll.find('.ui-icon-plus') 
       .removeClass('ui-icon-plus') 
       .addClass("ui-icon-minus"); 
      $(".tree-plus").trigger("click"); 
     } 
    } 
}); 

私はあなたが最後に掲載onClickGrouploadCompleteのコードは、あなたのパフォーマンスの問題の原因であることがわかります。あなたのケースでは120行ではなく、1000行であるthe demoを試してみてください。 1000行の拡張は遅いですが、IE11のコンピュータでは約0.5秒、Chromeでは約190ミリ秒です。それはあなたが報告した5〜6秒ほどではありません。

The next demoすべての行を展開/折りたたむ前に一時的にグリッドを非表示にしてから表示します。さらにパフォーマンスを向上させます。最後のデモは、Chromeで160ミリ秒、IE11で350ミリ秒ですべてのコンピュータに展開されます。

+0

ok、グリッド全体を追加しました。私はグループ配列で多くの作業をしています。私が行ったことは、各グループの折りたたみ/展開状態を保存しようとしているため、並べ替えとグリッドのリロード時にデフォルトですべてのグループが折りたたまれることはありません。多分あなたはそれをやるより良い方法を知っているかもしれません。ありがとう! –

+0

@ MarkHighfield:あなたの最後のコメントからの質問は、あなたの元の質問から自由に独立しているように思えます - 無料のjqGridでTreeGridを展開/折りたたむパフォーマンス。 [古い答え](http://stackoverflow.com/a/9202378/315935)を見てください。ここで、TreeGridの状態を保存する方法について説明します。もう一度質問のコードを修正したことがわかります。 TreeGridの代わりにデータのグループ化を使用していることがわかりました。申し訳ありませんが、初めに質問を十分に明確にする必要があります。私はあなたが本当に必要とするものを推測するための多くの異なるデモを作成することはできません。 – Oleg

+0

私の言葉がはっきりしない場合はお詫び申し上げます。私のグループの展開/折りたたみのすべてのボタンのパフォーマンスを向上させようとしています –

関連する問題