防止谷歌图表表中的默认排序事件 [英] prevent default sort event in google chart table
问题描述
是否可以防止在Google图表表格对象中触发默认排序事件?如果我们从代码游乐场中获取示例,那怎么可能进行了调整,例如防止在单击年龄"标题时发生排序?
Is it possible to prevent default sort event from being fired in a google chart table object? If we take the example from code playground how could this be tweaked so as to e.g. prevent sorting from happening when clicking on "age" header?
即对表图表不执行任何操作(我的想法是对所有标头进行默认排序,除了其中的一个外,将触发另一个单击事件(由外部js中的事件监听器管理)将触发,但不触发sort事件(因此而不出现小排序箭头))
i.e. doing nothing to the table chart (My idea is to have default sorting in all headers except in one, where another clicking event (managed by an event listener in an external js would fire) would be fired but without firing the sort event (and thus without appearing the little sorting arrow))
例如,以下操作不起作用(它仍调用默认的排序方法):
For example, the following doesn't work (it still calls the default sort method):
function drawVisualization() {
var table = new google.visualization.Table(document.getElementById('table'));
table.draw(sortData, null);
var chart = new google.visualization.BarChart(document.getElementById('chart'));
chart.draw(sortData, null);
google.visualization.events.addListener(table, 'sort',
function(event) {
if(event.column != 1){
sortData.sort([{column: event.column, desc: !event.ascending}]);
chart.draw(sortData, null);
}
}
);
}
谢谢您的指点
推荐答案
将表的sort
选项设置为event
.这将禁用默认排序,并在单击标题时使图表引发排序"事件.如果要正确地跟踪排序,则需要使用设置了sortColumn
和sortAscending
选项的重绘图表:
Set the Table's sort
option to event
. This disables the default sorting and makes the chart throw a "sort" event when the headers are clicked. If you want to track sorting properly, you need to redraw the chart with the sortColumn
and sortAscending
options set:
var options = {
sort: 'event'
};
google.visualization.events.addListener(table, 'sort', function(event) {
if(event.column != 1) {
options.sortColumn = event.column;
options.sortAscending = event.ascending;
sortData.sort([{column: event.column, desc: !event.ascending}]);
table.draw(sortData, options);
}
});
function drawVisualization() {
var options = {
sort: 'event'
};
var table = new google.visualization.Table(document.getElementById('table'));
table.draw(sortData, options);
var chart = new google.visualization.BarChart(document.getElementById('chart'));
chart.draw(sortData, null);
google.visualization.events.addListener(table, 'sort', function(event) {
if(event.column != 1) {
options.sortColumn = event.column;
options.sortAscending = event.ascending;
sortData.sort([{column: event.column, desc: !event.ascending}]);
table.draw(sortData, options);
}
});
}
这篇关于防止谷歌图表表中的默认排序事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!