简体   繁体   English

Google可视化表中的“自定义排序”列

[英]Customize Sorting column in Google Visualization Table

I have successfully implemented a google visualization table and add customize sorting process just for one column by adding events listener. 我已经成功实现了Google可视化表,并通过添加事件监听器仅针对一列添加了自定义排序过程。

Background : Data comes from the database to render the table. 背景:数据来自数据库以呈现表。 I have few table columns with 'Status' column. 我的“状态”列中有几个表列。 This column should sort according to another data field which isn't displaying the table.(All data comes from the database with one query. They all working well. ) In addListener I sort the array that I'm getting from the database according to my requirement and recreate the table by table.draw(data, options); 此列应根据不显示表的另一个数据字段进行排序。(所有数据都来自一个查询的数据库。它们都运行良好。)在addListener中,我根据从数据库中获取的数组进行排序我的要求,并通过table.draw(data, options);重新创建表table.draw(data, options); .

google.visualization.events.addListener(table, 'sort',
          function(event) {
        if(event.column ==  4){
        }
   }
} 

The problem is now google visualization table always sort by second column during the load. 现在的问题是,在加载过程中,谷歌可视化表始终按第二列排序。 If user selected any other column to sort table will sort according to the selected column. 如果用户选择了其他任何要排序的列,表将根据选定的列进行排序。 They all work as normal (Sorted column display by down arrow in table header). 它们都正常工作(表标题中的向下箭头按排序的列显示)。

But when I tried to sort by the field I added my own sorting process, earlier sorting not remove (I can see down arrow mark still at previous sorted table header) so sorting not work properly with added sorting code. 但是,当我尝试按字段排序时,我添加了自己的排序过程,因此无法删除较早的排序(我仍然可以看到以前的排序表头处的向下箭头标记),因此无法在添加的排序代码下正常进行排序。 How to prevent this issue. 如何预防这个问题。

If this isn't clear or need more information please let me know. 如果不清楚或需要更多信息,请告诉我。

UPDATE: Explanation 更新:说明

Table draw from the array which is creating from the databse. 从数据库创建的数组中绘制表格。 this is the structure 这是结构

Array
(
    [0] => Array
        (
            [Field 1] => test 1
            [Field 2] =>test 2
            [sortOrder] => 4
            [Field 3] => Array
                (
                    [key 1] => val 1
                    [Name] => test
                    [Date] => 2016-08-18 00:00:00
                    [warningLevel] => OK
                )

        )
....
....
...

From this array I use [Field 1], [Field 2] and [warningLevel] field as table columns. 在此数组中,我将[Field 1],[Field 2]和[warningLevel]字段用作表列。 But [warningLevel] column should sort according to [sortOrder] field in array . 但是[warningLevel] column should sort according to [sortOrder] field in array

So what I did, I add listener and inside it I rearrange(sort) the whole data array and recreate the table again. 因此,我做了什么,我添加了侦听器,并在其中重新排列(排序)整个数据数组并再次重新创建表。

 google.visualization.events.addListener(table, 'sort',
          function(event) {
          if(event.column ==  4){
vData.sort(function(a, b) {
                    var valueA, valueB;

                    valueA = a['sortOrder']; 
                    valueB = b['sortOrder'];
                    if (valueA < valueB) {
                        return -1;
                    }
                    else if (valueA > valueB) {
                        return 1;
                    }
                    return 0;
                });
table.draw(vdata, options);

}
  });

All the other table column should sort as the normal way (When user sort one column it is sorting and if he moves to sort another column earlier sorting disregard and sort the newly selected column. Sorted column header has arrow icon ) and they are working fine. 所有其他表列均应按常规方式进行排序(当用户对一个列进行排序时,如果正在排序,并且如果他先移至对另一列进行排序,则忽略排序并对新选择的列进行排序。已排序的列标题带有箭头图标),它们可以正常工作。 The problem is when I tried to sort the Status column last sorted column still use it's sorting.Also the arrow in header still displaying in earlier sorted column. 问题是当我尝试对状态列进行排序时,最后一个排序列仍在使用它的排序方式。而且标题中的箭头仍显示在较早的排序列中。 So this column sorting not working as expect. 因此,此列排序无法按预期方式工作。

Any help would be appreciated. 任何帮助,将不胜感激。

sounds like you need to set the following configuration options before drawing the chart 听起来您需要在绘制图表之前设置以下配置选项
sortColumn & sortAscending sortColumnsortAscending

you control over which column the sorting arrow appears (and direction), 您可以控制排序箭头显示在哪一列(和方向),
regardless of the actual sort taking place 不管实际发生的排序

see following working snippet, 请参阅以下工作片段,
regardless what the user clicks, the data will be sorted on column 0 无论用户点击什么,数据都会在第0列上排序
while the arrows follow what was actually clicked... 而箭头跟随实际点击的内容...

 google.charts.load('current', { callback: function () { var data = google.visualization.arrayToDataTable([ ['Level', 'Status', 'Sort'], [1, 'Critical', {v: 1, f: 'Critical'}], [2, 'OK', {v: 2, f: 'OK'}], [3, 'Warning', {v: 3, f: 'Warning'}], [4, 'Message', {v: 4, f: 'Message'}] ]); var options = { allowHtml: true, cssClassNames: { tableCell: 'googleTableCell' }, sort: 'event', sortAscending: true, sortColumn: 0 }; var chart = new google.visualization.Table(document.getElementById('chart_div_table')); google.visualization.events.addListener(chart, 'sort', function (sender) { data.sort([{column: 0, desc: !sender.ascending}]); options.sortAscending = sender.ascending; options.sortColumn = sender.column; chart.draw(data, options); }); chart.draw(data, options); }, packages: ['table'] }); 
 <script src="https://www.gstatic.com/charts/loader.js"></script> <div id="chart_div_table"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM