Create new rows on initialization ?
Create new rows on initialization ?
At the start of a new datatable/editor instance I would like to preload some new records but I'm having problems finding the right method and event to do this with? On initial page load I have a small set of key field records I need to pre-populate the form with (and db backend) and I have it working okay in a manual way at the moment. I can make it work by hanging the action onto a button and iterating through the list one click at a time. I've tried hanging it off both table and editor callbacks ( initComplete:, etc.), for loops and various on.events with no luck other than getting the first record inserted. The following section (load button) from the table init works fine when I manually index through it but the (commented) for loop only gets the 1st record?
Missing something fundamental here with either timing or method (or both)? Also, would field().multiset() be better than using using field().def() here? I only have about 5-10 records to insert so I'm not to worried about performance and its pretty much a tossup between entering multiple rows vs. columns?
Haven't studied dependent() enough yet but I'm guessing there is a more elegant solution possible here also?
select: true,
initComplete: function(settings, json) {
headids = json.options.hid;
},
pageLength: 10,
responsive: false,
buttons: [
{ extend: 'create', editor: editor },
{ extend: 'edit', editor: editor },
{ extend: 'remove', editor: editor },
{ text: 'Load',
action: function ( e, dt, node, config ) {
// for (var i = 0; i < headids.length; i++) {
if(lidx < headids.length) {
editor.field('hid').def(headids[lidx].value);
editor.field('heading').def(headids[lidx].label);
console.log(lidx+" : "+editor.field('hid').def()+" : "+editor.field('heading').def());
editor
.create(false)
.submit();
lidx++;
}
}
}
]
I've also tried putting the same function in both the table and editor callbacks, table initComplete: will do the same 1st record but the editor initComplete: never fires? Also tried a separate editor event handler and it responds the same (for an event other than initComplete to get it started) .
editor.on( 'initComplete', function ( e, json, data ) {
console.log( 'Editor Init Complete' );
for (var i = 0; i < headids.length; i++) {
editor.field('hid').def(headids[i].value);
editor.field('heading').def(headids[i].label);
console.log(editor.field('hid').def() + " : " + editor.field('heading').def());
editor
.create(false)
.submit();
}
} );
This question has an accepted answers - jump to answer
Answers
Could you explain a little more about your end goal here before we get into the details of how to implement it, as I'm not quite clear what that is at the moment.
Is there a server-side database for this data? If so, do you really want to create 'x' new rows every time Editor is initialised on the client-side? A simple page reload would trigger those
x
new rows being creating again. Is that really the goal?Or do you just want to preload the database with
x
rows, in which case an SQL INSERT would probably be the way to do it immediately after you've runCREATE TABLE
.Allan
What I have is a master table and numerous child tables. The master table has all the high level when, where, what activity info. The child tables represent additional details that need to be collected for each activity present in the corresponding master table record. Every day and every shift at multiple locations, this cycle repeats itself. Everything is referenced together via the master table record key id. I'm using the master editor table to launch separate sub-editor screens for each child activity table (or use a standalone editor if its simple enough). What I start out with is a handful of master table record id's that I need to populate a number of child table records around. Since the child table doesn't have any of those id references yet I cant use any of the left join methods to do the initial load. I am using your handy little options array to pull data I need along for the ride although. What I end up with on the first fetch is an empty data[] array but I have a full options[] array of just what I need to create my seed rows for further data entry. Once I have something in the editor table I can go to town making the rest of it easy and no human hands have touched the key references to mess them up. I'm sure there is a more elegant or rigorous way to do this but this does work quite well just using stock datatables features (and very little coding! :). All I'm trying to do now is automate as much as I can.
Setup is a standard server-side database but all the processing is client-side other than the stock editor php libraries feeding the data in and out. The problem of overpopulating the table each time its loaded I can logic around a bit or simply chuck the unused ones out. For each distinct seed row I'll need multiple copies anyway (data entry can only duplicate an existing row not create new) this keeps them from creating references to nowhere.
I did look a little bit into doing it direct with a simple php/sql script, either with your Datatable/database functions or rolling my own but part of my goal is to get away from the many dozens of specialized scripts I have now and adopt some more standardized methods which you've very nicely provided.
Sorry for the long winded explanation. Do you have any good examples of some direct sql execution using the datatables libraries? My google-fu was coming up a bit short on that. My last resort is to dig through my old php code base and mod something there but what fun is that!
After a bit more study it does look very clean and simple just to use the $db->sql functionality in the php script for what I'm trying to do. I can easily do the "if already exists don't create new record" stuff on the SQL side.
Still curious how to properly manage the event handling of what I was originally trying?
Thanks Allan, you sent me in the right direction.
Good to hear you have it working.
With regard to the original method there are two issues I can see:
1) The use of
initComplete
- this is actually of somewhat limited use this event. It is only useful if you assign events in the Editor constructor, which is not something that is documented any more (it was in Editor 1.0-1.3). The Editor constructor is always synchronous at the moment, so once thenew ...()
code has run theinitComplete
event will already have run!2). The
for
loop isn't appropriate in the above since thesubmit()
method is asynchronous - you would need to wait for it to complete before then executing the next part of the loop - otherwise things get really confused!Allan