我遵循以下针对我的图表场景的答案,我在渲染图表、过滤图表等预期功能方面没有问题。
我的问题是我使用 JQuery 按钮清除可选择的内容,清除按钮被创建并禁用。我正在尝试启用该按钮,但它不起作用。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css"/>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
var click = 0;
function filter_st_charts(){
var st_chartview={};
var st_selectedvalues = [];
var r_row;
var r_view = {
columns: [0]
};
$('.st_selectable li.ui-selected').each(function(index, selected) {
st_selectedvalues.push(selected.innerHTML);
});
if(st_selectedvalues.length > 0){
$('.st_selectable').closest('.st_accordion').find('.button-reset').button('enable'); // Button is not enabling here
for (var i = 0; i < st_selectedvalues.length; i++) {
r_row = r_columnTable.getFilteredRows([{column: 1, value: st_selectedvalues[i]}])[0];
r_view.columns.push(r_columnTable.getValue(r_row, 0));
}
}
st_chart.setView(r_view);
st_chart.draw();
}
onload_st_filter_values = st_initState.selectedValues;
google.visualization.events.addListener(st_chart,'ready',function() {
var filt_values = onload_st_filter_values;
console.log("Inside ready");
if(click==0){
$.each(filt_values.filter(uniquelists), function(index, value) {
$('.st_selectable').append('<li>' + value + '</li>');
});
}
$('.st_accordion').accordion({
active: false,
create: function () {
$('.st_selectable').selectable({
filter: '*',
stop: filter_st_charts
});
},
collapsible: true,
heightStyle: 'content'
});
$('.button-reset').button();
$('.button-reset').button('disable');
$('.button-reset').on('click', clearFilter);
click++
})
function clearFilter(sender) {
console.log("Inside Clear Filter");
var accordion;
sender.preventDefault();
sender.stopPropagation();
accordion = $(sender.target).closest('.st_accordion');
accordion.find('.st_selectable li').removeClass('st_selectable .ui-selected');
accordion.accordion('option', 'active', false);
$(sender.target).closest('button').button('disable');
filter_st_charts();
return false;
}
</script>
我的 HTML 代码
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<?!= include('page_style'); ?>
</head>
<body>
<div class="st_accordion">
<h3>
<span>State </span>
<button class="button-reset ui-button ui-widget ui-corner-all ui-button-icon-only" title="Reset">
<span class="ui-icon ui-icon-close"></span>
</button>
</h3>
<div><ul class="st_selectable"></ul></div>
</div>
<div id="chart_st_pdiv" class="chart_container1"></div>
</body>
我的page_style CSS
.st_accordion > div.ui-accordion-content {
padding: 6px 6px 6px 6px;
}
.st_selectable {
list-style-type: none;
margin: 0;
padding: 0;
}
.st_selectable li {
background-color: #f6f6f6;
border: 1px solid #c5c5c5;
cursor: pointer;
font-size: 8pt;
margin-top: 2px;
padding: 6px 8px 6px 8px;
}
.st_selectable .ui-selecting {
background-color: #99ccff;
border: 1px solid #809fff;
}
.st_selectable .ui-selected {
background-color: #007fff;
border: 1px solid #003eff;
color: #ffffff;
}
.ui-button-icon-only {
float: right;
height: 18px;
margin: 0px;
min-width: 18px;
width: 18px;
z-index: 1;
}
.ui-widget {
font-size: 8pt;
}
在ready函数中,我正在创建一个按钮并禁用它,在函数filter_st_charts上尝试启用它但它不起作用。
我尝试在下面启用它
var btn = $('.st_selectable').closest('.st_accordion').find('.button-reset');
console.log(btn.length) -> returns 1
我试过这个
btn.prop('disabled',false) // not working
然后这个
btn.attr('disabled', false);
btn.removeAttr("disabled");
他们都不起作用,我想了解我哪里做错了。
您需要使用
button
小部件的 jQuery-UI 方法在禁用它后启用它:https://api.jqueryui.com/button/#method-enable
btn.button( "enable" );
如果绕过 jQuery-UI,它将无法正确管理按钮状态,这就是 jQuery-UI 的要点。