My question is, I want to print table in reactJS. I have already use axios to get data from django views and I got a response. But, I don't know who to set this data in table format. I have used Table of react-bootstrap. And I am very new in ReactJS.
My Code Snippet is:
change(){
axios.get('http://127.0.0.1:8000/users/')
.then(function (response) {
console.log("in user",response.data[0].id);
this.setState({
id: response.data[0].id,
username: response.data[0].username,
email: response.data[0].email,
});
})
.catch(function (error) {
console.log(error);
});
}
But Now how to use this id, username, and email in "tbody" tag? Please Guide me. Thanks.
Inside your render function where you used react-bootstrap Component. Just use
<tbody>
<tr>
<td>{this.state.id}</td>
<td>{this.state.username}</td>
<td>{this.state.email}</td>
</tr>
</tbody>
You are actually only saving to state one element, you should save to state the resulting array of data.
import React, { Component } from 'react';
import Axios from 'axios';
export default class example extends Component {
constructor(props) {
super(props);
this.state = {
users: [],
};
}
componentDidMount() {
Axios.get('http://127.0.0.1:8000/users/')
.then(function (response) {
this.setState({
users: response.data,
});
})
.catch(function (error) {
console.log(error);
});
}
render() {
const { users } = this.state;
return users.lenght > 0 ? (
<table striped>
<thead>
<tr>
<td>ID</td>
<td>Username</td>
<td>Email</td>
</tr>
</thead>
<tbody>
{users.map((user) => {
return (
<tr key={user.id}>
<td>{user.id}</td>
<td>{user.username}</td>
<td>{user.email}</td>
</tr>
);
})}
</tbody>
</table>
) : (
<h1>No data available!</h1>
);
}
}
Related
I have build RestApi in Django and receiving JSON response for project-list at localhost:8000/project/list and for project-details at localhost:8000/project/details/:id
I have also created respective service, class, and component for receiving the JSON data.I'm also able to GET project_list as well as project_details.
When I do GET Project_detials it gives JSON response with DATA but also gives this error at runtime.
"ProjectDetailsComponent.html:18 ERROR TypeError: Cannot read property 'id' of undefined."
project.ts (Class)
export class Project_list {
id : number;
projectname : string;
}
export class Project_details {
id : number;
projectname : string;
project_status : string;
description : string;
start_date : Date;
due_date : Date;
}
```
# project.service.ts (Service)
```
import { Injectable } from '#angular/core';
import { HttpClient, HttpHeaders } from '#angular/common/http';
import { Project_list,Project_details } from '../models/project';
import { Observable } from 'rxjs';
#Injectable({
providedIn: 'root'
})
export class ProjectService {
private projectUrl: string;
constructor(private http: HttpClient) {
this.projectUrl = 'http://localhost:8000/project';
}
public findAllProjects(): Observable<Project_list[]>
{
return this.http.get<Project_list[]>(this.projectUrl+'/list');
}
public findProjectById(id:number) : Observable<any>
{
return this.http.get<Project_details>(this.projectUrl+'/details/'+id+'/');
}
}
```
# ProjectListComponent.ts (Component)
```
import { Component, OnInit } from '#angular/core';
import { ProjectService } from '../../../services/project.service'
import { Project_list } from '../../../models/project'
#Component({
selector: 'app-project-list',
templateUrl: './project-list.component.html',
styleUrls: ['./project-list.component.css']
})
export class ProjectListComponent implements OnInit {
project_list : Project_list[];
constructor( private projectService:ProjectService ) { }
ngOnInit() {
this.projectService.findAllProjects().subscribe(data => {
this.project_list = data;
console.log(data);
});
}
}
ProjectDetailsComponent.ts (Component)
import { Component, OnInit } from '#angular/core';
import { ProjectService } from '../../../services/project.service';
import {Project_details} from '../../../models/project';
import { ActivatedRoute } from '#angular/router';
#Component({
selector: 'app-project-details',
templateUrl: './project-details.component.html',
styleUrls: ['./project-details.component.css']
})
export class ProjectDetailsComponent implements OnInit {
project_details : Project_details;
id:number;
private sub:any;
constructor(private projectService:ProjectService, private route: ActivatedRoute) { }
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
this.id = +params['id'];
console.log(this.id);
});
this.projectService.findProjectById(this.id).subscribe(data => {
this.project_details = data;
console.log(this.project_details);
});
}
}
ProjectDetailsComponent.html (HTML)
<table class="table table-bordered table-striped">
<thead class="thead-dark">
<tr>
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">Status</th>
<th scope="col">Description</th>
<th scope="col">Start Date</th>
<th scope="col">Due Date</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ project_details.id }}</td>
<td>{{ project_details.projectname }}</td>
<td>{{ project_details.project_status }}</td>
<td>{{ project_details.description }}</td>
<td>{{ project_details.start_date }}</td>
<td>{{ project_details.due_date }}</td></td>
</tr>
</tbody>
</table>
Can you please paste the JSON object returned in the API response also?
Django might send an array of data.
Please try (in ProjectDetailsComponent.ts (Component))
this.project_details = data[0];
insted of
this.project_details = data;
I am trying to pass a list of int Ids from one View to another on button click. I am able to hit the controller but its passing null.
Html:
#foreach (var business in Model.Businesses)
{
<tr>
<td>#business.Name</td>
<td>
<p>
#foreach (var bt in #business.BusinessTypes)
{
#bt.Name;
}
</p>
</td>
<td><button type="button" id="btnCalcVS"> Calculate Validator Score</button></td>
</tr>
}
JQUery:
<script type="text/javascript">
$(document).ready(function () {
var businessTypeIds = [];
$(document).on("click", "#btnCalcVS", function () {
$.ajax({
type: 'POST',
url: '/BusinessType/Index',
contentType: "application/json",
data: JSON.stringify( #foreach (var business in Model.Businesses)
{
#foreach (var bt in #business.BusinessTypes)
{
#bt.Id;
}
}),
success: function (data) {
alert("success");
},
error: function (e) {
alert(e);
}
});
});
});
</script>
Controller:
[HttpPost]
[Route("/BusinessType/Index")]
public IActionResult Index([FromBody] List<int> businessTypeIds)
{
//logic
}
As mentioned, I am hitting the controller but it's having null values. As can be seen from the HTML code, i have a list inside a list. (Business Types inside Businesses, so 2 many to many relationships)
Can someone tell me where i am going wrong?
You could try get the id from html and then pass it to controller.
<form method="post">
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.DenniPlan2[0].Id)
</th>
<th>
#Html.DisplayNameFor(model => model.DenniPlan2[0].CisloZakazky)
</th>
<th>
#Html.DisplayNameFor(model => model.DenniPlan2[0].CisloProduktu)
</th>
<th>
#Html.DisplayNameFor(model => model.DenniPlan2[0].Poradi)
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.DenniPlan2)
{
<tr>
<td>
#Html.EditorFor(model => item.Id)
</td>
<td>
#Html.EditorFor(model => item.CisloZakazky)
</td>
<td>
#Html.EditorFor(model => item.CisloProduktu)
</td>
<td>
#Html.EditorFor(model => item.Poradi)
</td>
</tr>
}
</tbody>
</table>
<input type="button" id="btnCalcVS" value="submit" />
</form>
#section Scripts{
<script type="text/javascript">
$(document).ready(function () {
var businessTypeIds = [];
$("[id=item_Id]").each(function () {
businessTypeIds.push($(this).val());
});
console.log(businessTypeIds);
$(document).on("click", "#btnCalcVS", function () {
$.ajax({
type: 'POST',
url: '/BusinessType/Index',
contentType: "application/json",
data: JSON.stringify(businessTypeIds),
success: function (data) {
alert("success");
},
error: function (e) {
alert(e);
}
});
});
});
</script>
}
For this line $("[id=item_Id]").each(function () {, you could get the id value item_Id by check the generated html in the web browser.
hello guys i am fetching data from my api and i try to set a search bar like this :
import React, { Component } from "react";
import ProductsIndex from "./search_bar";
class SearchBar extends Component {
constructor(props) {
super(props);
this.state = { term: "" };
this.onInputChange = this.onInputChange.bind(this);
this.onFormSubmit = this.onFormSubmit.bind(this);
}
onInputChange(event) {
this.setState({ term: event.target.value });
}
onFormSubmit(event) {
event.preventDefault();
ProductsIndex.renderProducts(this.state.term)
// this.props.fetchWeather(this.state.term);
this.setState({ term: "" });
}
render() {
return (
<form onSubmit={this.onFormSubmit} className="input-group">
<input
placeholder="Get a five-day forecast in your favorite cities"
className="form-control"
value={this.state.term}
onChange={this.onInputChange}
/>
<span className="input-group-btn">
<button type="submit" className="btn btn-secondary">Submit</button>
</span>
</form>
);
}
}
export default SearchBar;
In my onSubmit function try to call my function renderProducts from my class ProductsIndex here :
class ProductsIndex extends Component {
componentDidMount() {
this.props.fetchProducts();
}
renderProducts(term) {
return _.map(this.props.products, product => {
if(product.name==term) {
return (
<tr key={product.name}>
<td>{product.name}</td>
<td>{product.product_category.name}</td>
<td>{product.price}</td>
</tr>
);
}
});
}
render(){
return(
<div>
<table className="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>Name</th>
<th>Category</th>
<th>Price</th>
</tr>
</thead>
<tbody>
{this.renderProducts()}
</tbody>
</table>
</div>
);
}
}
function mapStateToProps(state) {
return {products: state.products}
}
export default connect(mapStateToProps, {fetchProducts})
(ProductsIndex);
But that is doesn't work i get this error : Uncaught TypeError: _search_bar2.default.renderProducts is not a function
Thanks you for your help
The short answer is: ProductsIndex.prototype.renderProducts
The correct answer is: the state of terms should be owned by a component higher up in the hierarchy. SearchBar triggers a state change in the parent component and it trickles down to ProductIndex. You already probably have a component suitable for this in your hierarchy, but here is how it may look if you add one explicitly for this purpose.
export class SharedParent extends React.Component {
constructor() {
super();
this.state = {
term: ""
};
//I use this style for methods, you can also use fat arrows to ensure 'this' is properly set
this.setTerm = this.setTerm.bind(this);
}
setTerm(term) {
//beware current best practice is to pass a fucntion
//not an object to setState
this.setState({ term });
}
render() {
return [
<ProductIndex key="a" term={this.state.term} {...this.props.productProps} />,
<SearchBar key="b" setTerm={this.setTerm} {...this.props.searchProps}/>
];
}
}
PS: I would suggest also using Redux or another state management solution. You can't go wrong with Redux though.
Model: 'Category'
Subclass: 'Workflow'
I am trying to display the different 'Workflows' available for each 'Category' but I am receiving this error >>
Uncaught Error: Nothing handled the event 'createWorkflow'.
Here's some code
VpcYeoman.Category = DS.Model.extend({
permittype: DS.attr('string'),
isCompleted: DS.attr('boolean'),
classNameBindings: ['isAdministrator']
});
VpcYeoman.Workflow = VpcYeoman.Category.extend({
workflowtype: DS.attr('string')
})
VpcYeoman.Category.FIXTURES = [
{
id: 1,
permittype:'Building'
},
{
id: 2,
permittype:'Electrical'
},
{
id: 3,
permittype:'Zoning'
},
{
id: 4,
permittype:'Fire'
}
];
I'm also a little stumped on how to make FIXTURES for this subclass. I attempted recreating VpcYeoman.Workflow.FIXTURES = [id & workflowType examples], but it didn't display.
Category.hbs
<div class="department-header">
<div class="user-header">
Category: {{permittype}}
</div>
</div>
<table class="table table-hover table-responsive">
<thead>
<tr class="people-list">
<td><h4>Workflow Type</h4></td>
</tr>
</thead>
<table>
{{#each workflows}}
<tr>
<td>
{{workflowtype}}
</td>
</tr>
{{/each}}
</table>
<div class="input-bar">
<img src="images/lightning-icon-edited.png" class="input-icon">
{{input type="text" value=newWorkflowtype placeholder="Create a workflow and press enter" action="createWorkflow"}}
</div>
&&
VpcYeoman.CategoriesController = Ember.ArrayController.extend({
actions: {
createCategory: function () {
var permittype = this.get('newPermittype');
if (!permittype.trim()) {return;}
var category = this.store.createRecord('category', {
permittype: permittype
});
this.set('newPermittype', '');
category.save();
},
createWorkflow: function () {
var workflowtype = this.get('newWorkflowtype');
if (!workflowtype.trim()) {return;}
var workflow = this.store.createRecord('workflow', {
workflowtype: workflowtype
});
this.set('newWorkflowtype', '');
workflow.save();
}
}
});
&&
VpcYeoman.CategoriesRoute = Ember.Route.extend({
model: function() {
return this.store.find('category');
},
setupController:function(controller, model){
this._super(controller, model);
controller.set('workflows', this.store.find('workflow'));
}
});
VpcYeoman.CategoryRoute = Ember.Route.extend({
});
I'm assuming that you have a categories and category routes/templates based on the differently named things up there.
actions go the the particular route's controller Category then the route's route Category then up the routes Categories, Application
It looks like you are setting the workflows on the Categories controller, but trying to use it in the Category template
I'm new to ember.js which seems to be very interesting but hard/difficult to learn cause there are so many ways to solve a programming problem.
I try to code a very simple app but don't know how...
The app show at the top in a table all customers with an id received from a json file (testin local) and on click on the id there will be a json-request to receive detailed infos to the customer.
The problem is, that I receive detailed data but can't display it in a template under the overview template...
Can some one help me...!?
Greets
Christian [from germany]
Here's some code-snippets I got till now...
APP.JS
App.Search = Ember.Object.extend();
App.Search.reopenClass({
all: function() {
// console.log('Search.reopenClass all');
return $.getJSON("json/kunden_suchen.json").then(function(response) {
var items = [];
response.Konto.forEach(function(child) {
// console.log(child);
var item = new App.item();
item.set('content', child);
item.set('nr', child.nr);
item.set('name1', child.name1);
item.set('name2', child.name2);
item.set('plz', child.anschrift.plz);
item.set('anschrift', child.anschrift);
items.push(item);
});
return items;
});
}
});
App.SearchRoute = Ember.Route.extend({
model: function() {
// console.log('SearchRoute model');
return App.Search.all();
},
events: {
details: function() {
console.log('SearchRoute detail');
return App.Details.all();
}
}
});
App.Details= Ember.Object.extend();
App.Details.reopenClass({
all: function() {
// console.log('Search.reopenClass all');
// return $.getJSON("json/kunden_suchen.json").then(function(response) {
return $.getJSON("json/customer.json").then(function(response) {
var items = [];
response.Konto.forEach(function(child) {
console.log(child);
var item = new App.item();
item.set('content', child);
item.set('nr', child.nr);
items.push(item);
});
return items;
});
}
});
index.html
<script type="text/x-handlebars" data-template-name="items">
<div style="width: 80%; margin: auto;">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Name1</th>
<th>nr</th>
</tr>
</thead>
<tbody>
{{#each item in model}}
<tr class="info">
<td> {{ item.content.name1 }} </td>
<td> {{ item.content.id}} </td>
{{/each}}
</tbody>
</table>
</div>
</script>
Arrays in ember are augmented, e.g. it's prototype, so for your arrays to be binding aware and thus live updating your templates when the data asynchronously arrives you can't just use vanilla push(...) but pushObject(...) instead.
So try to change every occurrence of push to pushObject
...
items.pushObject(item);
...
Hope it helps.
Greets Alex [From Spain] :)