I am trying to modify this jQuery plug in:
So that I can dynamically add events to the timeline, using input fields.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>jQuery Timespace Plugin Examples</title>
<link href="css/jquery.timespace.dark.css" rel="stylesheet">
<style>
body {background:#262727;}
a {color: #8baac0;}
</style>
</head>
<body text="white">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="jquery.timespace.js"></script>
<br>
<label for="Estart">Start Time</label>
<input type="text" id="Estart" name="Estart" value="26">
<br><br>
<label for="Eend">End Time</label>
<input type="text" id="Eend" name="Eend" value="30">
<br><br>
<label for="Etitle">Title</label>
<input type="text" id="Etitle" name="Etitle" value="Event 4">
<br><br>
<label for="Edescription">Description</label>
<input type="text" id="Edescription" name="Edescription" value="New Event number 4">
<br><br>
<button id = "addNew">Add New Event</button>
<br><br>
<div id="timeline"> </div>
<script>
$(function () {
$('#timeline').timespace({
timeType: 'date',
useTimeSuffix: false,
startTime: 0,
endTime: 1900,
markerIncrement: 1, // the amount of time between each marker
selectedEvent: 0, // selected event 0 for first event, -1 to disable
markerAmount: 0, // the amount of time markers to use 0 to auto calculate
maxWidth: 1000, // max width in pixels
maxHeight: 280, // max height in pixels
markerWidth: 72, // width of marker
shiftOnEventSelect: true, // if the time table should shift when an event is selected
navigateAmount: 200, // the amount of pixels to move the Timespace
scrollToDisplayBox: true, // scroll to the event display box on event selection
customEventDisplay: null, // jQuery object to use for the event display box
controlText: {
navLeft: 'Move Left',
navRight: 'Move Right',
drag: 'Drag',
eventLeft: 'Previous Event',
eventRight: 'Next Event',
},
data: {
headings: [{start: 0, end: 1900, title: 'Event Time Line'},],
events: [
{start: 4.3, end:20.5 , title: 'Event 1', description: 'Event number 1 text.'},
{start: 19, end: 25, title: 'Event 2', description: 'Event number 2 text.'},
{start: 21, end: 24, title: 'Event 3', description: 'Event number 3 text.'},
],
},
},
function () {
// Set the navigation Seed
//this.navigateAmount = 100;
});
});
</script>
</body>
</html>
The only way I could get it to work was to add this the jQuery js script:
$("#addNew").on('click', () =>{
let newItem = {
start: $("#Estart").val(), end: $("#Eend").val(), title: ("#Etitle").val(),
description: $("#Edescription").val()
};
this.data.events.push(newItem);
this.buildTimeEvents();
this.buildTimeDisplay();
this.updateStaticData();
this.updateDynamicData();
this.setDOMEvents();
alert("added");
}),
This adds the new event fine, but it also duplicates the events that are already there, so I end up with 2 of every event, except for the new event added.
I need a way to only add the new event, but I can’t figure it out.
I also want to be able to save the events array (ex: saved events list 1, saved events list 2, and so on), then be able to load any saved events from previously saved arrays.
Any help or suggestions would be greatly appreciated.
Thanks.