Use some current datatable fields as defaults when creating new rows
Use some current datatable fields as defaults when creating new rows
My datatable/editor returns several columns of data such as:
project id, bldgid, eqid, subtype, etc.
I am sorting by eqid in the model file. The project id, bldgid, and eqid are the same for all rows. The subtype, etc. change per row.
I want to Create a new row of data in this table where project id, bldgid, eqid are the same and subtype, etc. are new values.
How can I pull the data of these three fields out of the json data and insert them in the create button?
Thanks,
Here is my view page so far:
var editor2 = new $.fn.dataTable.Editor( {
ajax: '/index.php/Ajax/subUnit',
table: '#subUnit',
fields: [
{
"label": "dataname:",
"name": "dataname"
},
{
"label": "design:",
"name": "design"
},
{
"label": "actual:",
"name": "actual"
}
]
} );
// Activate an inline edit on click of a table cell
$('#subUnit').on( 'click', 'tbody td:not(:first-child)', function (e) {
editor.inline( this );
} );
var table = $('#subUnit').DataTable( {
dom: 'Bfrtip',
ajax: {
url: '/index.php/Ajax/subUnitOfEquipment',
type: "POST",
data: {"eqid" : myID}
},
order: [[4, 'asc'], [5, 'asc'], [6, 'asc']],
rowGroup: {
dataSrc: ["subtype", "subname", "section"]
},
columns: [
{
"data": "projectid",
"visible": false
},
{
"data": "bldgid",
"visible": false
},
{
"data": "eqid",
"visible": false
},
{
"data": "subtype",
"visible": false
},
{
"data": "subname",
"visible": false
},
{
"data": "section",
"visible": false
},
{
"data": "line_order",
"visible": false
},
{
"data": "dataname"
},
{
"data": "design"
},
{
"data": "actual"
}
],
select: {
style: 'os',
selector: 'td:first-child'
},
lengthChange: false,
buttons: [
'pdf',
{ extend: 'create',
editor: editor2,
},
{ extend: 'edit', editor: editor2 },
{ extend: 'remove', editor: editor2 }
],
} );
editor2.on( 'initCreate', function () {
editor2.field('dataname').val("add current values here");
} );
This question has an accepted answers - jump to answer
Answers
I think i have it. Leaving this here for others
I had to grab the data out of the json.
This code pre-populates the fields of the Create form.
Spot on! That is virtually exactly what I would suggest .
I'd probably use
data[0]
rather than 1 though, just in case you don't have 2 records in the table already. You might also need to consider what happens if there are no records in the table, but that depends upon your data if that can ever happen.Allan
Great, thanks!