Google Geochart only appears after a refresh - ruby-on-rails

I am trying to integrate Google GeoChart to my rails application. In the view i simply copied-pasted the example from Google:
On top of the document i have the following:
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {
'packages': ['geochart'],
// Note: you will need to get a mapsApiKey for your project.
// See: https://developers.google.com/chart/interactive/docs/basic_load_libs#load-settings
'mapsApiKey': 'AIzaSyD-9tSrke72PouQMnMX-a7eZSW0jkFMBWY'
});
google.charts.setOnLoadCallback(drawRegionsMap);
function drawRegionsMap() {
var data = google.visualization.arrayToDataTable([
['Country', 'Popularity'],
['Germany', 200],
['United States', 300],
['Brazil', 400],
['Canada', 500],
['France', 600],
['RU', 700]
]);
var options = {};
var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));
chart.draw(data, options);
}
</script>
Then I add the div with the id:
<div id="regions_div" style="width: 100%; height: 500px;"></div>
But i get the following error:
VM17084:3 Uncaught TypeError: Cannot read property 'load' of undefined
at <anonymous>:3:21
at o.assignNewBody (turbolinks.self-569ee74eaa15c1e2019317ff770b8769b1ec033a0f572a485f64c82ddc8f989e.js?body=1:6)
at o.replaceBody (turbolinks.self-569ee74eaa15c1e2019317ff770b8769b1ec033a0f572a485f64c82ddc8f989e.js?body=1:6)
at turbolinks.self-569ee74eaa15c1e2019317ff770b8769b1ec033a0f572a485f64c82ddc8f989e.js?body=1:6
at o.e.renderView (turbolinks.self-569ee74eaa15c1e2019317ff770b8769b1ec033a0f572a485f64c82ddc8f989e.js?body=1:6)
at o.render (turbolinks.self-569ee74eaa15c1e2019317ff770b8769b1ec033a0f572a485f64c82ddc8f989e.js?body=1:6)
at Function.e.render (turbolinks.self-569ee74eaa15c1e2019317ff770b8769b1ec033a0f572a485f64c82ddc8f989e.js?body=1:6)
at t.renderSnapshot (turbolinks.self-569ee74eaa15c1e2019317ff770b8769b1ec033a0f572a485f64c82ddc8f989e.js?body=1:6)
at t.render (turbolinks.self-569ee74eaa15c1e2019317ff770b8769b1ec033a0f572a485f64c82ddc8f989e.js?body=1:6)
at r.render (turbolinks.self-569ee74eaa15c1e2019317ff770b8769b1ec033a0f572a485f64c82ddc8f989e.js?body=1:7)
The error disapears when I reload the page, which makes me thinks that it is related to Turbolinks.
Does anybody know how to fix this issue please ?

The page wasn't fully loaded when your javascript code started to run, google.charts was not available yet, hence the Cannot read property 'load' of undefined error.
Solution: Wrap your javascript code in a listener that fires when the page is loaded:
document.addEventListener("DOMContentLoaded", function(){
google.charts.load('current', {
// ...etc...
});
});

I ended up using the following:
setTimeout(function() {
google.charts.load('current', {
'packages': ['geochart'],
// Note: you will need to get a mapsApiKey for your project.
// See: https://developers.google.com/chart/interactive/docs/basic_load_libs#load-settings
'mapsApiKey': 'AIzaSyD-9tSrke72PouQMnMX-a7eZSW0jkFMBWY'
});
google.charts.setOnLoadCallback(drawRegionsMap);
function drawRegionsMap() {
var rubydata = JSON.parse('<%= #user_origin.to_json.html_safe -%>')
data = [["Country","Value"]].concat(rubydata)
var data = google.visualization.arrayToDataTable(data);
var options = {
legend: {position: 'none'},
colors: <%= raw #palette_final.to_json %>
};
var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));
chart.draw(data, options);
} }, 1000);

Related

How to retrieve data from firebase to Google chart?

Sorry but it's my first project with Firebase. I try this solution in my project but to no avail (from antoher post on Stack overflow). Data for this database are from ESP8266. My Firebase looks like this:
"MOIST" : {
"-MvDCD_qVJUZtZ9Pe98p" : {
"data" : "2022-2-6",
"time" : "09:44:40",
"value" : 5
},
"-MvDCE56y_4aXngGtQJx" : {
"data" : "2022-2-6",
"time" : "09:44:42",
"value" : 5
},
This is the code I am using:
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.7.8/firebase.js"></script>
<script src=" http://www.google.com/uds/modules/gviz/gviz-api.js"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
// Initialize Firebase
var config = {
apiKey: "AIzaSyD0y-idPsXKkqOI8333yQE9NTTvF5cDh0M",
authDomain: "podlewanie-2.firebaseapp.com",
databaseURL: "https://podlewanie-2-default-rtdb.europe-west1.firebasedatabase.app",
projectId: "podlewanie-2",
storageBucket: "podlewanie-2.appspot.com",
messagingSenderId: "266746962201",
appId: "1:266746962201:web:1a4c60a5824162c560a588",
measurementId: "G-7QW1Z2DD7W"
};
firebase.initializeApp(config);
var database = firebase.database();
firebase.database().ref('/MOIST').once('value').then(function(snapshot) {
temps = snapshot.val();
console.log(temps);
google.charts.load('current', {
'packages': ['corechart', 'line']
});
google.charts.setOnLoadCallback(drawChart(temps));
});
function parse(temp) {
return (new Date(temp.replace(/-/g, '/'))).getTime()
}
function drawChart(temps) {
var array = $.map(temps, function(value, index) {
return [value];
});
var data = new google.visualization.DataTable();
data.addColumn('number', 'date');
data.addColumn('number', 'time');
data.addColumn('number', 'value');
var sort = function(a, b) {
return parse(a.time) - parse(b.time)
};
var tempData = array.sort(sort);
var output = [];
for (var i = 0; i < tempData.length; i++) {
var item = tempData[i];
output.push([
// parseFloat(parse(item.time)),
parseFloat(item.time),
parseFloat(item.date),
parseFloat(item.value)
]);
}
console.log(output);
data.addRows(output);
var options = {
chart: {
title: 'title',
subtitle: 'subtitle'
},
width: 900,
height: 500
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
console.log("data + options: " + data, options)
chart.draw(data, options);
}
google.charts.load('current', {
'packages': ['corechart', 'line']
});
google.charts.setOnLoadCallback(drawChart);
</script>
</head>
<body>
<div id="curve_chart" style="width: 900px; height: 500px"></div>
</body>
</html>
After run this code i see only white page. I will be grateful for your help.
When you see a white screen, check your browser's console (Ctrl+Shift+J/⌘+⌥+J in most browsers, or F12 in LegacyEdge/IE) for errors. In this case, you get this error:
Uncaught Error: FIREBASE FATAL ERROR: Cannot parse Firebase url. Please use https://<YOUR FIREBASE>.firebaseio.com
This indicates that the version of the Firebase SDK you are using doesn't understand the value you gave for databaseURL in your configuration:
var config = {
/* ... */
databaseURL: "https://podlewanie-2-default-rtdb.europe-west1.firebasedatabase.app"
/* ... */
}
Because this was probably copied straight from the Firebase Console, ask yourself why doesn't the SDK understand this URL? The answer to that is because you are using Firebase SDK v3.7.8 (from April 2017).
<script src="https://www.gstatic.com/firebasejs/3.7.8/firebase.js"></script>
You should be using the latest version to make sure you are up-to-date on any security fixes, corrected flaws, updated server APIs or new product updates.
The latest legacy SDK (as of the time of writing) is 8.10.1 (January 2022):
<script src="https://www.gstatic.com/firebasejs/8.10.1/firebase.js"></script>
However, for all new projects you should use the new modular SDK by following the upgrade guide. The latest version for that SDK is 9.6.6 (February 2022).
Other issues
Aside from that, you also need to correct this line, that should be setting a callback function:
google.charts.setOnLoadCallback(drawChart(temps));
to the following:
google.charts.setOnLoadCallback(() => drawChart(temps));
Additionally, in that drawChart function, you are also targetting an element with the ID chart_div instead of the correct curve_chart on this line:
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
That line should be:
var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

Offline exporting a Highcharts chart using an external button

In this Highcharts chart the objective is to offline export using a button external to the chart.
The problem that I have is that even thought I added the offline-exporting.js file to my application, if I'm not connected to the Internet when I click the Offline Export button I get an error saying it cannot access the URL export.highcharts.com.
How to fix this error and export offline?
HTML
<button id="exp" >Offline Export</button>
<div id="container" style="height: 400px; width: 500px"></div>
Javascript:
var settings =
{
"chart": {
"type":"line"
},
"xAxis": {
"endOnTick":true
},
"series":[
{"name":"series1","data":[[1,1200],[2,2200],[3,3200],[4,1800],[5,1500]]},
{"name":"series2","data":[[1,1050],[2,2050],[3,1650],[4,1450],[5,1350]]},
{"name":"series3","data":[[1,1250],[2,2250],[3,1850],[4,1650],[5,1550]]}]
}
var chart = $('#container').highcharts(settings);
$( "#exp" ).click(function() {
alert( "Handler for .click() called." );
var chart = $('#container').highcharts();
chart.exportChart({
type: 'image/png',
filename: 'theimage'
});
});
Try to using exportChartLocal()
$( "#exp" ).click(function() {
alert( "Handler for .click() called." );
var chart = $('#container').highcharts();
chart.exportChartLocal({
type: 'image/png',
filename: 'theimage'
});
});

NumberRangeFilter giving "One or more participants failed to draw() error"

Using Visual Studio 2015 MVC Core 1.1
_Layout.cshtml
Load Javascript APIs loads for the site.
Here I call google.charts.load('current', { 'packages': ['corechart', 'table', 'gauge', 'controls'] });
Environment Variables set to "Development" in the project properties.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- Javascript -->
<environment names="Development">
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.js"></script>
<script src="~/lib/jquery-ui/jquery-ui.js"></script>
<script src="~/lib/clipboard/dist/clipboard.js"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
#*<script type="text/javascript" src="https://www.google.com/jsapi?ext.js"></script>*#
<script type="text/javascript">
google.charts.load('current', { 'packages': ['corechart', 'table', 'gauge', 'controls'] });
</script>
<script src="~/lib/highcharts/highcharts.js"></script>
<script src="~/js/site.js"></script> #*Production bundles to site.min.js - always last*#
<script src="~/js/Script_CodeStudio.js"></script> #*Production bundles to site.min.js - always last*#
<script src="~/js/Script_Gcharts.js"></script> #*Production bundles to site.min.js - always last*#
</environment>
<!-- Javascript Page Specific Header-->
#RenderSection("scripts_head", required: false)
</head>
<body>
<!-- CONTENT -->
#RenderBody()
<!-- Javascript Page Specific Body-->
#RenderSection("scripts", required: false)
Index.cshtml
Contains <div> id's needed to render dashboard
Contains callback google.charts.setOnLoadCallback();
<div id="div_dashboard_Detail">
<div id="div_dashboard_Detail_categoryPicker1"></div><br />
<div id="div_dashboard_Detail_categoryPicker2"></div><br />
<div id="div_dashboard_Detail_chart"></div>
</div>
#section scripts_head{
<script>
google.charts.setOnLoadCallback(gChart0);
function gChart0() {
drawChart_LogsSelectingEvents_MissedDelivery_test();
};
</script>
}
Google Dashboard function:
I'm trying to load a NumberRangeFilter called categoryPicker2 but get the error One or more participants failed to draw()
If I remove categoryPicker2 and leave categoryPicker1, the table renders just fine.
x
function drawChart_LogsSelectingEvents_MissedDelivery_test() {
var urlString = '../Logs/clnLogsSelectingEvents?grade=All&SC=1&CauseSC=3';
$.ajax({
type: 'GET',
dataType: 'json',
contentType: "application/json",
//url: urlString, //I have commented out but this is my original source.
success: function (result) {
//Create DataTable
var data = new google.visualization.DataTable();
//Add Columns
data.addColumn('string', 'Review on Week');
data.addColumn('string', 'Business Division');
data.addColumn('string', 'Product');
data.addColumn('string', 'Cause');
data.addColumn('string', 'Next Step'); //inserted
data.addColumn('string', 'Carrier Reference or Responsible');
data.addColumn('number', 'Cost');
data.addColumn('number', 'Quantity');
data.addColumn('number', 'Age (d)');
data.addColumn('string', 'Delivery Number');
data.addColumn('string', 'Material Description');
data.addColumn('string', '');
data.addColumn('string', 'Actual State');
//Add Rows
var dataArray = [];
for (i = 0; i < 10; i++) {
//manually push data into table
dataArray.push([
'string1',
'string2' + i,
'string3',
'string4',
'string5',
'string6',
10,
20,
5 + i,
'string7',
'string8',
'string9',
'string10'
]);
}
data.addRows(dataArray);
//Options
var options = {
width: '100%',
page: 'enable',
pageSize: '3'
};
//Create Data View
var view = new google.visualization.DataView(data);
view.setColumns([12, 0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11]);
// Create a dashboard.
var dashboard = new google.visualization.Dashboard(
document.getElementById('div_dashboard_Detail'));
var categoryPicker1 = new google.visualization.ControlWrapper({
'controlType': 'CategoryFilter',
'containerId': 'div_dashboard_Detail_categoryPicker1',
'options': {
'filterColumnIndex': 2,
'ui': {
'label': 'Business Division:'
}
}
});
//THIS IS MY PROBLEM - SLIDER WILL NOT RENDER
var categoryPicker2 = new google.visualization.ControlWrapper({
'controlType': 'NumberRangeFilter',
'containerId': 'div_dashboard_Detail_categoryPicker2',
'options': {
'filterColumnIndex': 8 //Age (d)
},
'state': { 'lowValue': 0, 'highValue': 7 }
});
var chart = new google.visualization.ChartWrapper({
'chartType': 'Table',
'containerId': 'div_dashboard_Detail_chart',
'options': {
showRowNumber: false,
width: '100%',
height: 'auto',
page: 'enable',
pageSize: '15',
sort: 'enable',
allowHtml: true
}
});
//Object Binding
dashboard.bind([categoryPicker1, categoryPicker2], [chart]);
// Draw the dashboard.
dashboard.draw(view);
} //END success: function (result) {
}); //END $.ajax({
} //END function drawChart()
What could I be missing? I can get it to work in JSFiddle but once I put all the pieces back together in MVC it's not rendering.
https://jsfiddle.net/hken6xco/35/
Thanks for any assistance!!
the dashboard appears to draw fine here, even without data,
no error is received, see following working snippet...
google.charts.load('current', {
callback: drawChart,
packages:['corechart', 'table', 'gauge', 'controls']
});
function drawChart() {
//Create DataTable
var data = new google.visualization.DataTable();
//Add Columns
data.addColumn('string', 'Review on Week');
data.addColumn('string', 'Business Division');
data.addColumn('string', 'Product');
data.addColumn('string', 'Cause');
data.addColumn('string', 'Next Step');
data.addColumn('string', 'Carrier Reference or Responsible');
data.addColumn('number', 'Cost');
data.addColumn('number', 'Quantity');
data.addColumn('number', 'Age (d)');
data.addColumn('string', 'Delivery Number');
data.addColumn('string', 'Material Description');
data.addColumn('string', '');
data.addColumn('string', 'Actual State');
//Create Data View
var view = new google.visualization.DataView(data);
view.setColumns([12, 0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11]);
// Create a dashboard.
var dashboard = new google.visualization.Dashboard(
document.getElementById('div_dashboard_Detail'));
var categoryPicker1 = new google.visualization.ControlWrapper({
'controlType': 'NumberRangeFilter',
'containerId': 'div_dashboard_Detail_categoryPicker1',
'options': {
'filterColumnIndex': 8
},
});
var categoryPicker2 = new google.visualization.ControlWrapper({
'controlType': 'CategoryFilter',
'containerId': 'div_dashboard_Detail_categoryPicker2',
'options': {
'filterColumnIndex': 0, //Column used in control
'ui': {'label': 'Actual State:'}
}
});
var chart = new google.visualization.ChartWrapper({
'chartType': 'Table',
'containerId': 'div_dashboard_Detail_chart',
'options': {
showRowNumber: false,
width: '100%',
height: 'auto',
page: 'enable',
pageSize: '15',
sort: 'enable',
allowHtml: true
}
});
//Object Binding
dashboard.bind([categoryPicker1, categoryPicker2], [chart]);
// Draw the dashboard.
dashboard.draw(view);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="div_dashboard_Detail">
<div id="div_dashboard_Detail_categoryPicker1"></div>
<div id="div_dashboard_Detail_categoryPicker2"></div>
<div id="div_dashboard_Detail_chart"></div>
</div>
I"m not sure what was going on. #WhiteHat Thank you for the example. My function initially was very complex so I separated it into a simpler version containing only the dashboard. The only thing I found was that one variable was not defined in my more complex version. It started working but I don't really know why. Perhaps it was a compile error in Visual Studio because I really didn't change any of the code.
So conclusion: the code above works and the fiddle as well.
Thanks for the help as always!!

Google Linechart - No Pointsize, Tooltips?

I'm using a Linechart from Google, in which I have drawn a graph from JSON data. There are two problems I'm running into I can't seem to fix.
Even though I have 'pointSize: 6' in my options, I still can't seem to draw any kind of point, anywhere on the graph. They just don't get visible.
I can't seem to edit any tooltip. I've added a new column, and neither "dataTable.setValue(i, 2, 'test')" nor manually adding a new entry in the JSON file with "Tooltip":"Test" seems to work.
Anyone who knows what I'm doing wrong, or who has a better suggestion for perhaps a framework/api to use? I'm trying to visualise a datastory with simple code.
<html>
<head>
<style>
body{
/*overflow: hidden;*/
}
#linechart{
margin-top: 20px;
margin-left: 30px}
</style>
<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://dimplejs.org/dist/dimple.v2.1.2.min.js"></script>
<script src='js/dimple.v2.1.2.js'></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1.1', {packages: ['line']});
google.setOnLoadCallback(drawChart);
function drawChart() {
$(function() {
$.getJSON('data/priceData.json', function(data) {
var dataTable = new google.visualization.DataTable();
dataTable.addRows(1800);
dataTable.addColumn('string', 'Date');
dataTable.addColumn('number', 'Value');
//dataTable.addColumn({type: 'string', role: 'tooltip'});
dataTable.addColumn({ type: 'string', role: 'tooltip', 'p': { 'html': true} });
$.each(data, function(i, object){
dataTable.setValue(i, 0, object.DateY);
dataTable.setValue(i, 1, object.ClosePrice);
dataTable.setValue(i, 2, object.Tooltip);
//dataTable.setValue(i, 2, 'yo');
});
var options = {
colors: ['orange'],
tooltip: {isHtml: true},
chart: {
title: 'The Value of the Bitcoin',
subtitle: 'in dollars (USD)'
},
animation: {
duration: 1000,
easing: 'in',
startup: true
},
width: 1950,
height: 850,
pointSize: 6
};
var chart = new google.charts.Line(document.getElementById('linechart'));
chart.draw(dataTable, options);
});
});
};
</script>
</head>
<body>
<div id="linechart"></div>
</body>
</html>
I got this to work using google.visualization.LineChart instead of google.charts.Line.
It gives me both points and tooltips, see here:
Instead of using
google.load('visualization', '1.1', {packages: ['line']});
Just try including
<script type="text/javascript"
src="https://www.google.com/jsapi?autoload={
'modules':[{
'name':'visualization',
'version':'1',
'packages':['corechart']
}]
}"></script>
Then instead of:
var chart = new google.charts.Line(document.getElementById('linechart'));
Just try using:
var chart = new google.visualization.LineChart(document.getElementById('linechart'));
That ought to do it. Let me know if you have other problems.

CKEditor 4 and jQuery UI sortable removes content after sorting

I've ran into an issue with CKEditor 4 and jQuery UI's sortable method where if I sort a container that has a CKEditor instance, it removes the value and throws an error "Uncaught TypeError: Cannot call method 'getSelection' of undefined". It also makes the editor uneditable. I was able to get around this in CKEditor 3 with one of the following hacks found here:
CKEditor freezes on jQuery UI Reorder
In looking at the Chrome DOM inspector, it appears that the contents of the iframe are removed.
Below is crude test code:
<html>
<head>
<title>test</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.24/jquery-ui.min.js"></script>
<script src="ckeditor.js"></script>
<script type="text/javascript">
$(function(){
var tmpStore = {};
$('#sortable').sortable({
cursor: 'move',
// Hack that use to work on V3 but not on V4:
// https://stackoverflow.com/questions/3379653/ckeditor-freezes-on-jquery-ui-reorder
start:function (event,ui) {
$('textarea').each(function(){
var id = $(this).attr('id');
tmpStore[id] = CKEDITOR.instances[id].getData();
})
},
stop: function(event, ui) {
$('textarea').each(function(){
var id = $(this).attr('id');
CKEDITOR.instances[id].setData(tmpStore[id]);
})
}
});
$('textarea').each(function(){
var ckId = $(this).attr('id');
config = {};
CKEDITOR.replace(ckId, config);
})
})
li { padding: 10px; width: 800px; height: 300px; }
</head>
<body>
<ul id="sortable">
<li><textarea id="test1" name="test1">test1</textarea></li>
<li><textarea id="test2" name="test1">test2</textarea></li>
<li><textarea id="test3" name="test1">test3</textarea></li>
</ul>
</body>
</html>
I was facing the same problem and have fixed based on answers here. Please see fiddles below
ISSUE:
https://jsfiddle.net/33qt24L9/1/
$(function() {
$( "#sortable" ).sortable({
placeholder: "ui-state-highlight"
});
CKEDITOR.replace( 'editor1' );
CKEDITOR.replace( 'editor2' );
CKEDITOR.replace( 'editor3' );
CKEDITOR.replace( 'editor4' );
});
RESOLVED ISSUE: https://jsfiddle.net/57djq2bh/2/
$(function() {
$( "#sortable" ).sortable({
placeholder: "ui-state-highlight",
start: function (event, ui)
{
var id_textarea = ui.item.find(".ckeditor").attr("id");
CKEDITOR.instances[id_textarea].destroy();
},
stop: function (event, ui)
{
var id_textarea = ui.item.find(".ckeditor").attr("id");
CKEDITOR.replace(id_textarea);
}
});
CKEDITOR.replace( 'editor1' );
CKEDITOR.replace( 'editor2' );
CKEDITOR.replace( 'editor3' );
CKEDITOR.replace( 'editor4' );
});
EDIT: If like me you had seperate configs per editor here's updated code that will help:
start: function (event, ui)
{
$('.wysiwyg', ui.item).each(function(){
var tagId = $(this).attr('id');
var ckeClone = $(this).next('.cke').clone().addClass('cloned');
ckeConfigs[tagId] = CKEDITOR.instances[tagId].config;
CKEDITOR.instances[tagId].destroy();
$(this).hide().after(ckeClone);
});
},
stop: function(event, ui) {
// for each textarea init ckeditor anew and remove the clone
$('.wysiwyg', ui.item).each(function(){
var tagId = $(this).attr('id');
CKEDITOR.replace(tagId, ckeConfigs[tagId]);
$(this).next('.cloned').remove();
});
}
Thanks to: https://github.com/trsteel88/TrsteelCkeditorBundle/issues/53
You have to re-create CKEditor once underlying DOM structure is modified. Save editor data with editor.getData() before editor.destroy() and restore contents with editor.setData( data ) once you create a new instance. There's no other way to fix this since CKEditor strongly depends on the DOM structure.
Remove CKEditor start Sortable
var ckeConfigs = [];
$('.ui-sortable').sortable({
start:function (event,ui) {
$('.lineItemCommentBox', ui.item).each(function(){
var tagId = $(this).attr('id');
ckeConfigs[tagId] = CKEDITOR.instances[tagId].config;
CKEDITOR.instances[tagId].destroy();
});
},
stop: function(event, ui) {
$('.lineItemCommentBox', ui.item).each(function(){
var tagId = $(this).attr('id');
CKEDITOR.replace(tagId, ckeConfigs[tagId]);
});
}
});
The code below works for me, we have to destroy the editor on start and recreate it when the drag is ended getting the value of the textarea which come the data from :
jQuery(function($)
{
var panelList = $("#nameofyourdiv");
panelList.sortable(
{
handle: ".classofyourdivforsorting",
start: function (event, ui)
{
var id_textarea = ui.item.find("textarea").attr("id");
CKEDITOR.instances[id_textarea].destroy();
}
stop: function (event, ui)
{
var id_textarea = ui.item.find("textarea").attr("id");
CKEDITOR.replace(id_textarea);
}
});
});
Hope it helps someone.
i ve solved this kind of problem by instantiating the CKEditor after having opened the jquery dialog
I had the Similar issue with CKEDITOR , The Code Below worked for me. Destroy the Ckeditor instance and Remove the Ckeditor and when the dragging ends replace the current textarea with Ckeditor again
$("#sortable").sortable({
items: '.dynamic',
start: function (event , ui) {
var editorId = $(ui.item).find('.ckeditor').attr('id');// get the id of your Ckeditor
editorInstance = CKEDITOR.instances[editorId]; // Get the Ckeditor Instance
editorInstance.destroy(); // Destroy it
CKEDITOR.remove(editorId);// Remove it
},
stop: function(event, ui) {
var editorId = $(ui.item).find('.ckeditor').attr('id');// Get the Id of your Ckeditor
CKEDITOR.replace(editorId);// Replace it
}
}
});
$("#sortable").disableSelection();
Here #sortable is the id of the DIV which is sortable and '.dynamic' is the Class assigned to DIV that are eligible to sort and '.ckeditor' is the class for the Textarea .
I got my solution from Here , Hope this helps for someone in future.
I simply use the ckeditorOff() and ckeditorOn() functions to keep data and re/de-instance ckeditor instances during movement.
$('#sortable').sortable({
cursor: 'move',
start:function (event,ui) {
if(typeof ckeditorOff=='function')ckeditorOff();
},
stop: function(event, ui) {
if(typeof ckeditorOn=='function')ckeditorOn();
}
});
I put the typeof ckeditorOff statement to make the code compatible with future versions of ckeditor in case they decide to remove these two functions.

Resources