I am new in ReactJS so fetching data isn't my expertise, so I'm trying to learn ReactJS with my PHP Native. The problem is fetching data is difficult for me. This code below there's no error but it won't display my data.
It can't display in my UI, the data is stayed in console(Network -> Response), i tried everything still can't output it. Can someone help?
import './Timesheet.css';
import React, { useState, useEffect } from 'react';
import { Table, Container, Button, } from 'react-bootstrap';
const Timesheet = () => {
const [item, setItem] = useState([]);
useEffect(() => {
fetch("http://localhost/WaterRefillingStation/app/controllers/timesheet_controller.php")
.then(response => response.json())
.then(
(result) => {
setItem(result);
}
)
}, [])
return (
<Container fluid>
<div className="d_flex my-4, text-uppercase">
<h1>
Timesheet Screen
</h1>
</div>
<div className="float-end, tryy" >
<Button variant="success" size="md">Time IN</Button>{' '}
<Button variant="secondary" size="md">Time OUT</Button>{' '}
<Button variant="danger" size="md">Logout</Button>
</div>
<Table striped bordered hover size="md" variant="dark" responsive="xl" >
<thead className="thead-dark">
<tr style={{textAlign: "center"}}>
<th>ID</th>
<th>Date</th>
<th>Profile Picture</th>
<th>Last Name</th>
<th>First Name</th>
<th>Designation</th>
<th>Time IN</th>
<th>Time OUT</th>
</tr>
</thead>
<tbody>
{item.map(item => (
<tr key={item}>
<td>{item.userID}</td>
<td>{item.birthday}</td>
<td>{item.profpic}</td>
<td>{item.lastname}</td>
<td>{item.firstname}</td>
<td>{item.designation}</td>
<td>{item.created_at}</td>
<td>{item.updated_at}</td>
</tr>
))}
</tbody>
</Table>
</Container>
);
}
export default Timesheet;
Ok, it seems that you have used .map in the wrong way. You can use it for an array, not an object. use this instead:
return (
<Container fluid>
<div className="d_flex my-4, text-uppercase">
<h1>
Timesheet Screen
</h1>
</div>
<div className="float-end, tryy" >
<Button variant="success" size="md">Time IN</Button>{' '}
<Button variant="secondary" size="md">Time OUT</Button>{' '}
<Button variant="danger" size="md">Logout</Button>
</div>
<Table striped bordered hover size="md" variant="dark" responsive="xl" >
<thead className="thead-dark">
<tr style={{textAlign: "center"}}>
<th>ID</th>
<th>Date</th>
<th>Profile Picture</th>
<th>Last Name</th>
<th>First Name</th>
<th>Designation</th>
<th>Time IN</th>
<th>Time OUT</th>
</tr>
</thead>
<tbody>
<tr key={item}>
<td>{item.userID}</td>
<td>{item.birthday}</td>
<td>{item.profpic}</td>
<td>{item.lastname}</td>
<td>{item.firstname}</td>
<td>{item.designation}</td>
<td>{item.created_at}</td>
<td>{item.updated_at}</td>
</tr>
</tbody>
</Table>
</Container>
if you want to have a table for each user (for example), you should to provide data like below in your backend:
{
data:[
{ USER1 Info},
{ USER2 Info},
// so on
]
}
Related
I have a table in which a column stores json data,but I am unable to loop through it in the view.
Sample Data :
[
{
"id":28,
"item_id":4,
"item_name":"Texco Dx",
"rate":"15.00",
"unit":"box",
"qty":"20",
"price":300,
"tax":12,
"taxType":"GST",
"tax_paid":36,
"disc":0,
"value":336,
"barcode":"1234ergeg",
"hsn":"q23423",
"lot_number":"1235r23r",
"checked":true,
"error":false,
"returnedQty":12,
"returnedValue":33.6,
"returnedTax":3.6,
"allowedQty":10
},
{
"id":27,
"item_id":3,
"item_name":"Norflox Tz",
"rate":"124.00",
"unit":"qty",
"qty":"10",
"price":1240,
"tax":10,
"taxType":"GST",
"tax_paid":124,
"disc":0,
"value":1364,
"barcode":"11121231",
"hsn":"123213",
"lot_number":"123",
"checked":true,
"error":false,
"returnedQty":3,
"returnedValue":272.8,
"returnedTax":24.8,
"allowedQty":9
}
]
This is how I am trying to loop through it :
<table>
<tr class="left">
<th>SR NO</th>
<th>ITEM NAME</th>
<th>PRICE/UNIT</th>
<th>QTY</th>
<th>DISCOUNT %</th>
<th>TAX</th>
<th>TOTAL</th>
</tr>
#foreach ($bill[0]->ITEM_DETAILS as $item)
<tr>
<td>{{$item->id}}</td>
<td>{{$item0->item_name}}</td>
</tr>
#endforeach
</table>
When I stored the same data using ng-init and tried iterating through it using ng-repeat it worked.
Can anyone explain this behaviour.
Thanks in advance
If you are getting a json in the view and assuming that $bill contains the json:
<table>
<tr class="left">
<th>SR NO</th>
<th>ITEM NAME</th>
<th>PRICE/UNIT</th>
<th>QTY</th>
<th>DISCOUNT %</th>
<th>TAX</th>
<th>TOTAL</th>
</tr>
#foreach (json_decode($bill, true) as $item)
<tr>
<td>{{$item['id']}}</td>
<td>{{$item['item_name']}}</td>
<td>{{$item['price']}}</td>
<td>...</td>
</tr>
#endforeach
</table>
If your json comes from the controller you could pass an array and do this.
In controller
$bills = ....
return view('your.template', ['bill' => json_decode($bills, true)]);
In view
<table>
<tr class="left">
<th>SR NO</th>
<th>ITEM NAME</th>
<th>PRICE/UNIT</th>
<th>QTY</th>
<th>DISCOUNT %</th>
<th>TAX</th>
<th>TOTAL</th>
</tr>
#foreach ($bill as $item)
<tr>
<td>{{$item['id']}}</td>
<td>{{$item['item_name']}}</td>
<td>{{$item['price']}}</td>
<td>...</td>
</tr>
#endforeach
</table>
I have a table display and each row contains a button. With this button l have to pass the id to the controller where using this Admin can view user details one by one.
<table id="example2" class="table table-bordered table-hover" style="text-align: center">
<thead>
<tr>
<th>Applicant ID</th>
<th>Full name</th>
<th>Position applied</th>
<th>Date & time applied</th>
<th>View CV</th>
</tr>
</thead>
<tbody>
#foreach ($jobapplied as $row)
<tr>
<td>{{$row->app_id}}</td>
<td>{{$row->fullname}}</td>
<td>{{$row->position}}</td>
<td>{{$row->created_at}}</td>
<td>
<button type="submit" class="btn btn-
default" name="viewcv"> View</button> <br>
<br>
</td>
</tr>
#endforeach
</tbody>
</table>
Please check the code , hope it helps:
<table id="example2" class="table table-bordered table-hover" style="text-align: center">
<thead>
<tr>
<th>Applicant ID</th>
<th>Full name</th>
<th>Position applied</th>
<th>Date & time applied</th>
<th>View CV</th>
</tr>
</thead>
<tbody>
#foreach ($jobapplied as $row)
<tr>
<td>{{$row->app_id}}</td>
<td>{{$row->fullname}}</td>
<td>{{$row->position}}</td>
<td>{{$row->created_at}}</td>
<td>CV</td>
</tr>
#endforeach
</tbody>
</table>
Assuming you want to pass the field "id" or change it according to your requirement.
If you want to link to a show view, try something like this:
#foreach ($jobapplied as $row)
<tr>
<td>{{$row->app_id}}</td>
<td>{{$row->fullname}}</td>
<td>{{$row->position}}</td>
<td>{{$row->created_at}}</td>
<td>
<a href="{{action('JobController#show', $row->id)}}" class="btn btn-default">View</button>
</td>
</tr>
#endforeach
This will create a link to a view, where you can show the data.
In your controller JobController, you must create a function that receives the data:
public function show(Request $request, $id) {
...
}
Just pass the id of the record to controller function and get the details from database.
<td>
View <br>
</td>
When i display data in bootstrap datatable using ajax jquery then pagination and search box is not display.so how to solve this problem
My html sample code is here.
<div class="row" style="margin-top: 2em;">
<div class="panel panel-white">
<div class="panel-heading clearfix">
</div>
<div class="panel-body">
<div id="live_data" >
//data table display here
</div>
</div>
</div>
And my ajax file code is here
<?php
$output='';
$output.=' <div class="table-responsive">
<table id="example" class="table table-striped table-
bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
<th>Age</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>System Architect</td>
<td>Edinburgh</td>
<td>61</td>
<td>2011/04/25</td>
<td>$320,800</td>
</tr>
<tr>
<td>Garrett Winters</td>
<td>Accountant</td>
<td>Tokyo</td>
<td>63</td>
<td>2011/07/25</td>
<td>$170,750</td>
</tr>
<tr>
<td>Ashton Cox</td>
<td>Junior Technical Author</td>
<td>San Francisco</td>
<td>66</td>
<td>2009/01/12</td>
<td>$86,000</td>
</tr>
</tbody>
</table>
</div>';
echo $output;
?>
And my jquery function is here
getcamera();
function getcamera(){
$.ajax({
type:"POST",
url:'../ajaxfiles/ajax_getcamera.php',
success:function(data){
$('#live_data').html(data);
}
});
}
This above code is work properly but search box and pagination is not display
so how to solve that problem???
`getcamera();
function getcamera(){
$.ajax({
type:"POST",
url:'../ajaxfiles/ajax_getcamera.php',
success:function(data){
$('#live_data').html(data);
$('#example').DataTable();// Add this line.
}
});
}`
Above code will work. This is not the best solution.
For good ajax implementation you can see example usages at
https://datatables.net/examples/ajax/simple.html And https://datatables.net/examples/server_side/simple.html
I'm new to Angularjs. I'm trying to call function onclick of the buttons. I used ng-click but it's not working. These buttons are dynamically created.
I'm implementing this application in CodeIgniter framework. Here is my code;
//view
<section class="content" ng-app="manageApp">
<div growl></div>
<div class="row" ng-controller="customerManageController">
<div class="col-md-12">
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">Customers Information</h3>
</div>
<div class="box-body">
<table class="table table-striped table-bordered" id="customer_table" >
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Mobile</th>
<th>City</th>
<th>Email</th>
<th>Address1</th>
<th>Address2</th>
<th>Action</th>
</tr>
</thead>
<tbody id="tableBody">
</tbody>
<tfoot>
<tr>
<th>ID</th>
<th>Name</th>
<th>Mobile</th>
<th>City</th>
<th>Email</th>
<th>Address1</th>
<th>Address2</th>
<th>Action</th>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
//js file
var manageApp = angular.module('manageApp', ['ngRoute', 'angular-growl', 'ngAnimate']);
manageApp.controller('customerManageController', function ($scope, $http, growl) {
$http({
method: 'POST',
url: 'list_customers',
headers: {
"Content-Type": "application/json"
},
}).success(function (data) {
for(var i=0; i<data.length; i++) {
var tableBody = tableBody+('<tr id="'+data[i].id+'"><td>'+data[i].id+'</td><td>'+data[i].name+'</td><td>'+data[i].mobile+'</td><td>'+data[i].city+'</td><td>'+data[i].email+'</td><td>'+data[i].address1+'</td><td>'+data[i].address2+'</td><td><button ng-click="deleteFunc()" class="btn btn-danger"><i class="fa fa-trash" aria-hidden="true"></i></button></td></tr>');
}
$('#tableBody').html(tableBody);
$('#customer_table').DataTable();
});
$scope.deleteFunc = function () {
alert('here');
}
});
The problem is you're inserting new HTML that won't be bound to the controller. Anytime you insert new HTML code with Angular directives that isn't compiled, like you did with JQuery, it won't be bound to the controller unless you use the $compile function to bind it.
There is a way you can do this with your code but instead this should be refactored. You're waiting for data from a POST request before you render it. Instead of binding the HTML after you get the data (this is where $compile would be required) you can instead make use of a $scope which will contain an array of the data and render the HTML based on what is in that array.
The code would look like this:
<section class="content" ng-app="manageApp">
<div growl></div>
<div class="row" ng-controller="customerManageController">
<div class="col-md-12">
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">Customers Information</h3>
</div>
<div class="box-body">
<table class="table table-striped table-bordered" id="customer_table" >
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Mobile</th>
<th>City</th>
<th>Email</th>
<th>Address1</th>
<th>Address2</th>
<th>Action</th>
</tr>
</thead>
<!-- Table now populates with data after it's received -->
<tbody id="tableBody">
<div ng-repeat="for data in receivedData">
<tr id="{{ data.id }}>
<td>{{data.id}}</td>
<td>{{data.name}}</td>
<td>{{data.mobile}}</td>
<td>{{data.city}}</td>
<td>{{data.email}}</td>
<td>{{data.address1}}</td>
<td>{{data.address2}}</td>
<td>
<button ng-click="deleteFunc()" class="btn btn-danger">
<i class="fa fa-trash" aria-hidden="true"></i>
</button>
</td>
</tr>
</div>
</tbody>
<tfoot>
<tr>
<th>ID</th>
<th>Name</th>
<th>Mobile</th>
<th>City</th>
<th>Email</th>
<th>Address1</th>
<th>Address2</th>
<th>Action</th>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
JavaScript:
var manageApp = angular.module('manageApp', ['ngRoute', 'angular-growl', 'ngAnimate']);
manageApp.controller('customerManageController', function ($scope, $http, growl) {
// This will hold the data
$scope.receivedData = [];
$http({
method: 'POST',
url: 'list_customers',
headers: {
"Content-Type": "application/json"
},
}).success(function (data) {
// If here there is data, show it.
if (data.length > 0) {
$scope.receivedData = data;
}
}
});
$scope.deleteFunc = function () {
alert('here');
}
});
I have created page with Datatable and its code is given below
$('#test').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "http://www.****.com/***/patientInfo"
} );
This ajax URL will give list of Patients list in JSON format
<table id="test" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Position</th>
<th>Office</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</thead>
<tfoot>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Position</th>
<th>Office</th>
<th>Start date</th>
<th>Salary</th>
</tr>
</tfoot>
</table>
Now i couldn't achieve it. I got an error, i prompting that Datatable Warning: Table id=test, Invalid JSON response.
I want to show all the JSON data in the Datatable from Ajax URL.
How can I achieve this?