First of all, I am using Blazor WebAssembly 3.2.0 Preview 4.
I have a list of objects named ObjectList and each object has an bool property named principal. This list populates a table.
The logic is that only one element in the list can have the value of principal property set to true so this is why radio buttons are ideal.
In this example, I am using MatBlazor as a library for controls.
Is there a way to put radiobutton for this property inside a table as in the code below?
<table class="table table-condensed">
<thead>
<tr>
<th>Object</th>
<th>Principal</th>
</tr>
</thead>
<tbody>
#foreach (var object in ObjectList)
{
<tr>
<td>#object .name</td>
<td>
<MatRadioButton TValue="bool" Value="#object.principal"></MatRadioButton>
</td>
</tr>
}
</tbody>
</table>
The MatRadioButton button goes inside a MatRadioGroup:
<MatRadioGroup #bind-Value="#Val1" TValue="string">
<MatRadioButton Value="#string.Empty" TValue="string">Default</MatRadioButton>
<MatRadioButton Value="#("f")" Label="Female" TValue="string"></MatRadioButton>
<MatRadioButton Value="#("m")" TValue="string">Male</MatRadioButton>
<MatRadioButton Value="#("d")" Disabled="true" TValue="string">Disabled</MatRadioButton>
</MatRadioGroup>
#code
{
protected string Val1;
}
MatRadioButton Documenation
So in your case, it might be something like:
<MatRadioGroup #bind-Value="#object.principal" TValue="bool">
<MatRadioButton Value="true" TValue="bool">True</MatRadioButton>
<MatRadioButton Value="false" TValue="bool">False</MatRadioButton>
</MatRadioGroup>
I share this here also because I needed similiar, and there weren't many great answers around to be found.
Related
I ran into an almost identical issue before and posted a question about it here: Posting data of a single table row when table is in one form We were able to solve it but it felt pretty hacky. To summarize that post, I was attempting to use AJAX and JS to POST values from a table row to a controller. The problem was that every row would be serialized within the form. To solve this, I serialized only the row I needed.
This time I am running into the same issue, the only difference being that I'm not using AJAX and am submitting the model data from the table row directly to the controller action.
I tried to use both the foreach and for loops to generate table rows.
The foreach loop always POSTs the first row in the table, even if I click submit button on the second row.
The for loop doesn't POST anything, or at least I get all null values in my controller action's "shipment" parameter.
I also tried to encase the row in a form, but HTML does not allow to have a <form> in <tbody>.
What I want is to be be able to POST a single row (generated by looping through a list for models) directly to the "UpdateShipment" controller action. All of this without using AJAX.
HTML code:
#model OrderTrackingContract.SalesOrder
#foreach (var lineItem in Model.LineItems)
{
<table class="lineItemTables">
//line items table
</table>
#if (lineItem.Shipments.Count > 0)
{
<form method="post">
<table class="table shipmentTable">
<thead>
<tr>
<th>
ShipmentID
</th>
<th>
Qty Shipped
</th>
<th>
Actions
</th>
</tr>
</thead>
<tbody>
#for (int i = 0; i < lineItem.Shipments.Count; i++)
{
<tr>
<td>
<input class="shipmentID" asp-for="#lineItem.Shipments[i].ShipmentID" />
</td>
<td>
<input class="qtyShipped" asp-for="#lineItem.Shipments[i].QtyShipped" value="#lineItem.Shipments[i].QtyShipped" min="1" max="999" />
</td>
<td>
<input class="updateButton" type="submit" value="Update" asp-action="UpdateShipment" asp-controller="Shipments" />
<input class="deleteButton" type="submit" value="Delete" asp-route-shipmentID="#lineItem.Shipments[i].ShipmentID" asp-action="DeleteShipment" asp-controller="Shipments" />
</td>
</tr>
}
</tbody>
</table>
</form>
}
}
Controller action:
[HttpPost]
public IActionResult UpdateShipment(Shipment shipment)
{
_orderTrackingService.UpdateShipmentByID(shipment.ShipmentID, shipment.QtyShipped);
return NoContent();
}
EDIT: I used fiddler and I'm starting to understand what's going on. Both for and foreach loop submit the whole form because the whole table is in it.
Using a for loop I am forced to write asp-for="#lineItem.Shipments[i].ShipmentID", which means the controller must accept a parameter of LineItem lineItem.
Using foreach loop I am able to write asp-for="#shipment.ShipmentID, but because the generated attributes of the <input> are identical, the 'Shipment shipment' parameter binds to the first values POSTed.
What I want is to be be able to POST a single row (generated by looping through a list for models) directly to the "UpdateShipment" controller action. All of this without using AJAX.
To achieve above requirement, you can try to generate <form> and <table> for each Shipment item, like below.
#foreach (var lineItem in Model.LineItems)
{
<table class="table shipmentTable">
<thead>
<tr>
<th>
ShipmentID
</th>
<th>
Qty Shipped
</th>
<th>
Actions
</th>
</tr>
</thead>
</table>
#if (lineItem.Shipments.Count > 0)
{
foreach (var Shipment in lineItem.Shipments)
{
<form method="post">
<table class="table shipmentTable">
<tbody>
<tr>
<td>
<input class="shipmentID" asp-for="#Shipment.ShipmentID" />
</td>
<td>
<input class="qtyShipped" asp-for="#Shipment.QtyShipped" value="#Shipment.QtyShipped" min="1" max="999" />
</td>
<td>
<input class="updateButton" type="submit" value="Update" asp-action="UpdateShipment" asp-controller="Shipments" />
<input class="deleteButton" type="submit" value="Delete" asp-route-shipmentID="#Shipment.ShipmentID" asp-action="DeleteShipment" asp-controller="Shipments" />
</td>
</tr>
</tbody>
</table>
</form>
}
}
}
Test Result
I have a list of object and iterating that list using ng-repeat to generate table row. each td contains source name and checkbox. I want to bind the checkbox with a property which is not available into list. how that is possible? The list is like that:-
scope.reasons = [
{sourceName:'Lack of rainfall'},
{ sourceName: 'Change in land use' },
{sourceName:'Change in Land Cover'}
];
and the HTML code is like that:-
<table>
<tr ng-repeat="source in reasons">
<td>{{source.sourceName}}</td>
<td><input type="checkbox" ng-checked="source.postAdequate"></td>
</tr>
</table>
You can do this using the ng-model attribute, ng-change is just for the checking purposes that is change detection.
<table>
<tr ng-repeat="source in reasons">
<td>{{source.sourceName}}</td>
<td>
<input type="checkbox" ng-model="source.postAdequate" ng-change="changeDet()">
</td>
</tr>
</table>
Demo Fiddle
Hope this helps
Try this:
<input type="checkbox" ng-model="source.postAdequate">
See here jsfiddle link:
https://jsfiddle.net/avnesh2/5cpm48tc/2/
But it will add source.postAdequate: true/false only if you click, remains objects will remains same.
So if you want to add source.postAdequate: true/false in all add in $scope.reasons before only.
Hope this will help you.
Using ng-model instead of ng-checked will add property internally when checkbox is changed
<input type="checkbox" ng-model="source.postAdequate"></td>
If you must have that property on all objects iterate the array and add it
scope.reasons.forEach(function(item){
item.postAdequate = false
})
Can you loop the scope object once to initially set postAdequate:false for all items. Then bind that object in checkbox ng-model.
angular.forEach($scope.reasons, function(e,i){
e.postAdequate = false;
});
html code
<table>
<tr ng-repeat="source in reasons">
<td>{{source.sourceName}}</td>
<td>
<input type="checkbox" ng-model="source.postAdequate" ng-click="clickfunction(source.postAdequate)">
</td></tr>
</table>
I created xquery function which returns a table:
declare function local:table($collection as xs:string*, $interface as xs:string?, $date as xs:string?) as node() {
<table border="1">
<thead>
<tr>
<th>Inteface Name</th>
<th>Test Date</th>
<th>Test Result</th>
<th>Report Link</th>
</tr>
</thead>
<tbody>
{
for $child in xmldb:get-child-resources($collection)
let $doc := fn:doc(fn:concat($collection, '/', $child))
where (fn:ends-with($child, '.xml'))
and (($doc//*:interfaceName/text() eq $interface) or empty($interface))
and (($doc//*:reportDate/text() eq $date) or empty($date))
order by $doc//*:reportDate/text() descending
return
<tr>
<td>
{$doc//*:interfaceName/text()}
</td>
<td>
{$doc//*:reportDate/text()}
</td>
<td>
{$doc//*:testResult/text()}
</td>
<td>
<li>
<!--{$child} -->
{$child}
</li>
</td>
</tr>
}
</tbody>
</table>
I also added a few input controls on the page. One of them looks like:
<InterfaceName constraint="true" readonly="false" required="false" relevant="true">
<value>test</value>
</InterfaceName>
<xf:bind nodeset="InterfaceName">
<xf:bind nodeset="value" type="string"/>
<xf:input id="InterfaceName" ref="InterfaceName/value" incremental="true">
<xf:label></xf:label>
<xf:hint>xxxxxYYYZZZ</xf:hint>
<xf:help>Enter interface name</xf:help>
<xf:alert>Enter interface name</xf:alert>
</xf:input>
I also added a button to the webpage:
<trigger1 constraint="true" readonly="false" required="false" relevant="true">
<value></value>
</trigger1>
<xf:submission id="s-send"
replace="instance"
resource="echo:test"
method="get">
</xf:submission>
<div>
<xf:trigger id="trigger1" ref="trigger1/value" incremental="true">
<xf:label>Filter output</xf:label>
<xf:hint>a Hint for this control</xf:hint>
<xf:help>help for trigger1</xf:help>
<xf:send submission="s-send"/>
</xf:trigger>
</div>
On this button click I need to somehow pass parameters form those input controls to xquery function and return the table to the webpage. Entire webpage is of type xQuery (it builds html) and run with eXist-db.
Could you help me with this, please?
Thank you.
You'll need four elements to achieve your goal:
An <xf:instance id="result" > to store the result of calling your xquery. Make sure to add an id attribute to identify the instance further.
An <xf:submission> to call your xquery an store the result in the instance. This is the submission you'll call in the <xf:send> and may look like this:
<xf:submission id="s-send" method="get" replace="instance" instance="result">
<xf:resource value="concat('myxquery.xq?interface=',InterfaceName/value)"/>
</xf:submission>
Note that the concat function is used to build the xquery url, including parameters.
An <xf:output value="instance('result')" mediatype="application/xhtml+xml"> to show the contents of the result instance. The mediatype="application/xhtml+xml" attribute is needed to display the html table.
In the server side, you can't call an xquery function directly, you need to write an xquery (myquery.xq) that calls the function and extracts the parameters from the URL.
Take a look to this sample https://en.wikibooks.org/wiki/XQuery/Getting_URL_Parameters
<tr th:each="current : ${object.list}" >
<td th:text="${current.currentList...???}"></td>
...
I have an object that has a list.
"current" has also a list inside it called currentList.
currentList has only one element called objectTwo. I want to access to the attributes of objectTwo.
It's possible?
Hope this will work for you.
<tr th:each="current : ${object.list}" >
<td th:text="${current.currentList[0].objectTwo...}"></td>
...
I have a HTML widget in my ui.xml which I am using in Uibinder to populate data as given below:
ui.xml ->
<g:HTML ui:field="operationsDetailTableTemplate" visible="false">
<table class="{style.LAYOUT_STYLE}" width="100%" border="1">
<tr>
<td><img src="images/indent-blue.gif"/></td>
<td>
<table class="{style.DEFAULT_STYLE}">
<thead>
<tr>
<th>OperationUuid</th>
....
</tr>
</thead>
<tbody>
<tr>
<td>%s</td>
...
</tr>
</tbody>
</table>
</td>
</tr>
....
</g:html>
Uibinder.java--->
String htmlText = operationsDetailTableTemplate.getHTML()
.replaceFirst("%s", toSafeString(operation.getOperationUuid()))
....
HTML html = new HTML(htmlText);
operationsDetail.add(html);
The above is done in a for loop for each of the operation retrieved from the database.
My question is how I can embed a hyperlink or an anchor tag on one of the cell (eg. operation id ) for each of the operation set retrieved. I also wish to have a listener attached to it.
P.S. - It does not allow me to have a anchor tag in HTML in ui.xml.
You'd better use the tools in the way they've been designed to be used: use ui:field="foo" on the <td> and #UiField Element foo + foo.setInnerHTML(toSafeString(...)) instead of extracting the HTML, modifying it and reinjecting it elsewhere. You could also use a <g:Anchor> and attach an #UiHandler to handle ClickEvents.
Your way of using UiBinder makes me think of SafeHtmlTemplates, or the new UiRenderer aka UiBinder for Cells: https://developers.google.com/web-toolkit/doc/latest/DevGuideUiBinder#Rendering_HTML_for_Cells