Reputation: 322
I have a working select box inside a table.
<tr>
<td>
<select name="type" class="inputbox input_table2 table2_index_4" id="item_type">
<?php foreach ($item_type as $type) { ?>
<option value="<?php echo $type->id ?>"><?php echo $type->name ?></option>
<?php } ?>
</select>
</td>
</tr>
And here is my scenario My table rows are appended with on trigger a button So how can code this loop in jQuery
I am very beginner in JavaScript libraries and here is what i am trying in JQuery
<?php
$addtablerow = '
$(document).on("click",".add_row_button",function(e) {
e.preventDefault();
var add_new_row = "<tr class=\'row\'> \
<td><select name=\'type\' id=\'item_type\' class=\'item_type\'> \
<?php foreach ($item_type as $type) { ?> \
<option vlaue=\'<?php echo $type->id ?>\'> <?php echo $type->name ?> </option> \
<?php } ?> \
</select> \
</td> \
</tr>";
$("table#item_type tbody").append(add_new_row);
indexassigner();
});
';
$this->registerJs($addtablerow, View::POS_READY);
?>
Thanks
Upvotes: 0
Views: 2026
Reputation: 322
I appreciate @Swati for introducing me the clone() property and thank you cauz i didn't know before. Here is the simplest way I found that just appending the PHP code as string in to jQuery.
Inside table
<tr>
<td>
<select name="type[]" class="item_type inputbox input_table2 table2_index_4">
<?php
$code_as_string = "<select name='type[]' class='item_type inputbox input_table2' >";
foreach ($item_type as $type) {
?>
<option value="<?php echo $type->id ?>"><?php echo $type->name ?></option>
<?php
$code_as_string .= "<option value='" . $type->id . "'>" . $type->name . "</option>";
}
$code_as_string .= "</select>";
?>
</select>
</td>
</tr>
And simply append the variable $code_as_string to jQuery part
<?php
$addtablerow = '
$(document).on("click",".add_row_button",function(e) {
e.preventDefault();
var add_new_row = "<tr class=\'row\'><td>' . $code_as_string . ' </td> </tr>";
$("table#item_type tbody").append(add_new_row);
indexassigner();
});
';
$this->registerJs($addtablerow, View::POS_READY);
?>
Upvotes: 1
Reputation: 28522
As your first select-box i.e : type
is same for all trs you can use clone()
method to get clone of that first select-box which might be already present in your table and then pass same to your dynamically generated html code.
Demo Code :
$(document).on("click", ".add_row_button", function(e) {
e.preventDefault();
var slects = $('#item_type tr:first').find("select[name=type]").clone(); //get options clone them
var add_new_row = "<tr class='row'><td><select name = 'type' id='item_type' class='item_type'>" + $(slects).html() + "</select></td> </tr>"; //same same inside slect-box
$("table#item_type tbody").append(add_new_row);
//indexassigner();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="item_type">
<tbody>
<tr>
<td>
<select name="type" class="inputbox input_table2 table2_index_4" id="item_type">
<!--dummy datas-->
<option value="1">X</option>
<option value="2">Y</option>
<option value="3">Z</option>
</select>
</td>
</tr>
</tbody>
</table>
<button class="add_row_button">Add more </button>
Upvotes: 1