[{"id":"b66f8b1e-cc14-4e89-9679-abd5687d283d","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"569bb90a-ea68-46c6-96f1-ab151c120714","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"5eb52f08-2d1a-4362-9ffc-4871bdc10f3f","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"341dd607-b97d-4d70-bde2-53acda6b6c95","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"05349273-414f-4208-9ea2-c4fc8f4ea2cb","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"86089f76-b778-4d52-821e-6f27de3df613","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"b81e4fd6-1fc5-43a0-a258-b6e16a5cbec6","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"8aa8ce31-43e4-438e-951f-241608435260","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"37343f41-6ec2-4c7e-b21d-2cc18d5ce1e0","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"5915e52f-64f8-4146-b8bd-81bead6324a3","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"836ba889-af9e-460d-a4cc-c24d922795f2","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"4ae14b06-bb68-4394-a210-a46b8f028346","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"d8f42066-e9dc-4411-bdcf-43b1a203370c","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"7ef86f16-b1a0-49f7-9592-612b9be02b25","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"b29c7775-a9a4-451e-a1b5-01d19ed5ca5e","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"e56f3989-8f81-46af-90fa-a4813eeb976f","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"2526c963-f170-45a8-923e-91b0712a9810","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"24575cf0-501a-44f9-8426-c40f8f4b5552","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"1a6f8d7f-acd6-42be-8c4f-f464c6218381","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"82b176fd-5cab-498c-909e-8fa7d29c38d8","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"1708b3a9-4f37-44a8-8f0e-f9a2d2e5d940","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"c9ac246e-29fb-4bc4-8231-8439795bb590","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"94f2a5c3-2539-436a-af75-23fbbd1a3957","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"d99594f4-2d40-4df4-9419-ba2ca6aa3f7f","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"fd66e72d-0f10-4f57-9807-6db26290ab2e","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"8f344863-503d-4bc3-a594-3815e7d55f5c","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"9be82601-de9d-4c18-948a-23ab6f4dd431","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"6495f3c0-b463-47e0-b08a-ca949672211e","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"b4bede08-3f08-4839-ba4a-abc7ac195bde","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"4afcdfc0-3ff5-4f2b-a223-f8fc042a5bbe","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"422062c4-fa40-4771-a86f-008efe6d86e5","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"d87e8013-86a7-4840-8d25-6f62e14eb4ac","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"a6acedfd-4043-4c64-a5d1-aec3326df9e7","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"dbdc0b24-06c4-48b9-8d6c-7455119dc773","tags":[{"product":null,"links":null,"id":"9e117e35-984a-4c14-95ca-ef0ec7b9fb60","name":"\u65B0\u589E","color":"DarkGreen","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"a3856849-954a-4cfc-96a6-382e530d3638","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]},{"id":"4a39306b-ffa5-433d-80a3-28e41f929b72","tags":[{"product":null,"links":null,"id":"a2f84374-4a3f-4d22-96fd-765e9de495bf","name":"\u66F4\u65B0","color":"Coral","productId":"098be112-50ec-44e4-b746-6bc8bf76af97"}]}]
        
(Showing Draft Content)

自定义切片器

您可以使用 GeneralSlicerData 类或 TableSlicerData 类自定义切片器。

GeneralSlicerData 类用于处理二维数组的数据。 该类提供可用于筛选数据并获得筛选结果的属性和方法。 您可以将多个切片器附加到自定义切片器。 每个切片器可以筛选一列数据。 筛选完成后,其他切片器将由 GeneralSlicerData 类通知。 然后,切片器可以从 GeneralSlicerData 获取筛选后的结果,并更新其UI。

切片器与 GeneralSlicerData 类基于以下步骤共同使用:

  • 使用您的数据创建一个新的 GeneralSlicerData
  • 创建一个新的自定义切片器,并将其附加到 GeneralSlicerData
  • GeneralSlicerData 获取列数据,并构建切片器UI。
  • 响应UI事件并调用 GeneralSlicerDatadoFilter 方法。
  • GeneralSlicerData 获取筛选后的结果,并更新切片器UI。

您可以使用 getData 方法来获取特定列的所有数据。 您可以使用 getExclusiveData 方法获取专有(非重复数据)。 您可以使用 doFilter 方法筛选列,或使用 doUnfilter 方法。 有关方法的完整列表,请参考 GeneralSlicerData 类。

您可以使用 attachListener 函数将侦听器附加到 GeneralSlicerData ,并使用 detachListener 函数。

下图显示了一个自定义切片器。

示例代码

本示例创建一个自定义切片器。

<!DOCTYPE html>
<html>
<head>
 <title>SpreadJS Sample</title>
     <link href="./css/gc.spread.sheets.excel2013white.13.0.0.css" rel="stylesheet" type="text/css" />
     <script src="http://code.jquery.com/jquery-2.0.2.js" type="text/javascript"></script>
     <script src="./scripts/gc.spread.sheets.all.13.0.0.min.js"></script>
    <script type="text/javascript">
     //定义数据源。
         columnNames = ["Name", "City", "Birthday"];
     data = [
         ["Bob", "NewYork", "1968/6/8"],
         ["Betty", "Washington", "1972/7/3"],
         ["Alice", "NewYork", "1964/3/2"]];
    //定义自定义切片器。
         function MySlicer(container) {
         this.container = container;
         this.slicerData = null;
         this.columnName = null;
     }
     MySlicer.prototype.setData = function (slicerData, columnName) {
         this.slicerData = slicerData;
         this.columnName = columnName;
         this.slicerData.attachListener(this);
         this.onDataLoaded();
     }
     MySlicer.prototype.onDataLoaded = function () {
         //创建切片器dom树。
             var columnName = this.columnName,
         exclusiveData = this.slicerData.getExclusiveData(columnName);
         $(this.container).append($('<span>' + this.columnName + ':</span>' + '<br />'));
         var domString = "";
         for (var i = 0; i < exclusiveData.length; i++) {
             domString += '<input type="checkbox" name="' + columnName + '" value="' + exclusiveData[i] + '">';
             domString += '<span>' + exclusiveData[i] + '</span>';
             domString += '<br />';
         }
         $(this.container).append($(domString));
         //将事件附加到dom。
             var self = this;
         $("[name='" + self.columnName + "']").change(function () {
             var slicer = self,
             exclusiveData = slicer.slicerData.getExclusiveData(slicer.columnName),
             parent = $(this).parent(),
             items = parent.children(),
             indexes = [];
             for (var i = 0, length = items.length; i < length; i++) {
                 if (items[i].checked) {
                     var value = items[i].value;
                     if (!isNaN(parseInt(value))) {
                         value = parseInt(value);
                     }
                     indexes.push(exclusiveData.indexOf(value))
                 }
             }
             if (indexes.length === 0) {
                 slicer.slicerData.doUnfilter(slicer.columnName);
             } else {
                 slicer.slicerData.doFilter(slicer.columnName, { exclusiveRowIndexes: indexes });
             }
         });
     };
     MySlicer.prototype.onFiltered = function () {
         //以下是显示筛选结果的示例。
             var slicerdata = this.slicerData;
         var filteredRowIndexs = slicerdata.getFilteredRowIndexes();
         var trs = $listTable.find("tr");
         for (var i = 0; i < slicerdata.data.length; i++) {
             if (filteredRowIndexs.indexOf(i) !== -1) {
                 $(trs[i + 1]).show();
             } else {
                 $(trs[i + 1]).hide();
             }
         }
     }
    //定义显示筛选结果方法。
         function initFilteredResultList() {
         var tableStr = "<table border='1' cellpadding='0' cellspacing='0'><tr>";
         for (var i = 0; i < columnNames.length; i++) {
             tableStr += "<th>" + columnNames[i] + "</th>";
         }
         tableStr += "</tr>";
         for (var i = 0; i < data.length; i++) {
             tableStr += "<tr>";
             for (var j = 0; j < data[i].length; j++) {
                 tableStr += "<td>" + data[i][j] + "</td>";
             }
             tableStr += "</tr>";
         }
         tableStr += "</table>";
         $listTable = $(tableStr);
         $("#list").append($listTable);
     }
    $(document).ready(function () {
         //初始化筛选结果列表。
             initFilteredResultList();
         //创建一个自定义切片器,然后将此切片器添加到“ slicerContainer” div中。
             var slicer = new MySlicer($("#slicerContainer")[0]);
         var slicerData = new GC.Spread.Slicers.GeneralSlicerData(data, columnNames);
         slicer.setData(slicerData, "Name");
     });
 </script>
</head>
 <body>
 <div id="slicerContainer" style="border:1px solid gray;width:190px"></div>
     <hr/>
     <div id="list" style="width:300px;float:left"></div>
 </body>
 </html>

本示例创建一个自定义切片器,并使用 TableSlicerData 数据类。

<!DOCTYPE html>
<html>
<head>
 <title>SpreadJS Development Sample</title>
     <link href="./css/gc.spread.sheets.excel2013white.13.0.0.css" rel="stylesheet" type="text/css" />
    <script src="http://code.jquery.com/jquery-2.0.2.js" type="text/javascript"></script>
     <script src="./scripts/gc.spread.sheets.all.13.0.0.min.js"></script>
     <script type="text/javascript">
     //定义数据源
         data = [
         { "Name": "Bob", "City": "NewYork", "Birthday": "1968/6/8" },
         { "Name": "Bob", "City": "NewYork", "Birthday": "1968/6/8" },
         { "Name": "Bob", "City": "NewYork", "Birthday": "1968/6/8" },
         { "Name": "Bob", "City": "NewYork", "Birthday": "1968/6/8" },
         { "Name": "Betty", "City": "Washington", "Birthday": "1972/7/3" },
         { "Name": "Betty", "City": "Washington", "Birthday": "1972/7/3" },
         { "Name": "Betty", "City": "Washington", "Birthday": "1972/7/3" },
         { "Name": "Alice", "City": "NewYork", "Birthday": "1964/3/2" },
         { "Name": "Alice", "City": "NewYork", "Birthday": "1964/3/2" },
         { "Name": "Alice", "City": "NewYork", "Birthday": "1964/3/2" }];
    //自定义切片器
         function MySlicer(container) {
         this.container = container;
         this.slicerData = null;
         this.columnName = null;
     }
     MySlicer.prototype.setData = function (slicerData, columnName) {
         this.slicerData = slicerData;
         this.columnName = columnName;
         this.slicerData.attachListener(this);
         this.onDataLoaded();
     }
     MySlicer.prototype.onDataLoaded = function () {
         //创建切片器dom树。
             var columnName = this.columnName,
         exclusiveData = this.slicerData.getExclusiveData(columnName);
         $(this.container).append($('<span>' + this.columnName + ':</span>' + '<br />'));
         var domString = "";
         for (var i = 0; i < exclusiveData.length; i++) {
             domString += '<input type="checkbox" name="' + columnName + '" value="' + exclusiveData[i] + '">';
             domString += '<span>' + exclusiveData[i] + '</span>';
             domString += '<br />';
         }
         $(this.container).append($(domString));
         //将事件附加到dom。
             var self = this;
         $("[name='" + self.columnName + "']").change(function () {
             var slicer = self,
             exclusiveData = slicer.slicerData.getExclusiveData(slicer.columnName),
             parent = $(this).parent(),
             items = parent.children(),
             indexes = [];
             for (var i = 0, length = items.length; i < length; i++) {
                 if (items[i].checked) {
                     var value = items[i].value;
                     if (!isNaN(parseInt(value))) {
                         value = parseInt(value);
                     }
                     indexes.push(exclusiveData.indexOf(value))
                 }
             }
             if (indexes.length === 0) {
                 slicer.slicerData.doUnfilter(slicer.columnName);
             } else {
                 slicer.slicerData.doFilter(slicer.columnName, { exclusiveRowIndexes: indexes });
             }
         });
     };
     MySlicer.prototype.onFiltered = function () {
         //如果数据已被SpreadJS表筛选,则同步状态。
             var slicerdata = this.slicerData;
         var exclusiveIndexes = slicerdata.getFilteredIndexes(this.columnName);
         $.each($("#slicerContainer").children("input"), function (i,input) {
        });
     }
    $(document).ready(function () {            
         var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"),{sheetCount:1});
         var sheet = spread.getActiveSheet();
        //创建一个自定义切片器,然后将此切片器添加到“ slicerContainer” div中。
             var slicer = new MySlicer($("#slicerContainer")[0]);
         var table = sheet.tables.addFromDataSource("table1", 1, 1, data);
         var slicerData = table.getSlicerData();
         slicer.setData(slicerData, "Name");
    });
 </script>
</head>
 <body>
 <div id="slicerContainer" style="border:1px solid gray;width:190px"></div>
     <hr />
     <div id="ss" style="width:500px;height:500px;border:1px solid gray"></div>
 </body>
 </html>