数据排序

SpreadJS可以按升序或降序对数据进行排序。可以选择表单区域并通过上下文菜单排序,也可以通过表格的表头筛选框进行排序。同时,也可以通过调用表单的sortRange方法进行排序。在一些特定的场景中,传统的升序或降序排序无法满足需求,这就是为什么SpreadJS提供了可定制的compare函数来进行排序。如下代码所示。

<p>使用<strong>sortRange</strong>方法可以对指定区域进行排序,如下所示:</p> <pre><code class="hljs js language-js"><span class="hljs-comment">// sort the range with column 5 descending and column 6 ascending.</span> sheet.sortRange(<span class="hljs-number">1</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">2</span>, <span class="hljs-literal">true</span>, [ { <span class="hljs-attr">index</span>: <span class="hljs-number">5</span>, <span class="hljs-attr">ascending</span>: <span class="hljs-literal">false</span>}, { <span class="hljs-attr">index</span>: <span class="hljs-number">6</span>, <span class="hljs-attr">ascending</span>: <span class="hljs-literal">true</span>}, ]); </code></pre> <p>用户也可以使用上下文菜单排序项或过滤框中对数据进行排序。</p> <p>不同数据的排序优先级为:布尔 &gt; 字符串 &gt; 数值,例如 True &gt; 'A' &gt; 8.</p> <p><strong>Customize sorting</strong></p> <p>用户可以使用回调函数定义自定义的compare方法进行排序。</p> <p>以下代码显示了如何使用localCompare进行排序。</p> <pre><code class="hljs js language-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">pinyinCompare</span> (<span class="hljs-params">obj1, obj2</span>) </span>{ <span class="hljs-keyword">return</span> obj1.toString().localeCompare(obj2.toString(), <span class="hljs-string">'zh'</span>); } sheet.sortRange(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">8</span>, <span class="hljs-number">2</span>, <span class="hljs-literal">true</span>, [ {<span class="hljs-attr">index</span>:<span class="hljs-number">0</span>, <span class="hljs-attr">ascending</span>:<span class="hljs-literal">true</span>, <span class="hljs-attr">compareFunction</span>: pinyinCompare}, <span class="hljs-comment">//Use the pinyin of column 0 to sort;</span> ]); </code></pre> <p>用户可以定义引发<strong>RangeSorting</strong>事件时使用的回调函数。</p> <pre><code class="hljs js language-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">compareSize</span>(<span class="hljs-params">value1, value2</span>) </span>{ <span class="hljs-comment">//...</span> <span class="hljs-comment">//return -1;</span> <span class="hljs-comment">//return 1;</span> <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; } sheet.bind(GC.Spread.Sheets.Events.RangeSorting, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">e, info</span>) </span>{ <span class="hljs-comment">//if(info.range is suited) {</span> info.compareFunction = compareSize; <span class="hljs-comment">// set the customize function.</span> <span class="hljs-comment">//}</span> }); </code></pre> <p>Group sort 你可以使用groupSort函数来设置是否将数据分组,GroupSort提供以下的方式:</p> <ul> <li><strong>flat:排序忽略分组</strong></li> <li><strong>group:仅在组与组之间排序,组内不进行排序</strong></li> <li><strong>child:只在组内排序</strong></li> <li><strong>full:排序时移动组,并且对组内部进行排序</strong></li> </ul> <p>下面的代码展示了组排序选项。</p> <pre><code class="hljs js language-js">sheet.sortRange(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">8</span>, <span class="hljs-number">2</span>, <span class="hljs-literal">true</span>, [{<span class="hljs-attr">index</span>:<span class="hljs-number">0</span>, <span class="hljs-attr">ascending</span>:<span class="hljs-literal">true</span>},], {<span class="hljs-attr">groupSort</span>: GC.Spread.Sheets.GroupSort.flat}); </code></pre> <p>用户可以定义在<strong>RangeSorting</strong>事件发生时要使用的<strong>groupSorting</strong>选项。</p> <pre><code class="hljs js language-js">sheet.bind(GC.Spread.Sheets.Events.RangeSorting, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">e, info</span>) </span>{ info.groupSort = GC.Spread.Sheets.GroupSort.full; <span class="hljs-comment">// set the sorting GroupSort with full when sorting by UI.</span> }); </code></pre> <p><strong>排序忽略隐藏部分</strong></p> <p>你能够设置当排序时是否要忽略被隐藏的值, 当<strong>ignoreHidden</strong>设置为true, spread会跳过并且不移动被隐藏的值。 当<strong>ignoreHidden</strong>设置为false, spread会比较并移动被隐藏的值。 当<strong>groupSort</strong>设置为group/child/full, SpreadJS会移动被隐藏的值并且移动行/列的可见性并隐藏值。</p> <p>下面的代码会展示使用ignoreHidden选项.</p> <pre><code class="hljs js language-js">sheet.sortRange(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">8</span>, <span class="hljs-number">2</span>, <span class="hljs-literal">true</span>, [{<span class="hljs-attr">index</span>:<span class="hljs-number">0</span>, <span class="hljs-attr">ascending</span>:<span class="hljs-literal">true</span>},], {<span class="hljs-attr">ignoreHidden</span>: <span class="hljs-literal">false</span>}); </code></pre> <p>用户可以定义<strong>RangeSorting</strong>事件发生时要使用<strong>ignoreHidden</strong>选项。</p> <pre><code class="hljs js language-js">sheet.bind(GC.Spread.Sheets.Events.RangeSorting, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">e, info</span>) </span>{ info.ignoreHidden = <span class="hljs-literal">false</span>; <span class="hljs-comment">// set the sorting all the values when sorting by UI.</span> }); </code></pre> <p>默认情况下,如果排序范围包括组,SpreadJS将使用group排序对数据进行排序。否则,它将使用flat排序并且忽略隐藏。</p>
window.onload = function() { var spread = new GC.Spread.Sheets.Workbook(document.getElementById('ss')); spread.suspendPaint(); spread.fromJSON(sjsData); initSheet0(spread.getSheet(0)); initSheet1(spread.getSheet(1)); spread.resumePaint(); }; function initSheet0(sheet) { var style = sheet.getStyle(4, 7); style.cellButtons= [{ useButtonStyle: true, caption: "Sort with last name", width: 222, command: function() { sheet.sortRange(4, 0, 27, 5, true, [ { index: 1, ascending: true, compareFunction: function (value1, value2) { var str1 = value1.split(" ")[1], str2 = value2.split(" ")[1]; return str1.localeCompare(str2); } }, ]) }, }]; sheet.setStyle(4, 7, style); var grade = ["Freshmen", "Sophomore", "Junior", "Senior"]; var clothesSize = ["XX-Small", "X-Small", "Small", "Medium", "Large", "X-Large", "XX-Large"]; function compareList(obj1, obj2, list) { var index1 = list.indexOf(obj1), index2 = list.indexOf(obj2); if (index1 > index2) { return 1; } else if (index1 < index2) { return -1; } else { return 0; } } style = sheet.getStyle(5, 7); style.cellButtons= [{ useButtonStyle: true, caption: "Sort with Grade", width: 222, command: function() { sheet.sortRange(4, 0, 27, 5, true, [ { index: 2, ascending: true, compareFunction: function (value1, value2) { return compareList(value1, value2, grade); } }, ]) }, }]; sheet.setStyle(5, 7, style); style = sheet.getStyle(6, 7); style.cellButtons= [{ useButtonStyle: true, caption: "Sort with T-Shirt Size", width: 222, command: function() { sheet.sortRange(4, 0, 27, 5, true, [ { index: 3, ascending: true, compareFunction: function (value1, value2) { return compareList(value1, value2, clothesSize); } }, ]) }, }]; sheet.setStyle(6, 7, style); } function initSheet1(sheet) { function sortDomain (value1, value2) { var str1 = value1.substr(value1.lastIndexOf(".") + 1), str2 = value2.substr(value2.lastIndexOf(".") + 1); return str1.localeCompare(str2); } function sortIP (ip1, ip2) { var value1 = ip1.split("."), value2 = ip2.split("."); for (var i=0; i < 4; i++){ var num1 = parseInt(value1[i]), num2 = parseInt(value2[i]); if (num1 > num2) { return 1; } else if (num1 < num2){ return -1; } } return 0; } sheet.bind(GC.Spread.Sheets.Events.RangeSorting, function (e, info) { if (info.col === 0) { info.compareFunction = sortDomain; } else if (info.col === 1) { info.compareFunction = sortIP; } }); }
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta name="spreadjs culture" content="zh-cn" /> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/zh/purejs/node_modules/@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity/spread-sheets-resources-zh/dist/gc.spread.sheets.resources.zh.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/data/sorting.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/js/license.js" type="text/javascript"></script> <script src="app.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <body> <div class="sample-tutorial"> <div id="ss" class="sample-spreadsheets"></div> </div> </body> </html>
.sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-spreadsheets { width: 100%; height: 100%; overflow: hidden; float: left; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }