i have created a component in which i delete the table item its deleted i also want to add a button to add in the deleted item in the table done successfully but problem is that when I remove table item it item should be shown in the deleted component here is my code
class Movies extends Component {
state = {
movies,
deleted:[]
};
handleDelete = (_id) => {
const movie = this.state.movies.find(movie => movie._id === _id);
this.setState({ deleted: [...this.state.deleted, movie] });
this.setState({movies:this.state.movies.filter(movie => movie._id !== _id)})
};
handleReinstate = (_id) => {
const movie = this.state.deleted.find((movie) => movie._id === _id);
this.setState({ movies:[ ...this.state.movies,movie] });
this.setState({deleted:this.state.deleted.filter((movie) => movie._id !== _id)})
};
render() {
return (
<table className="table">
<thead>
<tr>
<th>Title</th>
<th>Genre</th>
<th>Stock</th>
<th>Rate</th>
<th></th>
</tr>
</thead>
<tbody>
{this.state.movies.map((movie) => {
return(
<tr key={movie._id}>
<td>{movie.title}</td>
<td>{movie.genre.name}</td>
<td>{movie.numberInStock}</td>
<td>{movie.dailyRentalRate}</td>
<td
onClick={() => this.handleDelete(movie._id)}
className="btn btn-danger btn-outline-warning btn-sm active "
>
Remove
</td>
</tr>
)})}
</tbody>
<tbody>
<h1>deleted</h1>
{this.state.deleted.map(movie => (
<tr key={movie._id}>
<td
onClick={() => this.handleReinstate(movie._id)}
className="btn btn-danger btn-outline-primary btn-sm active "
>
ADD
</td>
</tr>
))}
</tbody>
</table>
);
}
}
I want to show like this but failed
adding this will solve problem
{this.state.deleted.map(movie => (
<tr key={movie._id}>
<td>{movie.title}</td>
<td>{movie.genre.name}</td>
<td>{movie.numberInStock}</td>
<td>{movie.dailyRentalRate}</td>
<td
onClick={() => this.handleReinstate(movie._id)}
className="btn btn-danger btn-outline-primary btn-sm active "
>
ADD
</td>
</tr>
))}
Related
I want to add a checkbox to each row in the table I have already created.
In addition, there should be a select all button and it should be able to select all rows.
I tried this but I'm getting the error
support for the experimental syntax 'optional chaining' isn't currently enabled.
import React, { useState, useEffect } from "react";
/* const userData = [{name: "Didem1"}, {name : "Didem2"}] */
const UserTable = (props) => {
const [users, setUsers] = useState([]);
const userData = [props.users];
useEffect(() => {
setUsers(userData);
}, []);
const handleChange = (e) => {
const { name, checked } = e.target;
if (name === "allSelect") {
let tempUser = users.map((user) => {
return { ...user, isChecked: checked };
});
setUsers(tempUser);
} else {
let tempUser = users.map((user) =>
user.name === name ? { ...user, isChecked: checked } : user
);
setUsers(tempUser);
}
};
return (
<table className="table table-dark">
<thead>
<tr>
<th scope="col">
<input
type="checkbox"
className="form-check-input"
name="allSelect"
onChange={handleChange}
checked={
users.filter((user) => user?.isChecked !== true).length < 1
}
/>
Select All
</th>
<th scope="col">Hostname</th>
<th scope="col">Username</th>
<th scope="col">Stop</th>
<th scope="col">Sleep</th>
<th scope="col">Start</th>
<th scope="col">Status</th>
<th scope="col">CPU Temperature(°C)</th>
<th scope="col">GPU Info</th>
<th scope="col">Edit/Delete</th>
</tr>
</thead>
<tbody>
{props.users.length > 0 ? (
props.users.map((user) => (
<tr key={user.id}>
<th scope="row">
<input
type="checkbox"
className="form-check-input"
/* user?.isChecked || false */
name={user.name}
checked={user?.isChecked || false}
onChange={handleChange}
/>
</th>
<td>{user.name}</td>
<td>{user.username}</td>
<td>
<button
onClick={() => props.editStopPC(user)}
className="btn btn-danger"
>
Stop
</button>
</td>
<td>
<button
onClick={() => props.editSleepPC(user)}
className="btn btn-warning"
>
Sleep
</button>
</td>
<td>
<button
onClick={() => props.editStartPC(user)}
className="btn btn-success"
>
Start
</button>
</td>
<td>{user.status}</td>
<td>{user.cpu}</td>
<td>{user.info}</td>
<td className="center-align">
<button
className="btn btn-info"
onClick={() => props.editRow(user)}
>
edit
</button>
<button
className="btn btn-danger"
onClick={() => props.deleteUser(user.id)}
>
delete
</button>
</td>
</tr>
))
) : (
<tr>
<td colSpan={9}>{props.users[0]} No Users</td>
</tr>
)}
</tbody>
</table>
);
};
export default UserTable;
So I installed react-scripts#3.3.0 and #babel/plugin-proposal-optional-chaining and now I am getting error:
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
I'm not sure what causes this. I would be glad if you help.
I'm creating a table by pulling the information from the API. There is a button next to the head of each table. Let's say I click the button next to the name, it should sort by name. If I click it again, it should change the name order. OrderBy takes a string like "Id", "Name" in the API URL and sorts accordingly. Ascending reverses the order when it is true or false. So I created two states. When I click the buttons it doesn't work the first time, I have to click twice. I'm not sure, but it probably has something to do with my calling getProductList right after changing the state. How can I solve this?
Initial states must be true and "Id":
const [ascending, setAscending] = useState(true);
const [orderBy, setOrderBy] = useState("Id");
handleSort function:
const handleSort = (e) => {
setOrderBy(e.target.name);
setAscending(!ascending);
getProductList();
};
API URL:
`api/common/products/getProductsAll?page=1&pageSize=30&orderBy=${orderBy}&ascending=${ascending}&Code=&Name=`
Table:
return (
<div className={styles.container}>
<button onClick={() => console.log(orderBy,ascending)}>xxx</button>
<Table striped bordered hover variant="dark">
<thead>
<tr>
<th>
<span>Ürün Kodu</span>
<button name="Id" onClick={(e) => handleSort(e)}>
o
</button>
</th>
<th>
<span>Ürün Adı</span>
<button name="Name" onClick={(e) => handleSort(e)}>
o
</button>
</th>
<th>
<span>Fiyat</span>
<button name="Price" onClick={(e) => handleSort(e)}>
o
</button>
</th>
<th>
<span>Para Birimi</span>
<button name="CurrencyId" onClick={(e) => handleSort(e)}>
o
</button>
</th>
<th>
<span>Birim Seti</span>
<button name="Unit" onClick={(e) => handleSort(e)}>
o
</button>
</th>
<th>
<span>İşlemler</span>
<button>o</button>
</th>
</tr>
</thead>
<tbody>
{list?.map((v) => {
return (
<tr key={v.Id}>
<th>{v.Code}</th>
<th>{v.Name}</th>
<th>{v.Price}</th>
<th>Adet</th>
<th>TL</th>
<th>-</th>
</tr>
);
})}
</tbody>
</Table>
</div>
);
};
In React I have a ternary operator returning a component if a condition is met:
{ this.state.houseHoldGroup.length > 0 ? (
<Table className="align-items-center table-flush" responsive>
<thead className="thead-light">
<tr>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col"></th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
{this.checkHouseholdGroup()}
</tbody>
</Table>
) : null }
Works good.
Inside this component I have a method: this.checkHouseholdGroup()
The expected behavior is for this method to return the table data inside <tbody></tbody>
checkHouseholdGroup = () => {
const householdDetails = this.state.houseHoldGroup;
householdDetails.forEach(el => {
console.log(el.firstHousehold)
return (
<tr>
<th scope="row">{el.firstHousehold}</th>
<td>{el.lastHousehold}</td>
<td>
<Button
color="primary"
href="#pablo"
onClick={e => e.preventDefault()}
size="sm"
onClick={e => this.submitMember(e)}>
Update
</Button>
</td>
<td>
<Button
color="primary"
href="#pablo"
onClick={e => e.preventDefault()}
size="sm"
onClick={e => this.submitMember(e)}>
Delete
</Button>
</td>
</tr>
)
})
}
I can confirm the element has data. I console.log(el.firstHousehold) can see it's not empty. What am I doing wrong? The expected result would be that it would return my with the data in it.
Have you tried mapping instead of using forEach?
checkHouseholdGroup = () => {
const householdDetails = this.state.houseHoldGroup;
return householdDetails.map(el => {
console.log(el.firstHousehold)
return (
<tr>
<th scope="row">{el.firstHousehold}</th>
<td>{el.lastHousehold}</td>
<td>
<Button
color="primary"
href="#pablo"
onClick={e => e.preventDefault()}
size="sm"
onClick={e => this.submitMember(e)}>
Update
</Button>
</td>
<td>
<Button
color="primary"
href="#pablo"
onClick={e => e.preventDefault()}
size="sm"
onClick={e => this.submitMember(e)}>
Delete
</Button>
</td>
</tr>
)
})
}
Replace householdDetails.forEach with return householdDetails.map and you should be good.
forEach is used to create side effects - it does not return anything. The parent component of checkHouseholdGroup waits for a value to be returned, but nothing comes out of the function.
Using return inside a forEach call will make the returned values go "nowhere". That's why you need to use map (ir returns a list with the elements), and then return the array.
Say I have a table that stores items and their weights and I want to sum the total weight from that column and display that at the top on the table. Not using anything other than react/jsx. For more clarification, my table is pulling data stored on a node.js/express server.
I tried writing a function to go into my table component that used reduce, it did not work at all and I'm not sure how to go about it at this point.
Would like the total weight to be displayed next to the table header.
const GearTable = (props) => {
return(
<div style={styles.box}>
<h3>Your Gear Locker</h3>
<br />
<br />
<Table hover style={[styles.font, styles.box]}>
<thead>
<tr>
<th>ID</th>
<th>Item Name</th>
<th>Description</th>
<th>Weight</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
{props.gear.map((gear, id) => {
return(
<tr key={id}>
<th scope="row" style={styles.font}> {gear.id}</th>
<td style={styles.font}> {gear.itemName}</td>
<td style={styles.font}> {gear.description}</td>
<td style={styles.font}> {gear.weight.value}</td>
<td style={styles.font}> {gear.quantity}</td>
<td>
<Button className="btn btn-secondary" size="lg" style={styles.font} id={gear.id} onClick={e => props.update(e, gear)}>Update Locker</Button>
<Button className="btn btn-secondary" size="lg" style={styles.font} id={gear.id} onClick={props.delete}>Delete Item</Button>
</td>
</tr>
)
})
}
</tbody>
</Table>
</div>
);
}
const GearTable = (props) => {
return(
Your Gear Locker
ID
Item Name
Description
Weight
Quantity
{props.gear.map((gear, id) => {
return(
{gear.id}
{gear.itemName}
{gear.description}
{gear.weight.value}
{gear.quantity}
props.update(e, gear)}>Update Locker
Delete Item
)
})
}
);
}
I have table so I want to retrieve key/value of specific row in which button was clicked.
componentDidMount() {
let com = this;
firebase
.database()
.ref()
.child("bills")
.once("value", snap => {
let items = [];
snap.forEach(childD => {
items.push({
balance: childD.val().balance,
bill_num: childD.val().bill_num,
date: childD.val().date,
key: childD.val().key,
name: childD.val().name,
total: childD.val().total
});
});
Array.prototype.push.apply(com.state.products, items);
com.setState({
products: com.state.products
});
});
}
open = e => {
e.preventDefault();
console.log("kal" + this.state.value);
};
handleChange=event=>{
this.setState({value: event.target.value});
}
render() {
return (
<table className="table table-striped">
<thead>
<tr>
<th scope="col">Bill number</th>
<th scope="col">Name</th>
<th scope="col">Date</th>
<th scope="col">Total</th>
<th scope="col">Balance</th>
<th scope="col">Delete</th>
<th scope="col">Open bill</th>
</tr>
</thead>
<tbody>
{console.log(this.state.products)}
{this.state.products.map((value, key) => (
<tr key={value.key}>
<th scope="row">{value.bill_num}</th>
<td>{value.name}</td>
<td>{value.date}</td>
<td>{value.total}</td>
<td>{value.balance}</td>
<td>
<form onSubmit={this.returnOrder}>
<input value={value.key} type="hidden" />
<button className="btn btn-danger" type="submit">
Return
</button>
</form>
</td>
<td>
<form onSubmit={this.open}>
<input value={value.key} onChange={this.handleChange} ref={ eKey => (this.inputeKey = eKey)} />
<button className="btn btn-info" type="submit">
Open
</button>
</form>
</td>
</tr>
))}
</tbody>
</table>
</div>
) : (
<Printout
keya={{
key: this.props.keyas
}}
/>
)}
);
}
}
Typically, tables in React are made from several components: whole table, row and for example buttons in row. So in table you place array of rows. Each row has it's child buttons. So you can send props to RowButtons child with information about clicked row.