knockout dynamically adding a row in a table - html

Problem Description:
I am using knockout and I have a table. In this table, I have 3 columns. The first column has a drop down list. I want to generate a new row whenever a user chooses a value from a drop down list.
Here's my fiddle :
http://jsfiddle.net/JPVUk/10/
<table class="table table-bordered">
<thead class="mbhead">
<tr class="mbrow">
<th>Type</th>
<th>Comment</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<select class="input-small">
<option value="">Type</option>
<option value="">One</option>
<option value="">Two</option>
<option value="">Three</option>
</select>
</td>
<td><input class="input-small"/></td>
<td><input class="input-small"/></td>
</tr>
</tbody>
</table>
<button id="saveButton">save</button>`
I want to accomplish this using knockout. Is there a way to accomplish it using knockout?

If I understand your question correctly, you want knockout to add a new row every time the selection in one of your dropdowns is changed.
You can do this by creating a view model with an observable array containing items. You then get your dropdown to insert items into it whenever the selection changes. See the following:
var ViewModel = function() {
var self = this;
self.items = ko.observableArray([{comment:'first comment', amount:0}]);
self.addNewItem = function(){
self.items.push(new Item('',0));
};
}
var Item = function(comment, amount) {
var self = this;
self.comment = ko.observable(comment);
self.amount = ko.observable(amount);
};
vm = new ViewModel()
ko.applyBindings(vm);
If you then add the following to your table markup:
<tbody data-bind="foreach: items">
<tr>
<td>
<select class="input-small" data-bind="event: { change: $root.addNewItem }">
<option value="">Type</option>
<option value="">One</option>
<option value="">Two</option>
<option value="">Three</option>
</select>
</td>
<td><input class="input-small" data-bind="value: comment"/></td>
<td><input class="input-small" data-bind="value: amount"/></td>
</tr>
</tbody>
I updated your JsFiddle here

Related

Using option id to change value of an html table text element with jQuery

I want to change the value of an html table text element using a toggle and dropdown. When the option is selected, the HTML table text element should change numbers. Here is the html and jQuery I have so far.
<select name="subject" id="subject">
<option value="option1" id="xTest">Option 1</option>
<option value="option2" id="yTest">Option 2</option>
</select>
<table>
<tr>
<td id="testing">Test</td>
<td>Test</td>
</tr>
<tr>
<td>Test</td>
<td>Test</td>
</tr>
</table>
and the jQuery
<script>
$(document).ready(function() {
$("#xTest").click(function(){
("#testing").text("change");
});
})
$(document).ready(funtion() {
$(#yTest).click (function(){
("#testing").text("change2");
});
})
</script>
When selecting either dropdown (#xTest or #yTest), the value of #testing isn't changing. Do I need to add an a href attribute to the html? This wouldn't make sense to me, however, because I am not adding a link.
Any help would be much appreciated.
Thank you!
Events are not supported cross browser on <option> elements. Instead use the change event on the <select> and use it's value to determine the text to display
$("#subject").change(function() {
const txt = 'change ' + (this.value === 'option1' ? 1 : 2)
$("#testing").text(txt);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="subject" id="subject">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
</select>
<table>
<tr>
<td id="testing">Test</td>
<td>Test</td>
</tr>
<tr>
<td>Test</td>
<td>Test</td>
</tr>
</table>
Monitor <select> changes using jquery like this:
$('#subject').change(function () {
const text = 'change ' + (this.value === 'option1' ? 1 : 2)
$("#testing").text(text);
});

Get the selected value in a select - option Thymeleaf and Spring Boot

Im trying to generate a search options and in myweb page the user choose the value that is looking for this is my html code:
<table>
<tr>
<td><h2 align="left">Red Social: </h2></td>
<td>
<select th:field="*{redes}" name="redSoc">
<option th:each="r : ${redes}" th:value="${r.nombreRedSocial}"
th:text="${r.nombreRedSocial}"></option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" value="Buscar"/></td>
</tr>
</table>
And this is my controller code:
#GetMapping("/RedesSociales")
public String infoPorRedSocial(Model model, #RequestParam(name = "redSoc", required = false) String redSoc) {
RedesSociales[] redes = redesService.obtenerTodasRedes();
model.addAttribute("redes", redes);
//model.addAttribute("grupo", null);
System.out.println(redSoc);
if (redSoc != null) {
System.out.println(redSoc);
model.addAttribute("info", infoService.obtenerTodosPorRedSocial(redSoc));
}
return "visual/reportes/ListarPorRedSocial";
}
Now as you can see im trying to get the value redSoc but im not sure how to the get this value from the selected option i tried several methods but didnt work.
EDIT:
I was using this code:
<form action="/RedesSociales" method="GET">
<h2 align="left">Red Social: </h2>
<input type="text" name="redSoc"/> <br>
<input type="submit" value="Buscar"/>
</form>
But this was not usefull because the option would be insert by the user and i wanted to made it more dynamic thats why now im using the current values stored in the database
already solve the problem Change my code to this :
<table> <tr> <td><h2 align="left">Red Social: </h2></td>
<td> <select name="redSoc"> <option th:each="r : ${redes}" th:value="${r.nombreRedSocial}" th:text="${r.nombreRedSocial}">
</option>
</select> </td> </tr>
<tr> <td><input type="submit" value="Buscar"/></td> </tr> </table>

Handling of form data inside html table

I have a dynamic table with select and input type="text" for each row in database. How do i set attr "name" for the select and input to be properly read on server side, and how do i pass the values with ajax? Should all names be dynamic, unique, or with array....There are more inputs in the form beside table, but my concern is the table data only. Table is sortable, so rows change positions, and serialize change the order as well.
<table id="config" class="table" cellspacing="0" width="100%">
<thead>
<tr>
<th>name</th>
<th>Action</th>
<th class="no-sort">tag</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name 1</td>
<td>
<div class="form-group">
<select class="full-width config-action" name="action" data-init-plugin="select2" data-disable-search="true">
<option value="1">1</option>
<option value="2" selected>2</option>
<option value="3">3</option>
<option value="tag">Tag</option>
</select>
</div>
</td>
<td>
<input type="text" name="tag" class="form-control table-tag" >
</td>
</tr>
<tr>
<td>Name 2</td>
<td>
<div class="form-group">
<select class="full-width config-action" name="action" data-init-plugin="select2" data-disable-search="true">
<option value="1" selected>1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="tag">Tag</option>
</select>
</div>
</td>
<td>
<input type="text" name="tag" class="form-control table-tag" >
</td>
</tr>
</tbody>
</table>
For ajax, if i serialize the data, they can change order if table is sorted, so names should be dynamic, but how should i handle this without serialize?
........
var form = $(this);
var action = form.attr('action');
$.ajax({
type: "POST",
url: action,
data: form.serialize(),
success: function(response) {
if(response === 'true') {
}
else {
var msg = response;
}
}
});
May be you can dynamically add the "name" attributes before submitting the form this way:
$("input.table-tag").each(function(i){
$(this).attr("name","tag"+i);
})
The names will turn to be tag0, tag1 etc. Not sure if that is what is required.
Your form elements should be named appropriately in your HTML.
For instance:
<select name="table[1][action]" ...
<input name=table[1][tag]" ...
...
<select name="table[2][action]" ...
<input name=table[2][tag]" ...
Then you would do something like this, assuming you're using PHP:
foreach($_POST['table'] as $key => $row){
// now use $key, $row['tag'], $row['action'] ...
}
The $key variable would normally be a database ID.

How to add tr or any html tag between two tr

Following is the html that I am using I want to add one tr between first two trs but it is not working.
I want to add a html tag and include class for first two tr. How can I achieve that? I do not want to include the submit button's tr in it.
<table cellspacing="0" id="content-space" class="content-space">
<tr>
<td>
Subscription Plan:
</td>
<td>
<select id="PlanNameId" style="width:auto !Important" name="data[][PlanNameId]">
<option value="4">
Disable
</option>
<option selected="selected" value="2">
Free
</option>
<option value="1">
Bronze
</option>
</select>
</td>
<td>
</td>
</tr>
<tr>
<td>
Auto Upgrade:
</td>
<td>
<select id="SubscriptionSetting" name="data[][SubscriptionSetting]">
<option value="1">
Yes
</option>
<option selected="selected" value="0">
No
</option>
</select>
</td>
<td>
</td>
</tr>
<tr>
<td colspan="2" class="signin-button-containers">
<input type="submit" value="Submit" id="subscriptionsubmit" class="primaryLargeButton">
</td>
</tr>
You can simply get the first tr and then insert the new row right after it. With jQuery this is very easy, but without any libraries it can also be done without too much effort.
$("table tr").first().after("<tr><td>Inserted data</td><td>More data in between</td></tr>")
Example: http://jsfiddle.net/qnu3c0uk/1/
Given:
<table>
<tr>
<td>Text</td>
</tr>
<tr>
<td>Text 2</td>
</tr>
<table>
And that you want to insert a table row (with a cell) after the first table row i would do:
$("table tr:first-of-type").after($("<tr>").append("<td>"));
If you then want to add a class to the first and the new row, i would do:
$("table tr:eq(0),table tr:eq(1)").addClass("row-class");
Or actually combining the two parts to:
var $newRow = $("<tr>").addClass("new-class");
var $firstRow = $("table tr:first-of-type").addClass("new-class");
$firstRow.after($newRow.append("<td>"));

HTML select or textarea

I'm trying to figure out how to create a test area where the user can either select from a list of items, or add to a textarea. Is this possible? Something like the following, except restrict the user to either selecting from the list, or entering into the textarea.
<tr>
<td width="100" class="formNames">Frequency</td>
<td colspan="2" class="cellColor">
<select name="yr" class="textbox">
<option value="0">-</option>
<option value="30">1</option>
<option value="30">2</option>
<option value="30">3</option>
<option value="30">4</option>
<option value="30">5</option>
</select>
</td>
<td width="10" class="formNames"> -or- </td>
<td class="formNames">
<input type="text" name="scheduletitle" class="textbox" value="default" />
</td>
</tr>
disable the other when data is changed in one. Using jQuery:
(function(){
var elements = $('select[name=yr],input[name=scheduletitle]');
elements.bind('change', function(){
var self = this;
elements.attr('disabled', false);
$(this).val() && elements.filter(function(){ return this != self; }).attr('disabled', true);
}
})();