I am using a jQuery DataTables and adding a child row without Ajax. The problem is I display some information in the parent row and when the trigger is clicked reveals some form elements that can be filled to update the post.
I have two problems:
-DataTables strips out all my HTML from the child data.It only keeps the html I put in the function for format:
function format(value) {
return '<div><form action="#" method="post">' + value + '<input type="submit" value="send"></form></div>';
}
-The child row includes the content from the 3 last elements from the parent row (separated with commas). The child content is stripped of html.
<tr>
<td colspan="8">
<div>
<form method="post" action="#">
999-15-03-02,2015-03-01,92854408,RSG09LECA-ROG09LEC,
<i class=" vis_info fi-alert size-16" title="Det mangler info. Klikk for å fylle ut!" style="color: red;"></i>
,
<i class="fi-print size-16" title="Skriv ut serviceskjema" style="color:#836F6F"></i>
,
<input class="service_dato dato" type="text" rel="2" value="08-03-15" name="service_dato">
<input class="alt_dato" type="hidden" name="alt_dato">
<i class="fi-check size-14" style="display:inline"></i>
,
<input class="fakt_mont dato" type="text" rel="2" value="10-03-15" name="fakt_mont">
<input class="alt_dato" type="hidden" name="alt_dato">
<i class="fi-check size-14" style="display:inline"></i>
<input type="submit" value="send">
</form>
</div>
</td>
</tr>
Here is my table:
<table class="datatable display compact cell-border" id="servicetable">
<thead>
<th>Servicenr.</th>
<th>Dato</th>
<th>Mobil</th>
<th>Pumpe</th>
<th></th>
<th></th>
<th>Utført</th>
<th>Fakturert</th>
</thead>
<tbody>
<tr data-child-name="row0" data-child-value='<label for="service_avtalt">Avtalt dato</label><input type="text" name="service_avtalt" value=""><label for="snr_innedel">Serienr. innedel</label><input type="text" name="snr_innedel" value=""><label for="snr_utedel">Avtalt dato</label><input type="text" name="snr_utedel" value=""><label for="reg_nr">Reg.nr.</label><input type="text" name="reg_nr" value=""><label for="sist_service">Tidl.service</label><input type="text" name="sist_service" value=""><label for="sist_feil">Tidlilgere feil</label><input type="text" name="sist_feil" value=""><label for="at_innerdel">Måling av Δt over innerdel i varmedrift</label><input type="text" name="at_innerdel" value=""><label for="trykk_vdrift">Trykk varmedrift</label><input type="text" name="trykk_vdrift" value=""><label for="kommentar">Kommentar</label><input type="text" name="kommentar" value="Toshiba pumpe!!">'>
<td>999-15-03-02</td>
<td>2015-03-01</td>
<td>92854408</td>
<td>RSG09LECA-ROG09LEC</td>
<td> <i class=" vis_info fi-alert size-16" style="color: red;" title="Det mangler info. Klikk for å fylle ut!"></i>
</td>
<td class="senter"><i class="fi-print size-16" style="color:#836F6F" title="Skriv ut serviceskjema"></i>
</td>
<td>
<input type="text" name="service_dato" class="service_dato dato" value="08-03-15" rel="2" />
<input type="hidden" name="alt_dato" class="alt_dato" /> <i class="fi-check size-14" style="display:inline"></i></td>
<td>
<input type="text" name="fakt_mont" class="fakt_mont dato" value="10-03-15" rel="2" />
<input type="hidden" name="alt_dato" class="alt_dato" /> <i class="fi-check size-14" style="display:inline"></i>
</td>
</tr>
Here is my example: http://jsfiddle.net/asle/a2p4kmh9/14/
Edited from @davidkonrads suggestion:
What if the link is like this with the rows ID and I pick it up in the click function?
<i class=" vis_info fi-alert" rel="34"></i>
$('#servicetable tbody').on('click', 'i.vis_info', function () {
var tr = $(this).closest('tr');
var row = table.row(tr);
var rid = $(this).attr('rel');
$.post("code/get_child_data.php", {
id: rid },
function(data) {
var form = (data);
});
if (row.child.isShown()) {
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
} else {
// Open this row and use the data from get_child_data.php
row.child(form).show();
tr.addClass('shown');
}
});
I forgot to say that some fields in the hidden row could have data already so I have to get the existing (if any) data for every row, also to edit current values. That is why I need to insert this dynamicaly.
Edited again and working - thanks @davidkonrads
Here is what works now. I had to move the functions around so I could get to the ajax data. - I attach a click event to the element - Add the rel=row_id from my loop so I can get it later - Post the row_id with ajax and add the content to hidden row and show it
$('#servicetable tbody').on('click', 'i.vis_info', function () {
var tr = $(this).closest('tr');
var row = table.row( tr );
var rid = $(this).attr('rel');
$.post("code/get_service_child.lasso", {
id: rid },
function(data) {
var form = (data);
if ( row.child.isShown() ) {
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
}
else {
// Open this row with data from $post call
row.child(form).show();
tr.addClass('shown');
};
});
} );
I will also be validating the form and posting it with $.post - then changing the alert icon to success icon. This is fun when it works :-)