im learning create fullstack apps and i have a problem with delete the record from database backend request work however, I can't add a button that will trigger removal
const Todo = props => (
<tr>
<td className={props.todo.todo_completed ? 'completed' : 'TableText'}>{props.todo.todo_description}</td>
<td className={props.todo.todo_completed ? 'completed' : 'TableText'}>{props.todo.todo_responsible}</td>
<td className={props.todo.todo_completed ? 'completed' : 'TableText'}>{props.todo.todo_priority}</td>
<td className="TableTitle">
<Link to={"/edit/"+props.todo._id} className="a_edit"><img src={edit} alt="Edytuj" className="favicon"/></Link>
<button onSubmit={this.delete}>Usunięcie</button>
</td>
</tr>
)
constructor(props) {
super(props);
this.delete = this.delete.bind(this);
this.state = {todos: []};
}
delete(){
axios.get('http://localhost:4000/todos/delete'+this.props.obj._id)
.then(console.log('Deleted'))
.catch(err =>console.log(err))
}
there is a delete method:
constructor(props) {
super(props);
this.OnRemoveTodo = this.OnRemoveTodo.bind(this);
this.state = {todos: []};
}
OnRemoveTodo(){
axios.get('http://localhost:4000/todos/delete'+this.todo._id)
.then(console.log('Deleted'))
.catch(err =>console.log(err))
}
there is changed const Todo:
const Todo = props => (
<tr>
<td className={props.todo.todo_completed ? 'completed' : 'TableText'}>{props.todo.todo_description}</td>
<td className={props.todo.todo_completed ? 'completed' : 'TableText'}>{props.todo.todo_responsible}</td>
<td className={props.todo.todo_completed ? 'completed' : 'TableText'}>{props.todo.todo_priority}</td>
<td className="TableTitle">
<Link to={"/edit/"+props.todo._id} className="a_edit"><img src={edit} alt="Edytuj" className="favicon"/></Link>
<button onSubmit={props.OnRemoveTodo}><img src={trash} alt="Usuń" className="favicon"/></button>
</td>
</tr>
)
and here is the backend code to which I am trying to appeal
todoRoutes.route('/delete/:id').get(function (req, res) {
Todo.findByIdAndDelete(req.params.id, function(err, todo) {
if(err) res.json(err);
else res.json('Successfully removed');
});
});
there is no this.delete method.
you are using functional component.
i thing you forgot to use props.delete
const Todo = props => (
<tr>
<td className={props.todo.todo_completed ? 'completed' : 'TableText'}>{props.todo.todo_description}</td>
<td className={props.todo.todo_completed ? 'completed' : 'TableText'}>{props.todo.todo_responsible}</td>
<td className={props.todo.todo_completed ? 'completed' : 'TableText'}>{props.todo.todo_priority}</td>
<td className="TableTitle">
<Link to={"/edit/"+props.todo._id} className="a_edit"><img src={edit} alt="Edytuj" className="favicon"/></Link>
<button onSubmit={props.delete}>Usunięcie</button>
</td>
</tr>
Related
I need map a array inside my page, and show the result in a table, but the content don't show up when I compiled the page.
Can anyone help me?
When I print in console the content of a var, this is here. But the info don't show up in the page
import Layout, { siteTitle } from '../components/layout'
const fetch = require('node-fetch');
export default function Home({ devices }) {
return (
<Layout >
{devices.map((device) => (
<table>
<thead>
<th>
{device.localname} / {device.localIP}
</th>
</thead>
{console.log('1')}
<tbody>
<tr>
<td>
{device.IPaddress[0][3].value} // I put this to test, and this works fine
</td>
</tr>
{device.IPaddress.map((port) =>{
<tr>
<td>
{console.log(port[3].value), port[3].value} // here I need to work, I want to put all results of port in a TD tag, the console.log shows up the info, but the page not.
</td>
</tr>
})}
</tbody>
</table>
))}
</Layout >
)
}
export async function getStaticProps() {
const res = await fetch('http://localhost:3000')
const devices = await res.json()
return {
props: {
devices
}
}
}
As commented by #evgenifotia, change the ( for { inside the second array map works fine.
here the final function:
export default function Home({ devices }) {
return (
<Layout >
{devices.map((device) => (
<table>
{console.log('1')}
<tbody>
<tr>
<th>
{device.localname} / {device.localIP}
</th>
</tr>
{device.IPaddress.map(port =>(
<tr>
<td>
{console.log(port[3].value), port[3].value}
</td>
</tr>
))}
</tbody>
</table>
))}
</Layout >
)
}
This is what I have tried? I wanted to call the deleteDebt() function in the code. But I can't pass the function to const Debt. (Outside the class) How can I do that?
Here I want to pass the props.debt._id through the function. Because I want to delete the particular row in the table with it's _id.
const Debt = props => (
<tr>
<td>{props.debt.fullName}</td>
<td>{props.debt.damount}</td>
<td>
<button className="btn btn-danger btn-info " type="delete" onClick={() => this.deleteDebtor(props.debt._id)}>DELETE</button>
</td>
</tr>
)
export default class profile extends Component {
constructor(props) {
super(props);
this.deleteDebtor = this.deleteDebtor.bind(this);
this.state = {
fullName: '',
damount: '',
users: []
}
}
this is where i get data from the database.
componentDidMount() {
axios.get('url')
.then(response => {
this.setState({ users: response.data.data });
})
.catch(function (error) {
console.log(error);
})
}
this is where i make the table.
UserList() {
// console.log(this.state.users);
return this.state.users.map(function (currentDebt, i) {
return <Debt debt={currentDebt} key={i} />;
}
}
this is the deleteDebt() function.
deleteDebtor(data) {
axios.delete('url' + data)
}
this is rendering part
render() {
return (
<React.Fragment>
<div >
<table } >
<thead>
<tr>
<th>Name </th>
<th>Amount</th>
</tr>
</thead>
<tbody>
{this.UserList()}
</tbody>
</table>
</div>
</React.Fragment>
)
}
}
You can pass a reference to the this.deleteDebtor as props.
<Debt delete={this.deleteDebtor} debt={currentDebt} key={i} />;
const Debt = props => (
<tr>
<td>{props.debt.fullName}</td>
<td>{props.debt.damount}</td>
<td>
<button className="btn btn-danger btn-info " type="delete" onClick={() => props.delete(props.debt._id)}>DELETE</button>
</td>
</tr>
)
Note that this will re-render the component whenever the parent re-renders because a new function gets created on each render.
You can use React.memo to prevent that:
const Debt = React.memo(props => (
<tr>
<td>{props.debt.fullName}</td>
<td>{props.debt.damount}</td>
<td>
<button className="btn btn-danger btn-info " type="delete" onClick={() => props.delete(props.debt._id)}>DELETE</button>
</td>
</tr>
));
update
I think you have a scoping issue change the normal function inside map into an arrow function:
UserList() {
// console.log(this.state.users);
return this.state.users.map((currentDebt, i) => {
return <Debt delete={this.deleteDebtor} debt={currentDebt} key={i} />;
});
}
I had search reference and the reference say to try like this :
<?php
...
class ReportExport implements ShouldAutoSize, FromView, WithColumnFormatting, WithEvents
{
...
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
...
$event->sheet->getDelegate()->getRowDimension(37)->setRowHeight(-1);
$event->sheet->getDelegate()->getStyle('R37:Z37')->getAlignment()->setWrapText(true);
},
];
}
}
I try like that, but the result like this :
Should the height of row automatic added based content/text. But it does not added
How can I solve this problem?
Update :
Seems this script : $event->sheet->getDelegate()->getRowDimension(37)->setRowHeight(-1); is not working in the table
I tried the script outside the table, it worked. So the script only work outside table tag
My table like this :
<table>
....
#php ($group = 'A')
#php ($number = 0)
#foreach($values as $item)
#if($number==0 || $group!=$item['group'])
<tr>
<td colspan="9">Kelompok {{$item['group']}}</td>
<td colspan="2"></td>
<td colspan="3"></td>
<td colspan="3"></td>
<td colspan="9"></td>
<td colspan="2"></td>
<td colspan="3"></td>
<td colspan="3"></td>
<td colspan="9"></td>
</tr>
#php ($number = 0)
#endif
<tr>
<td style="text-align:center;" colspan="2">{{++$number}}</td>
<td colspan="7">{{$item['lesson_name']}}</td>
<td style="text-align:center;" colspan="2">{{$item['kb_pengetahuan']}}</td>
<td style="text-align:center;" colspan="3">{{$item['nilai_pengetahuan']}}</td>
<td style="text-align:center;" colspan="3">{{$item['predikat_pengetahuan']}}</td>
<td colspan="9">{{$item['deskripsi_pengetahuan']}}</td>
<td style="text-align:center;" colspan="2">{{$item['kb_keterampilan']}}</td>
<td style="text-align:center;" colspan="3">{{$item['nilai_keterampilan']}}</td>
<td style="text-align:center;" colspan="3">{{$item['predikat_keterampilan']}}</td>
<td colspan="9">{{$item['deskripsi_keterampilan']}}</td>
</tr>
#php ($group = $item['group'])
#endforeach
</table>
Please help me. I need support for PhpSpreadsheet functionality
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
...
$event->sheet->getDelegate()
->getStyle('R37:Z37')
->applyFromArray([ 'alignment' => ['wrapText' => true]])
},
];
}
I'm using fetch API to access a REST API I developed and hosted online. But when retrieving data from the database (>60 rows) to be displayed in a section of the page, it takes about 3-5 seconds to display the data which I styled with HTML and CSS.
My question is how can i implement a css preloader to load before the actual data is displayed. And how do i know that the data has been displayed.
Below is a part of my code.
// Front end
frontend.html
<table>
<caption>User requests</caption>
<thead class="table-head">
<tr>
<th scope="col">Request Id</th>
<th scope="col">User ID</th>
<th scope="col">Brand</th>
<th scope="col">Type</th>
</tr>
</thead>
<tbody id="tablebody">
<div class="loader" id="loader"></div>
</tbody>
</table>
// file.js
fetch(url, {
method: 'GET',
headers: {'Authorization': 'Bearer ' +token}
}).then(response =>{
// Redirect the user to the login page
// If the user is not an admin
if(response.status === 401) {
window.location.replace('./Index.html');
}
return response.json()
})
.then((data) => {
let completedata = data.allRequests;
if(completedata) {
completedata.forEach((item) => {
timeStamp = new Date(item.createdon);
dateTime = timeStamp.toDateString();
theOutput +=`<tr id="listofrequests"><td data-
label="Request Id">${item.id}</td> </a>
<td data-label="Brand">${item.userid}</td>
<td data-label="Brand">${item.brand}</td>
<td data-label="Type">${item.other}</td>
<td data-label="Status">${item.name}</td>
<td data-label="Status"><a href="./Request-status.html" class="btn
view-detail" id="${item.id}" onClick="adminviewonerequest(this.id)">view</a>
</td>
<td data-label="Cancel"><button class="danger" id="${item.id}"
name="${item.name}" onClick="return cancelrequest(this.id, this.name)"><i
class="fa fa-trash"> Cancel Request</i></button></td>
</tr>`;
});
}
else {
toastr.warning(data.message);
}
document.getElementById('tablebody').innerHTML = theOutput;
})
.catch(err => console.log(err));
Use on of those already implemented load spinners How to make a loading spinner in html
Then , In your html code add <div id="loading" class="loader"></div> , Which make the loader displayed by default
and Using fetch :
fetch(YouUrl)
.then(res => {
if(res == 404){
// To hide the loader when the error is happen , There is no need to use loader
// Show some toaster notification for the user or sweet alert
document.getElementById("loading").style.display = 'none';
}
})
.then(json => {
// Here is your data so you need to hide the loader
document.getElementById("loading").style.display = 'none';
});
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)