Forum > Packages and Libraries
[solved] tvPlanit - how to create an event by code? - Troubelshooting
(1/1)
Nicole:
I took the code below out of the example which says "add a random date to the monthview".
I tried to do the same trick by using just an ordinary button.
My problem is the add line.
Called from the Month View it probalby "knows" something.
Called from a button out of th blue, it does not.
What I need is something like:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---"mydatastore.add(newEvent);"
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);const cat = 0;var t1, t2: TDateTime; txt: String;begin // Get random start time of the event on the selected day in the weekview t1 := trunc(VpWeekView1.Date) + EncodeTime(random(23), Random(60), 0, 0); // Get random end time of the event, event duration less than 1 hour. t2 := t1 + EncodeTime(0, Random(60), 0, 0); // Get event caption txt := 'Event' + IntToStr(VpIniDatastore1.Resource.Schedule.EventCount); // Add the event======> AddEvent(t1, t2, txt, cat);end;
wp:
You must do the "AddEvent" in several steps (like in the TForm1.AddEvent method of the sample) - I know it's a bit counter-intuitive (it's not my code...):
* Call the AddEvent method of the resource's Schedule (which is a list of all events). This creates a blank new event
* Set the properties of this new event
* Mark the event as being "changed". Otherwise it will not be posted to the database in the next step.
* Finally post the event to the database working in the background: Datastore.PostEvents
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.AddEvent(AStartTime, AEndTime: TDateTime; ACaption: String; ACategory: Integer);var event: TVpEvent; id: Integer;begin // Get the ID of the new event from the datastore id := VpIniDataStore1.GetNextID('Events'); // Add the event... event := VpIniDatastore1.Resource.Schedule.AddEvent(id, AStartTime, AEndTime); // ... and specify some properties event.Description := ACaption; event.Category := ACategory; event.Changed := true; // Save the event in the datastore VpIniDatastore1.PostEvents;end
Nicole:
I am afraid, I failed.
I tried this:
I called the method by this attempts:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TvPlanItFrame.neuesEvent(now,now + 2,'loesch',0);
or this
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TvPlanItFrame.neuesEvent(now,now,'test',1);
the result is this:
[Window Title]
Fehler
[Content]
Projekt project_Notizen hat Exception-Klasse »External: SIGSEGV« ausgelöst.
In Datei 'source\vpdata.pas' in Zeile 1088:
if FSchedule = nil then
[Ok]
what was called is this method
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TTvPlanItFrame.neuesEvent(AStartTime, AEndTime: TDateTime; ACaption: String; ACategory: Integer);var event: TVpEvent; id: Integer;begin // Get the ID of the new event from the datastore id := VpIniDataStore_Quelle.GetNextID('Events');// id := self.VpIniDatastore_myLinkDazu.get.GetNextID('Events'); // Add the event... event := VpIniDatastore_Quelle.Resource.Schedule.AddEvent(id, AStartTime, AEndTime); // ... and specify some properties event.Description := ACaption; event.Category := ACategory; event.Changed := true; // Save the event in the datastore VpIniDatastore_Quelle.PostEvents;end;
the problem is said to be here
vpdate, line 1088
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TVpResource.GetSchedule: TVpSchedule;begin if FSchedule = nil then FSchedule := TVpSchedule.Create(self); result := FSchedule;end;
wp:
What is VpIniDatastore_Quelle.Resource? When your application starts it does not automatically select an event. My sample codes contain a few lines to do this:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// Load the last resource.procedure TForm1.FormCreate(Sender: TObject);var lastRes: TVpResource; datastore: TVpCustomDatastore;begin datastore := VpControlLink1.Datastore; if datastore.Resources.Count > 0 then begin lastRes := datastore.Resources.Items[datastore.Resources.Count-1]; datastore.Resource := lastRes; end;end;
Nicole:
Thank you so much, it worked at sudden.
I did not change anything in my code. I just created a new ressource and tried my "add" button again: worked at sudden.
What I like most "now - now"
"TvPlanItFrame.neuesEvent(now,now,'test',1); "
If somebody does not know the background, he will not understand my joy about "now-now" giving reasonable results. The first now is the start, the second now is the end of the event. If this is accepted, this means, I have not to mess around in "creating" an artificial end-date, in the case, I just have a start date, what is the rule, not the exception.
Now I am going to implement the smarter solution
" if datastore.Resources.Count > 0 then...."
Navigation
[0] Message Index