動的に生成<div>
していますが、生成時に jQuery 関数を適用する必要がありますが、機能していません。メソッドを試し.on
ましたが、思い通りに動作しません。<div>
関数を適用したい はclaas= portlet-content and id= live_graph
、親がの<div>
クラスですportlet ui-widget ui-widget-content ui-helper-clearfix ui-corner-all'>
どこが間違っているのですか?
動的に生成された<div>'s
HTMLstr += "<div class='portlet ui-widget ui-widget-content ui-helper-clearfix ui-corner-all'>"+ \
"<div class='portlet-header ui-widget-header ui-corner-all'><span class='ui-icon ui-icon-minusthick'>"+\
"</span>Live Graph</div>" + \
"<div class='portlet-content' id='live_graph' style='height: 270px margin: 0 auto'>" + \
// Want to call the function when the above <div> is generated
"</div></div>"
return HttpResponse(HTMLstr)
jQuery関数(ライブチャート生成)
$(function () {
$(document).ready(function() {
Highcharts.setOptions({
global: {
useUTC: false
}
});
var chart;
$('#live_graph').highcharts({
chart: {
type: 'spline',
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
load: function() {
// set up the updating of the chart each second
var series = this.series[0];
setInterval(function() {
var x = (new Date()).getTime(), // current time
y = Math.random();
series.addPoint([x, y], true, true);
}, 1000);
}
}
},
title: {
text: 'Live random data'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150
},
yAxis: {
title: {
text: 'Value'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+
Highcharts.numberFormat(this.y, 2);
}
},
legend: {
enabled: false
},
exporting: {
enabled: false
},
series: [{
name: 'Random data',
data: (function() {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -19; i <= 0; i++) {
data.push({
x: time + i * 1000,
y: Math.random()
});
}
return data;
})()
}]
});
});
});
Ajax コード
$.ajax({
type: "POST",
url: "/dashboard/",
data : { 'widgets_list' : widgets_list },
success: function(result){
console.log(widgets_list);
$("#column1").append(result); // div gets appended.
}
});
これは私が試したものです:-
それ以外の
$(function () { in the jQuery function
私はそれを次のように置き換えました:-
$('.portlet').on('ready', '#live_graph',function() {
しかし、うまくいきませんでした。どこが間違っているのでしょうか?
Question2 .on( events [, selector ] [, data ], handler(eventObject) )
の構文on
ready
イベントできる?
これは私が試したものです
AJAX 成功関数
success: function(result){
console.log(widgets_list);
$("#column1").append(result);
$(".column").sortable("refresh");
$("#column1").generate_live_graph();
}
jQuery関数
function generate_live_graph(){
$(function () {
$(document).ready(function() {
Highcharts.setOptions({
global: {
useUTC: false
}
});
var chart;
$('#live_graph').highcharts({
chart: {
type: 'spline',
animation: Highcharts.svg, // don't animate in old IE
marginRight: 10,
events: {
load: function() {
// set up the updating of the chart each second
var series = this.series[0];
setInterval(function() {
var x = (new Date()).getTime(), // current time
y = Math.random();
series.addPoint([x, y], true, true);
}, 1000);
}
}
},
title: {
text: 'Live random data'
},
xAxis: {
type: 'datetime',
tickPixelInterval: 150
},
yAxis: {
title: {
text: 'Value'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+
Highcharts.numberFormat(this.y, 2);
}
},
legend: {
enabled: false
},
exporting: {
enabled: false
},
series: [{
name: 'Random data',
data: (function() {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i;
for (i = -19; i <= 0; i++) {
data.push({
x: time + i * 1000,
y: Math.random()
});
}
return data;
})()
}]
});
});
});
}