Reputation: 63
im trying to add the rows with:
$("#add_row").click(function(){
$('#addr'+i).html("<td>{{ Form::select('sexo', ['' => '', '0' => 'Macho', '1' => 'Hembra'],Input::old('sexo'), array('id' => 'sexo["+i+"]', 'class' => 'form-control', 'style' => 'min-width:100px;')) }}</td><td><input type='text' name='color' id='color["+i+"]' placeholder='Color' class='form-control' style='min-width:100px;'/></td><td>{{ Form::select('razas', ['' => ''] + $razas , Input::old('razas'), array('id' => 'razas["+i+"]', 'class' => 'form-control', 'style' => 'min-width:100px;')) }}</td><td>{{ Form::select('tipos_ganados', ['' => ''] + $tipos_ganados , Input::old('tipos_ganados'), array('id' => 'tipos_ganados["+i+"]', 'class' => 'form-control', 'style' => 'min-width:100px;')) }}</td><td><input type='text' name='precioxkg' id='A["+i+"]' placeholder='$' class='calc form-control' style='min-width:100px;' /></td><td><input type='text' name='peso' id='B["+i+"]' placeholder='Kg' class='peso form-control' style='min-width:100px;'/></td><td><input type='text' name='monto' id='C["+i+"]' class='form-control total' placeholder='$' style='min-width:100px;' required readonly></td><td><button name='del0' class='btn btn-danger glyphicon glyphicon-remove row-remove'></button></td>");
$('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
i++;
});
For remove button
$(tr).find("td button.row-remove").on("click", function() {
$(this).closest("tr").remove();
});
This is my default table
<tr id='addr0'>
<td>{{ Form::select('sexo', ['' => '', '0' => 'Macho', '1' => 'Hembra'],Input::old('sexo'), array('id' => 'sexo[1]', 'class' => 'form-control', 'style' => 'min-width:100px;')) }}</td>
<td><input type="text" name='color' id="color[1]" placeholder='Color' class="form-control" style="min-width:100px;"/></td>
<td>{{ Form::select('razas', ['' => ''] + $razas , Input::old('razas'), array('id' => 'razas[1]', 'class' => 'form-control', 'style' => 'min-width:100px;')) }}</td>
<td>{{ Form::select('tipos_ganados', ['' => ''] + $tipos_ganados , Input::old('tipos_ganados'), array('id' => 'tipos_ganados[1]', 'class' => 'form-control', 'style' => 'min-width:100px;')) }}</td>
<td><input type="text" name='precioxkg' id="A[1]" placeholder='$' class="calc form-control" style="min-width:100px;" /></td>
<td><input type="text" name='peso' id="B[1]" placeholder='Kg' class="peso form-control" style="min-width:100px;"/></td>
<td><input type="text" name="monto" id="C[1]" class="form-control total" placeholder="$" style="min-width:100px;" required readonly></td>
<td><button name="del0" class='btn btn-danger glyphicon glyphicon-remove row-remove'></button></td>
</tr>
<tr id='addr1'></tr>
But my add_row code isn't working ...
PD: Also i should Multiply for example A["+i+"] * B["+i+"] = C["+i+"] , With this other add arrow code i can add rows
$("#add_row").on("click", function() {
// Dynamic Rows Code
// Get max row id and set new id
var newid = 0;
$.each($("#tab_logic tr"), function() {
if (parseInt($(this).data("id")) > newid) {
newid = parseInt($(this).data("id"));
}
});
newid++;
var tr = $("<tr></tr>", {
id: "addr"+newid,
"data-id": newid
});
// loop through each td and create new elements with name of newid
$.each($("#tab_logic tbody tr:nth(0) td"), function() {
var cur_td = $(this);
var children = cur_td.children();
// add new td and element if it has a nane
if ($(this).data("name") != undefined) {
var td = $("<td></td>", {
"data-name": $(cur_td).data("name")
});
var c = $(cur_td).find($(children[0]).prop('tagName')).clone().val("");
c.attr("name", $(cur_td).data("name") + newid);
c.appendTo($(td));
td.appendTo($(tr));
} else {
var td = $("<td></td>", {
'text': $('#tab_logic tr').length
}).appendTo($(tr));
}
});
// add the new row
$(tr).appendTo($('#tab_logic'));
$(tr).find("td button.row-remove").on("click", function() {
$(this).closest("tr").remove();
});
});
});
The add_row button works with this one but cant calculate by row
The default table for this
<tr id='addr0' data-id="0">
<td data-name="sexo">
{{ Form::select('sexo0', ['' => '', '0' => 'Macho', '1' => 'Hembra'],Input::old('sexo'), array('id' => 'sexo[1]', 'class' => 'form-control', 'style' => 'min-width:100px;')) }}
</td>
<td data-name="color">
<input type="text" name='color' id="color[1]" placeholder='Color' class="form-control" style="min-width:100px;"/>
</td>
<td data-name="razas">
{{ Form::select('razas0', ['' => ''] + $razas , Input::old('razas'), array('id' => 'razas[1]', 'class' => 'form-control', 'style' => 'min-width:100px;')) }}
</td>
<td data-name="tipo">
{{ Form::select('tipos_ganados0', ['' => ''] + $tipos_ganados , Input::old('tipos_ganados'), array('id' => 'tipos_ganados[1]', 'class' => 'form-control', 'style' => 'min-width:100px;')) }}
</td>
<td data-name="precioxkg">
<input type="text" name='precioxkg' id="precioxkg[1]" placeholder='$' class="calc form-control" style="min-width:100px;" />
</td>
<td data-name="peso">
<input type="text" name='peso' id="peso[1]" placeholder='Kg' class="peso form-control" style="min-width:100px;"/>
</td>
<td data-name="monto">
<input type="text" name="monto" id="total[1]" class="form-control total" placeholder="$" style="min-width:100px;" required readonly>
</td>
<td data-name="del">
<button nam"del0" class='btn btn-danger glyphicon glyphicon-remove row-remove'></button>
</td>
</tr>
Need help! Thanks in advance
Upvotes: 0
Views: 789
Reputation: 598
Laravel uses PHP to process the template (and replace the content within {{ }} with real data) a single time, when the view is created.
You're generating blade template code on the client side with javascript, and so the php code never gets executed.
What you'd need to do is make an ajax call to the server to return the information you need to build your new form fields, and build them with actual data, not blade template code.
Upvotes: 1