How to dynamically hide and show html <tr> - razor-pages

How to add code in OnInitializedAsync to show/hide the in C# razor pages?
<div class="form-row">
<table border="0" cellpadding="2" style="border-style:solid;border-width:2px">
<tbody>
<tr id="rowId" style="display: none;">
<td>
<label>test</label>
</td>
</tr>
</tbody>
</table>
</div>
#code {
protected override void OnInitializedAsync()
{
// code to show <tr> with id="rowId"
}
}

With the following code, when the component is initialized the table row is hidden. When the button is pressed it becomes visible.
<div class="form-row">
<table border="0" cellpadding="2" style="border-style:solid;border-width:2px">
<tbody>
#if (_rowVisible)
{
<tr id="rowId">
<td>
<label>test</label>
</td>
</tr>
}
</tbody>
</table>
</div>
<button #onclick="OnHideRowBtnClicked">Hide row</button>
#code {
private bool _rowVisible;
protected override void OnInitializedAsync()
{
// set _rowVisible to true if row should be visible or false if row should be hidden
_rowVisible = true;
}
private void OnHideRowBtnClicked()
{
_rowVisible = false;
}
}
Another approach using the style attribute:
<div class="form-row">
<table border="0" cellpadding="2" style="border-style:solid;border-width:2px">
<tbody>
<tr id="rowId" style="#_rowStyle">
<td>
<label>test</label>
</td>
</tr>
</tbody>
</table>
</div>
<button #onclick="OnHideRowBtnClicked">Hide row</button>
<button #onclick="OnShowRowBtnClicked">Show row</button>
#code {
private string? _rowStyle;
protected override void OnInitializedAsync()
{
}
private void OnHideRowBtnClicked()
{
_rowStyle = "display: none;";
}
private void OnShowRowBtnClicked()
{
_rowStyle = null;
}
}

Related

How to get values dynamically from a collection in a model in Razor Pages?

I'm trying to render lists of elements dynamically in ASPNETCore Razor Pages but I can't find the way to show the values of properties using Display Helper. M
<table class="table datatable">
<thead>
<tr>
#foreach (var item in Model.GetFieldNamesForList())
{
<th>
#Html.DisplayName(item)
</th>
}
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.School) {
<tr>
#foreach (var propertyName in Model.GetFieldNamesForList())
{
<td>
// How to use #Html.Display using item from Model.School and the propertyName
</td>
}
<td>
<a asp-page="./Edit" asp-route-id="#item.Id" class="btn btn-primary btn-sm"><i class="material-icons md-16">edit</i></a>
<a asp-page="./Details" asp-route-id="#item.Id" class="btn btn-primary btn-sm"><i class="material-icons md-16">visibility</i></a>
<button action="submit" asp-page-handler="Delete" asp-route-id="#item.Id" class="btn btn-danger btn-sm"><i class="material-icons md-16">delete</i></button>
</td>
</tr>
}
</tbody>
</table>
In the meantime, I use reflection directly, but I would prefer use the Display method from helpers.
#item.GetType().GetProperty(propertyName).GetValue(item, null)

NextSibling property raises Object variable or With block variable not set

I have a working code on my side in which I have used NextSibling property in excel vba like that
stExecutive = thElements.Item(i).NextSibling.NextSibling.innerText
When a friend takes the code to try it on his side, the code raises the error Object variable or With block variable not set and I have fixed it using just one NextSibling
stExecutive = thElements.Item(i).NextSibling.innerText
Why this difference happen? and how can I make the code work on both sides without raising any errors?
This is part of the code
Set thElements = htmlCaseProcedures.querySelector(".table-striped").getElementsByTagName("th")
For i = 0 To thElements.Length - 1
If thElements.Item(i).innerText = sAvailableBalance Then
stAvailableBalance = thElements.Item(i).NextSibling.innerText
ElseIf thElements.Item(i).innerText = sFileStatus Then
stFileStatus = thElements.Item(i).NextSibling.innerText
ElseIf thElements.Item(i).innerText = sExecutive Then
stExecutive = thElements.Item(i).NextSibling.innerText
End If
Next i
and this is the content of html
<script type="text/javascript">
function doSubmitP(txtType){
document.frmTemp.reqtype.value = txtType;
$('#frmTemp').submit();
}
</script>
<style type="text/css">
th,td{
text-align: center !important;
}
</style>
<table class="table table-striped">
<tbody><tr align="center">
<th width="50%" align="right">الرقم الآلي</th>
<td><small>192833700</small></td>
</tr>
<tr align="center">
<th align="right">تاريخ التسجيل فى ملف التنفيذ</th>
<td><small>2020-03-09</small></td>
</tr>
<tr align="center">
<th align="right">رقم ملف التنفيذ</th>
<td><small>19283370</small></td>
</tr>
<tr align="center">
<th align="right">جهة التنفيذ</th>
<td><small>العاصمة-قسـم التنفيذشركات</small></td>
</tr>
<tr align="center">
<th align="right">الرصيد المتاح</th>
<td><small>0.000 د.ك</small></td>
</tr>
<tr align="center">
<th align="right">حالة الملف</th>
<td><small>مفتـــــوح</small></td>
</tr>
<tr>
<td align="center" colspan="2">
<table align="center" class="linksTbl">
<tbody><tr valign="middle">
<td><a class="hyperText" onclick="doSubmitP(1)" href="javascript:void(0);">الإجراءات العامة والرسوم</a></td>
<td>|</td>
<td><a class="hyperText" onclick="doSubmitP(2)" href="javascript:void(0);">إجراءات التنفيذ المدني</a></td>
<td>|</td>
<td><a class="hyperText" onclick="doSubmitP(3)" href="javascript:void(0);">الإجراءات المالية</a></td>
</tr>
</tbody></table>
</td>
</tr>
</tbody></table>
<div class="row" id="viewPaneDetails" style="display: none;"></div>
<form name="frmTemp" id="frmTemp" action="execReqView.jsp" method="POST" autocomplete="off">
<input name="reqtype" type="hidden">
</form>
<script>
$('#frmTemp').submit(function(){ // catch form submit event
$.ajax({ // create ajax call
data: $(this).serialize(), //get form data
type: $(this).attr('method'), //GET or POST
url: $(this).attr('action'), // the action url
success: function(response){ // on success
$('#viewPaneDetails').html(response); //update DIV
$('#viewPaneDetails').show();
}
});
return false; // cancel original event to prevent form submitting
});
</script>

Jquery datatable not working for first row in colspan

I tried to bind dynamically partial view its working but for jquery
datatable against first row what I specified below its not working.
#model IEnumerable<PurModelClass>
<table id="stock-audit-table" class="table table-striped table-bordered dt-responsive nowrap">
<thead>
<tr>
#*<th>Purchase From</th>
<th>Purchase Date</th>*#
<th>Item Name</th>
<th>Qty</th>
<th>Unit</th>
<th>Cost</th>
<th>Net Amount</th>
<th>Discount</th>
<th>Vat</th>
<th>Gross</th>
</tr>
</thead>
<tbody id="stock-audit-tbody">
#foreach (var item in Model)
{
<tr><td colspan="8">Invoice No: #item.PurInvNo Inv Date: #($"{#item.PurDate:MMM-dd-yyyy}") Vendor: #item.VendorName </td></tr>
foreach (var detail in item.PurDetails)
{
<tr>
<td>#detail.StockName</td>
<td>#detail.PurQty</td>
<td>#detail.UnitName</td>
<td>#detail.UnitPrice</td>
<td>#detail.Total</td>
<td>#detail.Discount</td>
<td>#detail.VAT</td>
<td>#detail.NetAmount</td>
</tr>
}
}
</tbody>
<tfoot>
</tfoot>
</table>
This the datatable script, is there any way to implement jquery datatable by demanding specific row with colspan properties.
<script type="text/javascript">
$(document)
.ready(function() {
$('#stock-audit-table').DataTable({
"autoWidth": false,
"order": [[0, "desc"]]
});
}
);
</script>
Try the rowGroup extension. You can use startRender and endRender to customize the summary data
$('#stock-audit-table').DataTable({
"autoWidth": false,
"order": [[0, "desc"]],
"rowGroup": {
dataSrc: 0,
startRender: function(groupName){
return $('<tr/>').append("<td colspan="8">TODO put data here</td>");
}
}
});
<tbody id="stock-audit-tbody">
#foreach (var item in Model)
{
foreach (var detail in item.PurDetails)
{
<tr>
<td>
<!-- Define a key to be referenced by the
'datasrc' property in dataTables -->
#item.GroupName
</td>
<td>#detail.StockName</td>
<td>#detail.PurQty</td>
<td>#detail.UnitName</td>
<td>#detail.UnitPrice</td>
<td>#detail.Total</td>
<td>#detail.Discount</td>
<td>#detail.VAT</td>
<td>#detail.NetAmount</td>
</tr>
}
}
</tbody>

Pass Id to ActionMethod

I have a Delete button inside my table and I m trying to delete the selected Row.
The problem is that I always get in the post method a null ID
<div>
<table class="table">
<thead>
<tr>
<th>Role Name</th>
<th>Action</th>
</tr>
</thead>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#using (Html.BeginForm("Delete","Role", new { id = item.Id },FormMethod.Post))
{
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-default" /> |
</div>
}
</td>
</tr>
}
</table>
In My Controller
// POST: Jobs/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult Delete(int id)
{
IdentityRole role = _context.Roles.Find(id);
_context.Roles.Remove(role);
_context.SaveChanges();
return RedirectToAction("Index");
}
Any time I click on the button the id is null
From your comments, the html generated in <form> tags is
action="/Role/Delete/b1bc13ca-a855-48b0-90e2-9e5fc081ac86"
meaning that the Id property of your model is typeof Guid. Change the POST method signature to
public ActionResult Delete(Guid id)

change color value with colorpicker

How do I change the bgcolor of 'colorId'. I tried the code below and I want the bgcolor to change in the value 'val'. But from then on I m doing something wrong.
<script type="text/javascript">
function updatevariable(elm) {
val = elm;
var divElement = document.getElementById(colorId);
divElement.bgcolor = val;
}
</script>
<table width="150" border="0" cellspacing="1" cellpading="0" align="center">
<tr>
<td bgcolor="#190707" onclick="updatevariable(this.bgColor)"> </td>
<td bgcolor="#fa5858" onclick="updatevariable(this.bgColor)"> </td>
<td bgcolor="#F4FA58" onclick="updatevariable(this.bgColor)"> </td>
<td bgcolor="#00FF00" onclick="updatevariable(this.bgColor)"> </td>
<td bgcolor="#fbefef" onclick="updatevariable(this.bgColor)"> </td>
</tr>
<tr>
<td id='colorId' bgcolor="#F4FA58"> </td>
</tr>
</table>
If you switch to jQuery you can do this in stead:
function updatevariable(elm){
var val = elm;
$('#colorId').css('background-color', val);
}
Just found your bugs. Use code below and it would work.
function updatevariable(elm) {
var val = elm;
var divElement = document.getElementById("colorId");
divElement.setAttribute("bgcolor", val);
}

Resources