Angular 5 Pass data on click event from parent component to child component on button clicked at parent component - angular5

i have some data bind in table and on click of any specific i want to show current clicked object more related data in to another component(child component)
for example the data I'm taking from this link:
http://jsonplaceholder.typicode.com/users
HTML Code:
<table>
<thead>
<th>
Id
</th>
<th>
name
</th>
<th>
username
</th>
<th>
email
</th>
<th>
street
</th>
<th>
suite
</th>
<th>
zipcode
</th>
<th>
phone
</th>
<th>
website
</th>
</thead>
<tbody>
<tr *ngFor="let data of httpdata">
<td>{{data.id}}
</td>
<td>{{data.name}}
</td>
<td>{{data.username}}
</td>
<td>{{data.email}}
</td>
<td>{{data.address.street}}
</td>
<td>{{data.address.city}}
</td>
<td>{{data.address.suite}}
</td>
<td>{{data.address.zipcode}}
</td>
<td>{{data.phone}}
</td>
<td>{{data.website}}
</td>
<td>
<a routerLink="/conflict-details/conflict" (click)="onSelect(data)">Go to
</a>
</td>
</tr>
</tbody>
</table>
if you see there is one go to button i have in table when i click on any specific data it should show me complete info about current clicked
but in my case i want to bind the data in another component when i click on go to for specific that all td data should be display in new component(child).
simple i want to track click event for selected data in child component . and the table is rendered in parent component.
attached is the data table I have.

You can use the #Input and #Output decorator to achieve the required output:
Changes:
In parent Component:
HTML Code:
<div>
<table *ngIf="isVisible === true">
<tr>
<th>
Id
</th>
<th>
name
</th>
<th>
username
</th>
<th>
email
</th>
</tr>
<tr *ngFor="let data of userInformation">
<td>{{data.id}}
</td>
<td>{{data.name}}
</td>
<td>{{data.username}}
</td>
<td>{{data.email}}
</td>
<td>
<a (click)="onSelect(data)">Go to
</a>
</td>
</tr>
</table>
<div *ngIf="isVisible === false">
<app-test-child [userInfo]="clickedUser" (notify)="backToList($event)"></app-test-child>
</div>
</div>
TS Code:
Local variables:
userInformation: any;
isVisible : boolean = true;
clickedUser: any;
Two functions in the parent component:
onSelect(data)
{
this.isVisible = false;
this.clickedUser = data;
}
backToList(flag) {
this.isVisible = flag;
console.log(flag)
}
In Child Component:
HTML code:
<table>
<tr>
<th>
Id
</th>
<th>
name
</th>
<th>
username
</th>
<th>
email
</th>
</tr>
<tr>
<td>{{clickedUser.id}}
</td>
<td>{{clickedUser.name}}
</td>
<td>{{clickedUser.username}}
</td>
<td>{{clickedUser.email}}
</td>
<td>
<a (click)="backToList()">Back
</a>
</td>
</tr>
</table>
TS Code:
import { Component, OnInit, Input, EventEmitter, Output } from '#angular/core';
#Input() userInfo: any;
#Output() notify: EventEmitter<any> = new EventEmitter<any>();
clickedUser: any;
constructor() { }
ngOnInit() {
this.clickedUser = this.userInfo;
}
backToList() {
var flag = true;
this.notify.emit(flag);
}

try this
In HTML
update
<a routerLink="/conflict-details/conflict" (click)="onSelect(data)">Go to
</a>
to
<a (click)="onSelect(data)">Go to
</a>
In Parent Component
import { Router } from '#angular/router';
export class ParentComponent implements OnInit {
constructor(private router: Router) {
}
onSelect(data) {
this.router.config.find(r => r.component == ChildComponent).data = data;
this.router.navigate(["/conflict-details/conflict"]);
}
}
In Child Component
import { ActivatedRoute } from '#angular/router';
export class ChildComponent implements OnInit {
SentItem : any;
constructor(private router: ActivatedRoute) { }
ngOnInit() {
this.router.data.subscribe(r=>this.SentItem =r);
}
}

Related

change button switch toggle in vuejs

I have a button that when I click it changes the status of the record, but when I update, the color of the button does not change, if I refresh the page if it changes, how can I make the button change when I update the record
Table:
<div class="card-body">
<table id="tblProfiles" class="table table-bordered table-hover dt-responsive">
<thead>
<tr>
<th>Description</th>
<th>Status</th>
<th>Options</th>
</tr>
</thead>
<tbody>
<tr v-for="(profile, index) in profiles" :key="index">
<td>{{ profile.description }}</td>
<td>
<button
class="btn-sm"
:class="[profile.status == 1 ? 'btn-success' : 'btn-danger']"
#click="statusUpdate(profile.id)"
>{{profile.status == 1 ? 'Active' : 'Inactive'}}
</button>
</td>
<td>
<button class="btn btn-info btn-sm">Details</button>
</td>
</tr>
</tbody>
</table>
</div>
Method:
// ...
statusUpdate: async function(id) {
try {
const response = await profileService.activateDesactivate(id);
console.log(response);
} catch (error) {
console.log(error);
}
}
Send profile object insted of id prop:
<div class="card-body">
<table id="tblProfiles" class="table table-bordered table-hover dt-responsive">
<thead>
<tr>
<th>Description</th>
<th>Status</th>
<th>Options</th>
</tr>
</thead>
<tbody>
<tr v-for="(profile, index) in profiles" :key="index">
<td>{{ profile.description }}</td>
<td>
<button
class="btn-sm"
:class="[profile.status == 1 ? 'btn-success' : 'btn-danger']"
#click="statusUpdate(profile)"
>{{profile.status == 1 ? 'Active' : 'Inactive'}}
</button>
</td>
<td>
<button class="btn btn-info btn-sm">Details</button>
</td>
</tr>
</tbody>
</table>
</div>
Method receives profile objetc and update it as you want. In your case status prop changes the color class of your button.
statusUpdate: async function(profile) {
try {
const response = await profileService.activateDesactivate(profile.id);
profile.status = 1;
console.log(response);
} catch (error) {
console.log(error);
}
},

How to multiply two columns in a table together to produce a new column

I have 2 fields quantity and price. I basically want to multiply them together and get a value for another column called Price (Which is the total of the multiplication).
I have tried using the below html code:
#Html.DisplayFor(modelItem => item.item_order_quantity*item.ITEM.item_price)
This is my Table row code:
<table class="table table-striped table-advance table-hover">
<tbody>
<tr>
<th><i class="icon_pin_alt"></i> Item Description</th>
<th><i class="icon_pin_alt"></i> Quantity</th>
<th><i class="icon_calendar"></i> Price</th>
</tr>
#foreach (var item in Model.ITEM_ORDER)
{
<tr>
<td style="width:auto">
#Html.DisplayFor(modelItem => item.ITEM.item_description)
</td>
<td style="width:auto">
#Html.DisplayFor(modelItem => item.item_order_quantity)
</td>
<td style="width:auto">
#Html.DisplayFor(modelItem => item.item_order_quantity*item.ITEM.item_price)
</td>
<td>
<div class="btn-group">
#Html.ActionLink("View", "Details", new { id = item.OrderID }) |
#Html.ActionLink("Edit", "Edit", new { id = item.OrderID }) |
#Html.ActionLink("Delete", "Delete", new { id = item.OrderID })
</div>
</td>
</tr>
}
</tbody>
</table>
Consider having an additional property in your viewmodel or Model which does this task for you. Then you can bind this with html helper as you did for every other field.
public class YourViewModel
{
public int Field1{ get; set; }
public int Field2{ get; set; }
public int CalculatedField{
get {return Field1*Field2;}
}
}
Or try below code which calculates the value and stores in variable, then renders value directly from variable.
try this
<table class="table table-striped table-advance table-hover">
<tbody>
<tr>
<th><i class="icon_pin_alt"></i> Item Description</th>
<th><i class="icon_pin_alt"></i> Quantity</th>
<th><i class="icon_calendar"></i> Price</th>
</tr>
#foreach (var item in Model.ITEM_ORDER)
{
var computedValue = item.item_order_quantity*item.ITEM.item_price
<tr>
<td style="width:auto">
#Html.DisplayFor(modelItem => item.ITEM.item_description)
</td>
<td style="width:auto">
#Html.DisplayFor(modelItem => item.item_order_quantity)
</td>
<td style="width:auto">
#(computedValue)
</td>
<td>
<div class="btn-group">
#Html.ActionLink("View", "Details", new { id = item.OrderID }) |
#Html.ActionLink("Edit", "Edit", new { id = item.OrderID }) |
#Html.ActionLink("Delete", "Delete", new { id = item.OrderID })
</div>
</td>
</tr>
}
</tbody>
</table>

Issue Saving Edited Data: DDL Value can not be null

I am having an issue with saving the edited fields in my MVC project. When I click the submit button I get an error
Value cannot be null.
Parameter name: items
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: items
Source Error:
Line 63:
Line 64:
Line 65: #Html.DropDownListFor(model => model.EventTypeID, new SelectList(ViewBag.EventTypes, "EventTypeID", "EventType1"), "Choose")
Line 66:
Line 67:
I have tried some things that I have seen on the internet but it doesn't seem to be doing anything for me :( Help would be appreciated thank you!
this is my controller When I put a break point here it never triggers.
[HttpPost]
public ActionResult IndexPST(Event_Setup es)
{
db.Event_Setup.Add(es);
db.SaveChanges();
return View("Index",es);
}
And this is my View
#model DixonGroupInc.Models.Event_Setup
<link href="~/Content/EventManagement.css" rel="stylesheet" />
#using (Html.BeginForm("IndexPST", "EventManagement", FormMethod.Post))
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<div id="cntr">
<div id="emLoc">
<table id="ldTbl">
<thead>
<tr>
<th class="ln">
<img src="~/Images/4.gif" />
</th>
<th>
<img src="~/Images/5.gif" />
</th>
<th>
<img src="~/Images/5.gif" />
</th>
<th>
<img src="~/Images/5.gif" />
</th>
<th>
<img src="~/Images/5.gif" />
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="chosen space">EventMetadata
</td>
<td class="space">Client / Vendor
</td>
<td class="space">Venu info
</td>
<td class="space">Sponsored Participants
</td>
<td class="space">Event Services
</td>
</tr>
</tbody>
</table>
</div>
<div id="emSetup">
Setup Meeting
</div>
<div id="emTable">
<table class="myTbl">
<tr>
<td class="lblEvent clm1">
#Html.Label("Event Type")
</td>
<td class="clm2">
#Html.DropDownListFor(model => model.EventTypeID, new SelectList(ViewBag.EventTypes, "EventTypeID", "EventType1"), "Choose")
</td>
<td class="clm3">
#{Html.RenderAction("EventType", "EventManagement");}
</td>
</tr>
<tr class="rows">
<td class="lblEvent clm1">
#Html.Label("Event Title")
</td>
<td class="clm2">
#Html.EditorFor(model => model.EventTitle)
#Html.ValidationMessageFor(model => model.EventTitle)
</td>
</tr>
<tr>
<td class="lblEvent clm1">
#Html.Label("Event Identifier")
</td>
<td class="clm2">
#Html.EditorFor(model => model.EventIdentifier)
#Html.ValidationMessageFor(model => model.EventIdentifier)
</td>
</tr>
</table>
<table class="dateTbl">
<tr class="rows">
<td class="lblEvent clm1">
#Html.Label("Event Date From")
</td>
<td class="clm2">
#Html.TextBoxFor(model => model.EventDateFrom, new { #class = "dateFrom" })
#Html.ValidationMessageFor(model => model.EventDateFrom)
</td>
<td class="lblEvent">
#Html.Label("To")
</td>
<td class="clm4">
#Html.TextBoxFor(model => model.EventDateTo, new { #class = "dateTo" })
#Html.ValidationMessageFor(model => model.EventDateTo)
</td>
</tr>
</table>
<table class="myTbl">
<tr>
<td class="lblEvent clm1">
#Html.Label("Event Description")
</td>
<td class="clm2">
#Html.TextAreaFor(model => model.EventDescription, new
{
id = "taED"
})
<div>
<span id="charLeft2"></span>characters remaining.
</div>
#Html.ValidationMessageFor(model => model.EventDescription)
</td>
</tr>
<tr class="rows">
<td class="lblEvent clm1">
#Html.Label("Custom Message")
</td>
<td class="clm2">
#Html.TextAreaFor(model => model.CustomMessage, new
{
id = "taCM"
})
<div>
<span id="charLeft1"></span>characters remaining.
</div>
#Html.ValidationMessageFor(model => model.CustomMessage)
</td>
</tr>
<tr>
<td class="lblEvent clm1">
#Html.Label("Instructions")
</td>
<td class="clm2">
#Html.TextAreaFor(model => model.Instructions, new
{
id = "taInstrct"
})
<div>
<span id="charLeft"></span>characters remaining.
</div>
#Html.ValidationMessageFor(model => model.Instructions)
</td>
</tr>
</table>
<table class="myTbl">
<tr class="rows">
<td>
#{Html.RenderAction("AddTrack", "EventManagement");}
</td>
</tr>
<tr>
<td>
#{Html.RenderAction("EventTrack", "EventManagement");}
</td>
</tr>
</table>
<table class="myTbl">
<tr class="rows">
<td class="lblEvent clm1">
#Html.Label("CFP Pocess")
</td>
<td class="clm2">
#Html.RadioButtonFor(model => model.CFPRequired, "true") Yes
</td>
<td class="clm3">
#Html.RadioButtonFor(model => model.CFPRequired, "false") No
</td>
</tr>
</table>
</div>
<div id="emSubmit">
<input type="submit" value="Save & Next" id="submit" />
</div>
</div>
}
Viewbag can be unreliable so generally it isn't recommended to pass your drop down list data through the viewbag. I would recommend adding it to your view model instead. In your Event_Setup class make sure that you have
public int EventTypeID { get; set; }
and
public List<SelectListItem> EventTypeList { get; set; }
on your controller get you can set EventTypeID to set a default on the drop down so your controller should have something like
Event_Setup es = new Event_Setup();
es.EventTypeID = //default value if you want;
es.EventTypeList = //Method call here where List<SelectListItem> is returned with your event list
then on your view you can change your drop down to
#Html.DropDownListFor(x => x.EventTypeID, Model.EventTypeList)

How to change MVC Web Grid structure?

I am trying to show list of items in a MVC 4 Web Grid control. But I am unable to re-format my web grid as table structure below.
Web Grid structure like this.
Table structure like this.
Here is my razor code for web grid.
#{
var grid = new WebGrid(Model.EnrollPlanPriceLevelList, canPage: true, rowsPerPage: 10, selectionFieldName: "id", ajaxUpdateContainerId: "gridContent");
grid.Pager(WebGridPagerModes.NextPrevious);
if (grid.HasSelection)
{
var EnrollPlanPriceLevelID = grid.Rows[grid.SelectedIndex].Value;
}
}
#grid.GetHtml(
tableStyle: "table table-condensed table-striped table-hover no-margin",
headerStyle: "",
mode: WebGridPagerModes.All,
columns:
grid.Columns(
grid.Column("", format: (item) => item.GetSelectLink(item.EnrollPlanPriceLevelID.ToString())),
grid.Column("SchoolYears.Name", "School Year"),
grid.Column("EnrollPlans.PlanName", "Enroll Plan"),
grid.Column("PriceLevels.Name", "Price Level"),
grid.Column("Price", "Price"),
grid.Column("", format: #<text>#Html.Label("Edit", new { #id = item.EnrollPlanPriceLevelID, #class = "badge", #onclick = "bindplanprogram(" + item.EnrollPlanPriceLevelID + ")" }) </text>),
grid.Column("", format: #<text>#Html.ActionLink("Delete", "Delete", new { id = item.EnrollPlanPriceLevelID }, new { #class = "badge" }) </text>)
))
Here is the table structure
<table class="table table-condensed table-striped table-hover no-margin">
<thead>
<tr>
<th>School Year
</th>
<th>Enroll Plan</th>
<th>
<span class="span3">Early Bird</span>
<span class="span3">Value Pricing</span>
<span class="span4">Standard Pricing</span>
</th>
<th>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>2011-2012</td>
<td>A(k-8) Grade</td>
<td>
<span class="span3">$ 50</span>
<span class="span3">$ 60</span>
<span class="span3">$ 70</span>
</td>
<td>
<a class="badge">Edit</a>
<a class="badge">Delete</a>
</td>
</tr>
<tr>
<td>2011-2012</td>
<td>B (9-12)Grade</td>
<td>
<span class="span3">$ 70</span>
<span class="span3">$ 80</span>
<span class="span3">$ 90</span>
</td>
<td>
<a class="badge">Edit</a>
<a class="badge">Delete</a>
</td>
</tr>
<tr>
<td>2011-2012</td>
<td>Adult Ed. (age 18+)</td>
<td>
<span class="span3">$ 1500</span>
<span class="span3">$ 175</span>
<span class="span3">$ 200</span>
</td>
<td>
<a class="badge">Edit</a>
<a class="badge">Delete</a>
</td>
</tr>
</tbody>
</table>
I want to reformate my web grid structure as above table structure. Please tell me someone! How to change web grid structure accordingly?
The only way to achieve what you want with the WebGrid is to change the shape of the data you are passing in. Whether you do that in your SQL or whether you do that using LINQ to create a collection of anonymous types that have the appropriate properties is up to you, but the end result must be a collection of items that have public properties that match what you want as column names.

Accordion within table rows

I am using Twitter Bootstrap. Currently I am working on Master Details looks alike table, which I am planning to place an accordion into each table's row and if it is expanded, the partial view will be reload and displayed.
Somehow it does not work as expected but if I change it to Modal, the partial view loaded perfectly.
This is the controller that process and returning the values:
public ActionResult Index(int id,int? page)
{
List<CustomerProduct> customerProducts =
(from c in RepositoryProduct.Reload(id)
select c).ToList<CustomerProduct>();
return PartialView("_CustomerProducts", customerProducts);
}
and this is the table in the view :
<table id="tbl" class="table table-condensed" style="border-collapse:collapse;">
<thead>
<tr>
<th>
#Html.ActionLink("Customer Name", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
#Html.DisplayNameFor(model => model.First().CustLocalName)
</th>
<th>
#Html.ActionLink("Country", "Index", new { sortOrder = ViewBag.CountrySortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
#Html.DisplayNameFor(model => model.First().City)
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr data-toggle="collapse">
<td style="display:none;">
#Html.DisplayFor(modelItem => item.CustID)
</td>
<td>
#Html.DisplayFor(modelItem => item.CustName)
</td>
<td>
#Html.DisplayFor(modelItem => item.CustLocalName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Country.CountryName)
</td>
<td>
#Html.DisplayFor(modelItem => item.City)
</td>
<td>
<a class="btn btn-small btn-info" type="button" href="#Url.Action("Index", "CustomerProduct", new { id = item.CustID })" data-toggle="collapse" data-target="##item.CustID" >Products</a>
</td>
</tr>
<tr>
<td colspan="6" class="hiddenRow">
<div id="#item.CustID" class="accordian-body collapse" tabindex="-1">
<div class="accordion-header">
<label>
<strong>#item.CustName product(s)</strong>
</label>
</div>
<div class="accordion-body">TEST</div>
</div>
</td>
</tr>
}
</tbody>
</table>
this is part of the code that calling the controller Index function :
<a class="btn btn-small btn-info" type="button" href="#Url.Action("Index", "CustomerProduct", new { id = item.CustID })" data-toggle="collapse" data-target="##item.CustID" >Products</a>
Actually I would like to load the partialview(_CustomerProduct) in the div class="accordion-body" as what I did in div class="modal-body" but no success. May I know if it is possible to do it and if yes, may I know how?
Any help will be appreciated.
for me to get the view to work i used
<td colspan="6" class="hiddenRow">
<div id="#item.CustID" class="accordian-body collapse" tabindex="-1">
<div class="accordion-header">
<label>
<strong>#item.CustName product(s)</strong>
</label>
</div>
<div class="accordion-body">
#Html.Partial("_CustomerProduct")
</div>
</div>
</td>