以下のコードで示すように、sortRange メソッドで指定した並び順にデータを並べ替えることができます。
ユーザーはコンテキストメニューからもソートまたはフィルタリングを行うことができます。
データ型の優先順位は、ブール値 > 文字列 > 数値 です。(例)TRUE > '4' > 8
ソートのカスタマイズ
ユーザーはコールバック関数を使用して、ソートのためのカスタマイズした比較メソッドを定義することができます。
以下のコードではソートのためのlocalCompareの使用方法を示しています。
ユーザーは RangeSorting イベントで使用されるコールバック関数を定義できます。
グループのソート
groupSortオプションを使用して、グループ化したデータのソート方法を指定できます。グループのソート方法には次の種類があります。
flat:グループに関係なくデータを並べ替えます(従来通り)
group:グループの親項目をソートします。グループの子項目はソートされません
child:グループの子項目のみソートします
full:グループの親項目と子項目をすべてソートします
以下のコードではgroupSortオプションの使用方法を示しています。
また、RangeSortingイベントでgroupSortオプションを設定することもできます。
非表示データのソート
非表示のデータをソートするかどうかを設定することができます。ignoreHiddenをtrueにすると、SpreadJSは非表示のデータを除外してソートします。ignoreHiddenをfalseにすると、非表示のデータも含めてソートします。groupSortをgroup/child/fullのいずれかに設定すると、非表示のデータもソートされますが、非表示の状態は維持されます。
以下のコードではignoreHiddenオプションの使用方法を示しています。
また、RangeSortingイベントでignoreHiddenオプションを設定することもできます。
デフォルトでは、ソート範囲にグループが含まれている場合、SpreadJSは groupモードでデータをソートします。その他の場合は、flatモードで非表示データを除外してソートします。
window.onload = function() {
var spread = new GC.Spread.Sheets.Workbook(document.getElementById('ss'), { sheetCount: 2 });
spread.suspendPaint();
spread.fromJSON(sjsData);
initSheet0(spread.getSheet(0));
initSheet1(spread.getSheet(1));
initSheet2(spread.getSheet(2));
initSheet3(spread.getSheet(3));
initSheet4(spread.getSheet(4));
spread.resumePaint();
};
function initSheet0(sheet) {
var style = sheet.getStyle(4, 7);
style.cellButtons= [{
useButtonStyle: true,
caption: "苗字で並べ替える",
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);
sheet.setRowHeight(4,35);
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: "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);
sheet.setRowHeight(5,35);
style = sheet.getStyle(6, 7);
style.cellButtons= [{
useButtonStyle: true,
caption: "Tシャツのサイズで並べ替える",
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);
sheet.setRowHeight(6,35);
}
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;
}
});
}
function initSheet2(sheet) {
sheet.bind(GC.Spread.Sheets.Events.RangeSorting, function (e, info) {
info.groupSort = GC.Spread.Sheets.GroupSort.full;
});
}
function initSheet3(sheet) {
sheet.outlineColumn.options({
columnIndex: 0,
showImage: false,
showIndicator: true,
showCheckBox: true,
maxLevel: 10
});
}
function initSheet4(sheet) {
var style = sheet.getStyle(1, 4);
style.cellButtons= [
{
useButtonStyle: true,
caption: "ignoreHidden = true",
command: function() {
sheet.sortRange(2, 0, 15, 1, true, [
{
index: 0,
ascending: sheet.getValue(1, 3),
},
], {ignoreHidden: true});
},
}, {
useButtonStyle: true,
caption: "ignoreHidden = false",
command: function() {
sheet.sortRange(2, 0, 15, 1, true, [
{
index: 0,
ascending: sheet.getValue(1, 3),
},
], {ignoreHidden: false});
},
}, {
useButtonStyle: true,
caption: "groupSort = group",
command: function() {
sheet.sortRange(2, 0, 15, 1, true, [
{
index: 0,
ascending: sheet.getValue(1, 3),
},
], {groupSort: GC.Spread.Sheets.GroupSort.group});
},
}];
sheet.setStyle(1, 4, style);
}
<!doctype html>
<html style="height:100%;font-size:14px;">
<head>
<meta name="spreadjs culture" content="ja-jp" />
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="$DEMOROOT$/ja/purejs/node_modules/@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/ja/purejs/node_modules/@grapecity/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/ja/purejs/node_modules/@grapecity/spread-sheets-resources-ja/dist/gc.spread.sheets.resources.ja.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;
}