How to dynamically alter a value in mvc3 html page - html

<tr>
<td>
Delivery
</td>
<td>
<div class="confdelivery">
<select id="NxtDay" name="NxtDay">
<option selected="selected" value="1">Normal Devliery + 10.00</option>
<option value="2">Next Day Delivery + 20.00</option>
</select>
</div>
</td>
</tr>
<tr>
<td>
Total
</td>
<td>
#{
var total = Model.CartTotal;
}
#total
</td>
</tr>
Basically if a user selects next day delivery. I want 20 to be added to #total?

I thing you are going about this the wrong way. You either update some input value with javascript, or use the NxtDay value in your controller action to assign the correct value, depending on the selection.
ADDED:
What you want to do is something like this: (I am assuming you are using jquery, since it come with MVC3).
HOWEVER: You should check out some tutorial if you are starting. This is pretty basic stuff and you won't go far without having to come back here unless to read up on this sutff.
<tr>
<td>
Delivery
</td>
<td>
<div class="confdelivery">
<select id="NxtDay" name="NxtDay">
<option selected="selected" value="10">Normal Devliery + 10.00</option>
<option value="20">Next Day Delivery + 20.00</option>
</select>
</div>
</td>
</tr>
<tr>
<td>
Total
</td>
<td>
<span id="totalValue" style="display: none">#Model.CartTotal</span>
<span id="totalWithShipping">0</span>
<input id="hiddenTotal" type="hidden" value="0">
</td>
</tr>
<script>
$(document).ready(function () {
$('#NxtDay').change(function() {
addToTotal();
});
});
function addToTotal(){
$('#totalWithShipping').html($('#totalValue').html() + $("#NxtDay option:selected").val());
$('#hiddenTotal').val($('#totalWithShipping').html());
}
</script>

Related

Typo3: How do I make a loop for each element in a table?

I have a Typo3(11.5.12) website, with a generated sitepackage and the Fluid Template Engine.
I want to create a dynamic table element in a custom template. It is supposed to create an element for each entry a user makes on a the page in Typo3.
I tried to implement this with a table and I want to make an iterator, that loops once for each element added to this table in the custom html layout. In the loop I want to create an option element containing the text of the table row and a custom ID.
I already made an Iterator with fluid, that loops for each element in an array, so maybe converting the table content to an array might work?
If there is another content module that fits better I would use that, I also considered a simple text element with an iterator for each line.
I made a variable content, which gets the content of said table in the setup.typoscript file.
The table:
The variable in setup.typoscript:
variables{
content < styles.content.get
}
The (current) content of the variable:
<div id="c21" class="frame frame-default frame-type-table frame-layout-0">
<table class="ce-table">
<tbody>
<tr> <td> Testkunde 1 </td> </tr>
<tr> <td> Testkunde 2 </td> </tr>
<tr> <td> Testkunde 3 </td> </tr>
<tr> <td> Testkunde 4 </td> </tr>
<tr> <td> Testkunde 5 </td> </tr>
<tr> <td> Testkunde 6 </td> </tr>
<tr> <td> Testkunde 7 </td> </tr>
<tr> <td> Testkunde 8 </td> </tr>
<tr> <td> Testkunde 9 </td> </tr>
<tr> <td> Testkunde 10 </td> </tr>
</tbody>
</table>
</div>
Snippet from the custom html layout, where I want to implement the iterator:
<table>
<tr>
<td>
<form>
<input type="hidden" name="id" value="4">
<input type="hidden" name="Select" value="All">
<select name="Nr" size="20" style="width:400px;">
<option value="1">Kunde 1</option>
<option value="2">Kunde 2</option>
<option value="3">Kunde 3</option>
<option value="4">Kunde 4</option>
<option value="99"> {content}</option>
<!--I want to create an option element
for each line of the table here-->
</select>
</form>
</td>
</tr>
</table>
When manually rendering a backend-created text table, I built my own fluid view helper for this that wraps TYPO3\CMS\Core\Utility\CsvUtility::csvToArray. Its result can easily be iterated on.

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>

Proper way to post a new form in each row of a table?

I have a page where there is a table. In each row of the table, there is data about an online grocery delivery order. At the end of each row, there is a submit button about action to be performed on the order and the form ends there. A new form begins with the next row <tr>
Where exactly should I put the <form> and </form> tags? Should I put the <form> before each <tr> and </form> after the </tr> or should I introduce them in the first data cell <td> of each row and close in the last <td>?
I mean, the page would probably function all right in both ways, but what is "proper" way of doing this? Right now mozilla code view is showing the table tag in red color.
This is how it looks right now.
And this is the relevant part of the php code I am using to dynamically generate a new form for each table row. I am posting this just for the sake of reference, because this does not matter at all here. My question is basic HTML based, not php based.
for($i=0;$i<$count;++$i){
$res.='<form action="" method="post">' ."\n" .'<input type="hidden" name="orderid" value="' .$orders[$i]->idcode .'" />';
$res.="$nt<tr><td align=\"center\">" .$orders[$i]->display("pe","</td><td align=\"center\">") ."</td>$nt\t<td align=\"center\"><select name=\"agent\">$alistcode</select></td>$nt\t<td align=\"center\"><select name=\"vendor\">$vlistcode</select></td>";
$res.="$nt\t<td align=\"center\"><input type=\"submit\" value=\"PROCESS\" /></td>\n</tr>\n</form>\n";
}
$res.="</table>";
echo $res;
HTML5 offers decent solution for your problem. And its name is form attribute. Just put the form into last <td> and refer to its id from other inputs. See example.
<table>
<tr>
<td>
<input type="text" name="inp1" form="form1" />
</td> <!-- ^^^^ ^^^^^ -->
<td>
<form id="form1" method="get" action="/">
<!-- ^^ ^^^^^ -->
<input type="submit" name="submit1" value="submit" />
</form>
</td>
</tr>
<tr>
<td>
<input type="text" name="inp2" form="form2" />
</td>
<td>
<form id="form2" method="get" action="/"><input type="submit" name="submit2" value="submit" /></form>
</td>
</tr>
</table>
If using javascript is an option, you can work without <form> tags?
I used jQuery in this example, data is posted to itself (first argument of $.post()).
Table
<table>
<tbody>
<tr>
<td><input name="inputfield[]" type="text" value="input1" /></td>
<td><select name="selectfield[]">
<option selected value="select1-option1">select1-option1</option>
<option value="select1-option2">select1-option2</option>
<option value="select1-option3">select1-option3</option>
</select></td>
<td><a class="submit">Update</a></td>
</tr>
<tr>
<td><input name="inputfield[]" type="text" value="input2" /></td>
<td><select name="selectfield[]">
<option selected value="select2-option1">select2-option1</option>
<option value="select2-option2">select2-option2</option>
<option value="select2-option3">select2-option3</option>
</select></td>
<td><a class="submit">Update</a></td>
</tr>
<tr>
<td><input name="inputfield[]" type="text" value="input3" /></td>
<td><select name="selectfield[]">
<option selected value="select3-option1">select3-option1</option>
<option value="select3-option2">select3-option2</option>
<option value="select3-option3">select3-option3</option>
</select></td>
<td><a class="submit">Update</a></td>
</tr>
</tbody>
</table>
Script
$(".submit").on("click", function(){
var row = $(this).parents("tr");
$.post("", row.find("input, select, radio").serialize(), function(data){ console.log(data); });
});
JSFiddle

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>"));