Display class content after clicking on a button reactjs - html

I am using reactjs, and I have two classes Table and APP each one is in an individual file(Table.js and App.js); in my App.js I wrote an html code where I have a button after clicking on that button I want to display Table content which is inside a render. I tried doing: <Button onClick={<Table/>}, but it doesn't work. Any suggestions?
import React, { Component } from 'react'
import './Table.css'
class Table extends Component {
constructor(props) {
super(props)
this.state = {
Offers: [
{ name: 'Name1', website: 25 },
{ name: 'Name2', website: 19},
{ name: 'Name3', website: 16},
{ name: 'Name4', website: 25}
],
}
}
renderTableHeader() {
let header = Object.keys(this.state.Offers[0])
return header.map((key, index) => {
return <th key={index}>{key.toUpperCase()}</th>
})
}
renderTableData() {
return this.state.Offers.map((offer, index) => {
const { name, website} = offer
return (
<tr >
<td>{name}</td>
<td>{website}</td>
</tr>
)
})
}
render() {
return (
<div>
<h1 id='title'>Start-ups List Table</h1>
<table id='offers'>
<tbody>
<tr>{this.renderTableHeader()}</tr>
{this.renderTableData()}
</tbody>
</table>
</div>
)
}
}
export default Table
import React, { Component} from 'react';
import {
Container, Col, Form,
FormGroup, Label, Input,
Button, Dropdown,
} from 'reactstrap';
import Axios from "axios";
import './App.css';
import Table from './Table';
class App extends Component {
render(){
return (
<div>
<div className="box">
<div className="element sourcedropdown" >
<label>Source :</label>
<select className="form-control" name="Sources" onChange={this.handleChange}>
<option selected>Select Source</option>
<option value="1">src1</option>
<option value="2">src2</option>
<option value="3">src3</option>
</select>
</div>
<div className="element Techdropdown">
<label>Technology :</label>
<select className="form-control" name="Tech" onChange= {this.handleChange}>
<option selected>Select Technology</option>
<option className="dropdown-item" value="1">tech1</option>
<option className="dropdown-item" value="2">tech2</option>
<option className="dropdown-item" value="3">tech3</option>
</select>
</div>
<div id="contrat" className="element contrat">
<label>Contract :</label>
<select className="form-control" name="Tech" onChange= {this.handleChange}>
<option selected>Select contract</option>
<option className="dropdown-item" value="1">V1</option>
<option className="dropdown-item" value="2">V2</option>
<option className="dropdown-item" value="3">V3</option>
<option className="dropdown-item" value="3">V4</option>
</select>
</div>
<div id="checkbox" className ="element checkbox">
<label check>
<input type="checkbox" onChange= {this.handleChange} />{' '}
Remote
</label>
</div>
<Button className="button" onClick={<Table/>}>Submit</Button> //here where i want to call Table
</div>
</div>
);
}
}
export default App;

You have placed Table rendering inside onClick method callback. The onClick is method is just a callback. You have to conditionally display Table based on a state as your are displaying other tags. You can changed the state based on Button click.
https://codesandbox.io/s/morning-hooks-ie74g?file=/src/App.js:0-2760
import React, { Component } from "react";
import {
Container,
Col,
Form,
FormGroup,
Label,
Input,
Button,
Dropdown
} from "reactstrap";
import Axios from "axios";
import Table from "./Table";
class App extends Component {
constructor (props) {
super(props);
this.state = {
displayTable: false
};
}
render () {
return (
<div>
<div className="box">
<div className="element sourcedropdown">
<label>Source :</label>
<select
className="form-control"
name="Sources"
onChange={this.handleChange}
>
<option selected>Select Source</option>
<option value="1">src1</option>
<option value="2">src2</option>
<option value="3">src3</option>
</select>
</div>
<div className="element Techdropdown">
<label>Technology :</label>
<select
className="form-control"
name="Tech"
onChange={this.handleChange}
>
<option selected>Select Technology</option>
<option className="dropdown-item" value="1">
tech1
</option>
<option className="dropdown-item" value="2">
tech2
</option>
<option className="dropdown-item" value="3">
tech3
</option>
</select>
</div>
<div id="contrat" className="element contrat">
<label>Contract :</label>
<select
className="form-control"
name="Tech"
onChange={this.handleChange}
>
<option selected>Select contract</option>
<option className="dropdown-item" value="1">
V1
</option>
<option className="dropdown-item" value="2">
V2
</option>
<option className="dropdown-item" value="3">
V3
</option>
<option className="dropdown-item" value="3">
V4
</option>
</select>
</div>
<div id="checkbox" className="element checkbox">
<label check>
<input type="checkbox" onChange={this.handleChange} /> Remote
</label>
</div>
<Button
className="button"
onClick={() => {
this.setState({ displayTable: true });
}}
>
Submit
</Button>
</div>
{this.state.displayTable ? <Table /> : ""}
</div>
);
}
}
export default App;

Related

How to add back element after i remove it? using .removeAttr in Jquery

HI i have a example with me when if select the drop down list - price by recyclables a hidden field apppear but how can i when select the Service charger the hidden field disappear
I have try using .attr to add back but it seems to be not working
$("#select-price-mode").change(function() {
if (this.value === "Price By Recyclables") {
$('.col').removeAttr('hidden');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-row">
<div class="col">
<label for="select-price-mode" class="col-form-label">Price Mode</label>
<select class="select-price-mode custom-select-sm col-10" id="select-price-mode" required>
<option selected disabled value="">Select ....</option>
<option value="Price By Recyclables">Price By Recyclables</option>
<option value="Service Charger">Service Charger</option>
</select>
</div>
<div class="col" hidden>
<label for="select-payment-frequency" class="col-form-label">Payment Frequency</label>
<select class="select-payment-frequency custom-select-sm col-10" id="select-payment-frequency" required>
<option selected disabled value="">Select ....</option>
</select>
</div>
i think this will solve your issue
$("#select-price-mode").change(function() {
if (this.value === "Price By Recyclables") {
$('.col').removeAttr('hidden');
} else {
$('.col').attr('hidden', '');
}
});

Show & hide html element based on selected text using jquery

I want to set attribute name & show select option if the selected text is not "Super Admin" and unset the attribute name & hide the select option if the selected text is "Super Admin".
<div class="form-group">
<select id="role_user" name="role" class="form-control" required>
<option value="">Select role user</option>
<option value="Admin">Admin</option>
<option value="Supervisor">Supervisor</option>
<option value="Super Admin">Super Admin</option>
</select>
</div>
The element that I want to show & hide:
<div class="form-group" id="company" style="display: none">
<label>Company</label>
<select id="company_i" class="form-control">
<option value="">Select Company</option>
#foreach ($company as $cp)
<option value="{{ $cp->id }}">{{ $cp->name }}</option>
#endforeach
</select>
</div>
JQuery code:
$(document).ready(function() {
$('#role_user').change(function() {
if ($('#role_user option:selected').text() != "Super Admin") {
$('#company').show();
$('#company_i').attr('name', 'company_id');
} else if ($('#role_user option:selected').text() == "Super Admin") {
$('#company').hide();
$('#company_i').removeAttr("name");
}
})
});
I have tried it using the code above, but only the set & remove name attribute are working. The show & hide not working.
$(document).ready(function () {
$('#role_user').change(function () {
if ($(this).val() != "Super Admin") {
$('#company').show();
$('#company_i').attr('name', 'company_id');
} else {
$('#company').hide();
$('#company_i').removeAttr("name");
}
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group">
<select id="role_user" name="role" class="form-control" required>
<option value="">Select role user</option>
<option value="Admin">Admin</option>
<option value="Supervisor">Supervisor</option>
<option value="Super Admin">Super Admin</option>
</select>
</div>
<div class="form-group" id="company" style="display: none">
<label>Company</label>
<select id="company_i" class="form-control">
<option value="">Select Company</option>
</select>
</div>

Component not rendering on Angular, It shows no error

I am trying to upload the product details to which I am trying to get the values from the form.
I always face problem with handling forms with angular. Other components are rendering with no problem.
This is the component.html code:
<form>
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="title" id="title" class="form-control" #title/>
</div>
<div class="form-group">
<label for="category">Category</label>
<select name="category" id="category" class="form-control" #category>
<option value="cardio">Cardio</option>
<option value="strength">Strength</option>
<option value="strength">Cross Training</option>
<option value="strength">Outdoor</option>
<option value="strength">Home equipment</option>
</select>
</div>
<div class="form-group">
<label for="imported">Imported</label>
<select name="category" id="category" class="form-control" #imported>
<option value="International">International</option>
<option value="Indian">Indian</option>
</select>
</div>
<div class="form-group">
<label for="type">Type</label>
<input type="text" name="type" id="type" class="form-control" #type/>
</div>
<div class="form-group">
<label for="description">Description</label>
<input name="description" id="description" class="form-control" #description/>
</div>
<div class="form-group">
<label for="product-img">Image of the product</label>
<input type="file" (change)="onFileChange($event)" class="form-control"/>
</div>
<button type="button" (click)="uploadProduct(title.value, category.value, imported.value, type.value, description.value)" class="button-theme">Submit</button>
</form>
This is the component.ts code,
import { Component, OnInit } from '#angular/core';
import { Product } from '../model/product';
import { ProductsService } from '../products.service';
import { FormControl, FormGroup } from '#angular/forms';
import {FormsModule,ReactiveFormsModule} from '#angular/forms';
#Component({
selector: 'app-addproduct',
templateUrl: './addproduct.component.html',
styleUrls: ['./addproduct.component.css']
})
export class AddproductComponent implements OnInit {
product: Product;
constructor(private helper: ProductsService) { }
ngOnInit(): void {
}
onFileChange(event){
var data = {
"file": event.target.file
}
this.helper.uploadProduct(data).subscribe(d => {
this.product.path = d;
});
}
uploadProduct(title: string, category: string, imported: string, type: string, description: string){
this.product.mapProduct(title, description, type, category, imported);
this.helper.saveProduct(this.product);
}
}
TIA! :)

How to hide a label of a dropdown with hide() function

I have 2 dropdowns (EmployeeType, Type) and a textbox (Rate) and I just want to hide "Type" and "Rate" according to the selected value of "EmployeeType". I've done it with no issues to the "Rate", but for the "Type" only the dropdown hides not the label. Can anyone help me to hide the label of the dropdown as well?
<div class="control-group">
<label class="control-label" for="Designation">Employee Type</label>
<div class="controls">
<select name="Designation" onchange="ajaxRate(this)" id="Designation">
<option value="">--Select Employee Type--</option>
<option value="Center">Center</option>
<option value="Visiting">Visiting</option>
<option value="Other">Other</option>
</select>
<span id="ajax_img3"></span>
</div>
<!-- designation-->
<br/>
<!-- 2019-03-18 Visiting Type -->
<div class="control-group">
<label class="control-label" for="Type">Type</label>
<div class="controls">
<select name="Type" id="TypeValue">
<option value="">--Select Type--</option>
<option value="Yes">Regular</option>
<option value="No">Visiting</option>
</select>
</div>
</div>
<div class="control-group" id='rate'>
<label class="control-label" for="Rate">Hourly Rate</label>
<div class="controls">
<input type="number" name="Rate" id='rateValue'/>
</div>
</div>
#include('includes.footer')
<script>
$(document).ready(function () {
$("#rate").hide();
// $("#Type").hide();
});
function ajaxRate(x) {
if (x.value == 'Center') {
$("#rate").hide();
$("#rateValue").val('0');
$("#TypeValue").show();
} else {
$("#rate").show();
$("#rateValue").val('0');
$("#TypeValue").hide();
}
}
$( document ).ready(function()
{
$("#rate").hide();
// $("#Type").hide();
});
function ajaxRate(x)
{
if(x.value=='Center')
{
$("#rate").hide();
$("#rateValue").val('0');
$("#TypeValue").show();
$("#TypeValue").parents(".control-group").show();
}
else
{
$("#rate").show();
$("#rateValue").val('0');
$("#TypeValue").hide();
$("#TypeValue").parents(".control-group").hide();
}
}
You have to hide full div using parents or you have to add any id to main div to hide.
function ajaxRate(x) {
if(x.value=='Center')
{
$("#rate").hide();
$("#rateValue").val('0');
$("#TypeValue").closest('div.control-group').show();
}
else
{
$("#rate").show();
$("#rateValue").val('0');
$("#TypeValue").closest('div.control-group').hide();
}
}

HTML Layout Cleanup?

I making a jsp which ncludes 5 jsp's all together. On my 4th one..it's operational but.. my design looks a lil clunky. Can you point out any suggestions to make it look better or cleaner? Any suggestions would be appreciated. Thanks
CSS
table.det
{ }
table.det td
{
border: none;
}
fieldset.det
{
display:block;
float:left;
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:12px;
margin-top:20px;
margin-left:20px;
border:2px solid #CCCCCC;
}
fieldset.det legend
{
padding:2px 5px;
border:2px solid #CCCCCC;
}
jsp:
<tr>
<th>
<span onclick="toggleDiv('filtering', 'filterImg')" style="cursor: hand;">Filters <img name="filterImg" src="../images/minus.gif" /></span>
</th>
</tr>
<tr>
<td>
<div id="filtering" style="display:block;">
<table class ="det">
<tr>
<td>
Workload Year: <select size="1" name="wYearSelect">
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option selected value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
</select>
Fiscal Year: <select size="1" name="wYearSelect">
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option selected value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
</select>
Condition Code: <select size="1" name="condCodeSelect">
<option selected value=""> </option>
<option value="2007">A</option>
<option value="2008">B</option>
<option value="2009">C</option>
<option value="2010">D</option>
<option value="2011">E</option>
<option value="2012">F</option>
<option value="2013">G</option>
<option value="2014">H</option>
</select>
Customer: <select size="1" name="customerSelect">
<option selected value=""> </option>
</select>
Repair Facility: <select size="1" name="repFacSelect">
<option selected value=""> </option>
</select><br/>
Work Type: <select size="1" name="workTypeSelect">
<option selected value=""> </option>
<option value="10">Rebuild</option>
<option value="11">IROAN</option>
<option value="12">SOAR</option>
<option value="13">Special</option>
</select>
Special Indicator: <select size="1" name="specIndSelect">
<option selected value=""> </option>
<option value="RST">RESET</option>
</select>
WBS: <select size="1" name="wbsSelect">
<option selected value=""> </option>
<!-- etc -->
</select>
<br/>
Appropriation: <select size="1" name="appropSelect">
<option selected value=""> </option>
</select>
MWSLIN: <input type="text" name="mwslin" value=""/>
Serial #: <input type="text" name="mwslin" value=""/>
<input type="checkbox" name="include_carryover">Include Carryover</input>
</td></tr><tr><td>
<fieldset>
<legend>Standard Materiel filter structure here</legend>
NSN: <input type="text" name="nsn" value=""/>
TAMCN: <input type="text" name="tamcn" value=""/>
PGD/Group: <input type="text" name="pgd" value=""/>
SAC : <select size="1" name="condCodeSelect">
<option selected value=""> </option>
<option value="2011">1</option>
<option value="2012">2</option>
<option value="2013">3</option>
</select>
<br/>
Nomenclature: <input type="text" name="nomen" value=""/>
</fieldset>
</td></tr><tr><td>
<fieldset class="det">
<legend>Source of Repair</legend>
<input type="checkbox" name="sorCodes" value="A">MCA </input>
</fieldset>
<fieldset class="det">
<legend>Schedule Type</legend>
<input type="checkbox" name="schedType" value="1">Regular</input><br/>
<input type="checkbox" name="schedType" value="2">Lot Job</input><br/>
<input type="checkbox" name="schedType" value="3">Batch Job</input>
</fieldset>
<fieldset class="det">
<legend>Workload Type</legend>
<input type="checkbox" name="workloadType" value="1">SMC Workload</input><br/>
<input type="checkbox" name="workloadType" value="2">Other Workload</input>
</fieldset>
<fieldset class="det">
<legend>Closure Level</legend>
<input type="checkbox" name="workloadType" checked value="1">Open</input><br/>
<input type="checkbox" name="workloadType" value="1">Physically Complete</input><br/>
<input type="checkbox" name="workloadType" value="2">Financially Complete</input>
</fieldset>
</td></tr></table>
</div>
</td>
</tr>
<tr>
<td align="center">
<button name="Generate Report" value="Generate Report">Generate Report</button>
<button name="Clear Criteria" value="Clear Criteria">Clear Criteria</button>
</td>
</tr>
At a first glance, getting rid of the tables would be somewhere to start if you can.
I am headed to work will look over it more thoroughly there.
Cheers!
EDIT - You could take out the border on your fieldset.det legend, it should cascade down from fieldset.det
On your div id=filtering, couldn't you add the extra styling to the filtering id?
EDIT - Here is something I threw together, not saying it is ANYWHERE near perfect. It gives you a div layout and ways to easily change the layout through CSS. Also, check out this site for best form practices, http://blog.sherpawebstudios.com/2009/06/17/top-10-html-form-layout-best-practices/.
CSS:
#wrapper{ width: 1040; overflow:hidden; }
#filter {
cursor: hand;
font-size: 20px;
font-weight: bold;
margin-bottom: 10px;
}
.formLabel {
font-weight: bold;
text-align: right;
width: 160px;
float: left;
}
.field {
float: left;
}
.content:before,
.content:after {
content: "";
display:table;
}
.content:after {
clear: both;
}
/*IE 6/7 */
.content { zoom:1;margin-bottom: 10px;}
.subset {
float: left;
margin: 0 15;
}
.subset div.formLabel {
width:100px;
}
#generate {
margin-right:15px;
}
HTML:
<div id = "wrapper" >
<div id = "filter" class ="clearfix">
<span onclick="toggleDiv('filtering', 'filterImg')">Filters <img name="filterImg" src="../images/minus.gif" /></span>
</div>
<div class = "content">
<div class = "formLabel">
Workload Year:
</div>
<div class = "field">
<select size="1" name="wYearSelect">
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option selected value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
</select>
</div>
</div>
<div class = "content">
<div class = "formLabel">
Fiscal Year:
</div>
<div class = "field">
<select size="1" name="wYearSelect">
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option selected value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
</select>
</div>
</div>
<div class = "content">
<div class = "formLabel">
Condition Code:
</div>
<div class = "field">
<select size="1" name="condCodeSelect">
<option selected value=""> </option>
<option value="2007">A</option>
<option value="2008">B</option>
<option value="2009">C</option>
<option value="2010">D</option>
<option value="2011">E</option>
<option value="2012">F</option>
<option value="2013">G</option>
<option value="2014">H</option>
</select>
</div>
</div>
<div class = "content">
<div class = "formLabel">
Customer:
</div>
<div class = "field">
<select size="1" name="customerSelect">
<option selected value=""> </option>
</select>
</div>
</div>
<div class = "content">
<div class = "formLabel">
Repair Facility:
</div>
<div class = "field">
<select size="1" name="repFacSelect">
<option selected value=""> </option>
</select>
</div>
</div>
<div class = "content">
<div class = "formLabel">
Work Type:
</div>
<div class = "field">
<select size="1" name="workTypeSelect">
<option selected value=""> </option>
<option value="10">Rebuild</option>
<option value="11">IROAN</option>
<option value="12">SOAR</option>
<option value="13">Special</option>
</select>
</div>
</div>
<div class = "content">
<div class = "formLabel">
Special Indicator:
</div>
<div class = "field">
<select size="1" name="specIndSelect">
<option selected value=""> </option>
<option value="RST">RESET</option>
</select>
</div>
</div>
<div class = "content">
<div class = "formLabel">
WBS:
</div>
<div class = "field">
<select size="1" name="wbsSelect">
<option selected value=""> </option>
</select>
</div>
</div>
<div class = "content">
<div class = "formLabel">
Appropriation:
</div>
<div class = "field">
<select size="1" name="appropSelect">
<option selected value=""> </option>
</select>
</div>
</div>
<div class = "content">
<div class = "formLabel">
MWSLIN:
</div>
<div class = "field">
<input type="text" name="mwslin" value=""/>
</div>
</div>
<div class = "content">
<div class = "formLabel">
Serial #:
</div>
<div class = "field">
<input type="text" name="mwslin" value=""/>
</div>
</div>
<div class = "content">
<div class = "formLabel">
Include Carryover:
</div>
<div class = "field">
<input type="checkbox" name="include_carryover"></input>
</div>
</div>
<div class ="content">
<div class = "subset">
<h3>Standard Material filter structure here</h3>
<div class = "content">
<div class = "formLabel">
NSN:
</div>
<div class = "field">
<input type="text" name="nsn" value=""/>
</div>
</div>
<div class = "content">
<div class = "formLabel">
TAMCN:
</div>
<div class = "field">
<input type="text" name="tamcn" value=""/>
</div>
</div>
<div class = "content">
<div class = "formLabel">
PGD/Group:
</div>
<div class = "field">
<input type="text" name="pgd" value=""/>
</div>
</div>
<div class = "content">
<div class = "formLabel">
SAC :
</div>
<div class = "field">
<select size="1" name="condCodeSelect">
<option selected value=""> </option>
<option value="2011">1</option>
<option value="2012">2</option>
<option value="2013">3</option>
</select>
</div>
</div>
<div class = "content">
<div class = "formLabel">
Nomenclature:
</div>
<div class = "field">
<input type="text" name="nomen" value=""/>
</div>
</div>
</div>
<div class = "subset">
<h3>Source of Repair</h3>
<div class = "formLabel">
MCA
</div>
<input type="checkbox" name="sorCodes" value="A"></input>
</div>
<div class = "subset">
<h3>Schedule Type</h3>
<div class = "formLabel">
Regular
</div>
<input type="checkbox" name="schedType" value="1"></input><br/>
<div class = "formLabel">
Lot Job
</div>
<input type="checkbox" name="schedType" value="2"></input><br/>
<div class = "formLabel">
Batch Job
</div>
<input type="checkbox" name="schedType" value="3"></input>
</div>
<div class = "subset">
<h3>Workload Type</h3>
<div class = "formLabel">
SMC Workload
</div>
<input type="checkbox" name="workloadType" value="1"></input><br/>
<div class = "formLabel">
Other Workload
</div>
<input type="checkbox" name="workloadType" value="2"></input>
</div>
<div class = "subset">
<h3>Closure Level</h3>
<div class = "formLabel">
Open
</div>
<input type="checkbox" name="workloadType" checked value="1"></input><br/>
<div class = "formLabel">
Physically Complete
</div>
<input type="checkbox" name="workloadType" value="1"></input><br/>
<div class = "formLabel">
Financially Complete
</div>
<input type="checkbox" name="workloadType" value="2"></input>
</div>
</div>
<button name="Generate Report" value="Generate Report" id="Generate">Generate Report</button>
<button name="Clear Criteria" value="Clear Criteria">Clear Criteria</button>