I'm currently using Codeigniter and using Ajax to post my search result through the server and result the result and display on my responsive datatable. However the result was not added into my datatable and i even try to add it with .append but it just added below my table and the search function on datatable is not searching it and it still show "No data available in table".
Here are my View :
<div id="reporting" class="container">
<h3 class="section-title">Export Time Location Panel</h3>
<form id = "search">
<div class="form-group">
<div class="form-group">
<h5>Date/ Time</h5>
<input type="datetime-local" name="datetime" id = "datetime">
<?php echo form_error('datetime'); ?>
</div>
</div>
<button type="submit" class="btn btn-primary">Confirm</button>
</form><br/>
<table class="table table-bordered table-light" >
<thead>
<tr>
<td class="id">ID</td>
<td class="na">NA Name</td>
<td class="centre">Centre Name</td>
<td class="login">Date & Time</td>
<td class="lat">Latitude</td>
<td class="long">Longtitude</td>
<td class="accuracy">Accuracy</td>
<td class="place">Location Name</td>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
<?php include('footer.php'); ?>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function() {
var tbl = $('.table').DataTable({
responsive: true,
"order": [[0, "desc"]]
})
$( "#search" ).submit(function(event) {
event.preventDefault();
var value = $("#datetime").val();
$.ajax({
type:'POST',
url:'<?php echo base_url(); ?>location/search_record',
data:{'datetime':value},
success:function(data){
$('.table').append(data);
}
});
});
} );
</script>
Here are my Model :
public function search_record(){ //Display Record at View Page
$this->load->model('location_model');
$this->load->library('form_validation');
echo '
<tr>
<td class="id">ID</td>
<td class="na">NA Name</td>
<td class="centre">Centre Name</td>
<td class="login">Date & Time</td>
<td class="lat">Latitude</td>
<td class="long">Longtitude</td>
<td class="accuracy">Accuracy</td>
<td class="place">Location Name</td>
</tr>
';
}
You have to add data to table body by
success:function(data){ tbl.rows.add($(data)).draw(); }
Please check this link
https://jsfiddle.net/kuLcqwav/3/
https://datatables.net/examples/api/add_row.html
Related
Please I have products(Cart) in session but loads very slow on server anytime I reload a table to display items added to the cart. How do I quickly display session data using ajax or JQuery? Kindly assist.After adding Item to cart. $("#tabler").load(" #tabler") Loads slowly. Please is there any better way to do this.
$("#newinvoice").on('submit', function(event) {
event.preventDefault();
$.ajax({
type: "post",
url: "{{route('InvoiceDump')}}",
dataType: "json",
data: $('#newinvoice').serialize(),
success: function(data){
document.getElementById('price').value = "";
document.getElementById('total').value = "";
document.getElementById('quantity').value = "1";
document.getElementById('ddlViewBy').value = "";
$("#tabler").load(" #tabler");
},
error: function(data){
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Something went wrong!',
footer: 'Why do I have this issue?'
})
}
});
});
My Table
<table class="table table-bordered table-striped m-0">
<thead>
<tr>
<th colspan="7" class="pt-3 pb-3">Invoice Details</th>
</tr>
<tr>
<th width="50%">Product</th>
<th width="50%">Category</th>
<th>Quantity</th>
<th width="20%">Price</th>
<th width="30%">Amount (Net)</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
#foreach($cartItems as $key => $cart)
<tr>
<td><select class="form-control"><option>{!!#strtoupper($cart['invoice_product_name'])!!}</option></select></td>
<td><input type="text" value="{{#strtoupper($cart['item_category'])}}" class="form-control" readonly></td>
<td>
<!-- Field wrapper start -->
<div class="field-wrapper m-0">
<input type="text" readonly class="form-control" value="{{empty($app->currency) ? "GHS" : $app->currency}}#convert($cart['invoice_quantity'])" placeholder="Qty">
</div>
<!-- Field wrapper end -->
</td>
<td>
<!-- Field wrapper start -->
<div class="field-wrapper m-0">
<input type="text" readonly class="form-control" value="{{empty($app->currency) ? "GHS" : $app->currency}}#convert($cart['invoice_unit_price'])">
</div>
<!-- Field wrapper end -->
</td>
<td>
<!-- Field wrapper start -->
<div class="field-wrapper m-0">
<input type="text" readonly class="form-control" value="{{empty($app->currency) ? "GHS" : $app->currency}}#convert($cart['invoice_total_price'])">
</div>
<!-- Field wrapper end -->
</td>
<td>
<div class="table-actions">
<button class="btn btn-light" id="{{$cart['id']}}" onClick="DeleteItem(this.id)">
<i class="icon-trash-2"></i>
</button>
</div>
</td>
</tr>
#endforeach
</tbody>
</table>
I have a tree structure where on hover of the user image it should show some of other user related info but instead of show that info in proper div, its show raw HTML data.
HTML Code
there can be n number of a href
<a href="{{ route('user-binary-tree',[ 'eUserID' => $ERight12]) }}" class="bt-element binary-tree-on-hover" id="{{$ERight12}}">
<img class="rounded-circle" style="width: 50px" src="{{ asset($packageImg) }}">
{{ $Right12[0]->sponsor_code }}
</a>
JavaScript code
$('.binary-tree-on-hover').mouseover(function() {
var userId = this.id;
var $this = $(this);
$.ajax('/binary-tree-ajax/' + userId + '/html', {
method: "GET",
dataType: "html",
success: function(data) {
$this.attr('title', data).mouseover(); //HTML DATA SHOW IN HOVER
}
});
});
Ajax response is
<div class="bt-details">
<table class="table color-table inverse-table">
<thead>
<tr>
<td class="text-center" style="font-size: 100%">Chethan k Test</td>
<td class="text-center" style="font-size: 100%"></td>
<td class="text-center">
<span class="btn btn-danger btn-xs btn-outline" style="border-width: 2px;">Inactive</span>
</td>
</tr>
<tr>
<td class="text-center" style="font-size: 100%">Sponsor ID</td>
<td class="text-center" style="font-size: 100%" colspan="2">UNO187175</td>
</tr>
<tr>
<td class="text-center" style="font-size: 100%">Binary Qualified</td>
<td class="text-center" style="font-size: 100%" colspan="2">
<span class="btn btn-danger btn-xs btn-outline" style="border-width: 2px;">No</span>
</td>
</tr>
<tr>
<th width="40%" class="text-center">Position</th>
<td class="text-center" style="font-size: 95%">Left</td>
<td class="text-center" style="font-size: 95%">Right</td>
</tr>
</thead>
<tbody>
<tr>
<th width="40%" class="text-center">Downline</th>
<td class="text-center">39</td>
<td class="text-center">6</td>
</tr>
<tr>
<th width="40%" class="text-center">Business</th>
<td class="text-center">$0</td>
<td class="text-center">$0</td>
</tr>
<tr>
<th width="40%" class="text-center">Carry</th>
<td class="text-center">$0</td>
<td class="text-center">$0</td>
</tr>
<tr>
<th width="40%" class="text-center" rowspan="2">Binary Generated</th>
<td class="text-center" style="font-size: 95%">Last Day</td>
<td class="text-center" style="font-size: 95%">Till Date</td>
</tr>
<tr>
<td class="text-center">$0</td>
<td class="text-center">$</td>
</tr>
</tbody>
</table>
When i hover on any of the user image its shows row HTML data instead of proper div as seen in 1st image
Also its keep calling the Ajax request infinite times but when i remove $this.attr('title', data).mouseover(); its called Ajax request only once. so i think the infinite calling happening because of this line only.
here how its shows
but i want to show like this
Note : 2nd one is working but its not ajax call and it will fetch all the user info from controller and load on view hence when user increase it will slow down the page so i am trying to call ajax on hover of the image.
End up with using popover function
HTML Code
<div id="popover_html" style="display:none;">
hover_data
</div>
JavaScript Code
jQuery('.binary-tree-on-hover').popover({
title: popoverContent,
html: true,
placement: 'bottom',
trigger: 'hover'
});
function popoverContent() {
var content = '';
var element = $(this);
var id = element.attr("id");
var APP_URL = "{{ url('/')}}";
const adminRoutePrefix = $("#admin_route_prefix").text();
$.ajax({
url: APP_URL + '/' + adminRoutePrefix + '/networks/binary-tree-ajax/' + id + '/html',
method: "GET",
async: false,
// dataType: "HTML",
success: function(data) {
content = $("#popover_html").html();
content = content.replace(/hover_data/g, data);
}
});
return content;
}
Currently I have the following table made with html
<table class="table table-dark table-bordered">
<tr>
<td>Nombre</td>
<td><?= $nombre?></td>
</tr>
<tr>
<td>Apellido</td>
<td><?= $apellido?></td>
</tr>
<tr>
<td>Email</td>
<td><?= $email?></td>
</tr>
<tr>
<td>DNI</td>
<td><?= $dni?></td>
</tr>
<tr>
<td>Rol</td>
<td><?= $rol?></td>
</tr>
<?php if($usuarios[$i]["IdRol"] == '2'){ ?>
<tr>
<td colspan="2">
<!-- -->
<form class="form-horizontal" method="post">
<fieldset>
<div class="form-group">
<input type="hidden" id="dniOculto" name="dni" value="<?= $dni?>">
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="ver"></label>
<div class="col-md-4">
<button id="ver" name="ver" class="btn btn-primary">Ver líneas</button>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#ver").click(function(e){
e.preventDefault();
$.ajax({
type: 'POST',
url: 'adminVerLineas.php',
// data: {
// tlf: $("#num").val(),
// idTarifa: $("#opcion").val()
// },
success: function(data) {
$("#postInfo").append(data);
}
});
});
});
</script>
<!-- -->
</fieldset>
</form>
<table class="table" id="postInfo" ></table>
</td>
</tr>
<?php } ?>
</table>
Point is that its printed multiple times because its printed with a for based on the results of a database.
So its located inside this php for
for($i = 0 ; $i < count($usuarios); $i++){ //the table is there }
It basically prints all the user data, and in each table ive setup a button with ajax which should append new data just below the button.
The called script is as simple as this
<?php
echo "<tr>
<td style='color : white'>Número</td>
<td style='color : white'> 1</td>
</tr>
<tr>
<td style='color : white' >Minutos Gastados</td>
<td style='color : white' >1 / 1</td>
</tr>
<tr>
<td style='color : white' >Megas Gastados</td>
<td style='color : white' >1 / 1</td>
</tr>
<tr>
<td style='color : white' >Tarifa</td>
<td style='color : white' >1</td>
</tr>";
?>
The point is that when I click the button, it always posts the data below the first table, but I have more tables with the same button, and no matter what button i press, it will always post the data below the first table, instead of below the clicked button.
I have this for generating php buttons, and I want to append a specific data right after the button in the table that's right below.
This is my html
<tr>
<td colspan="2">
<!-- -->
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="ver"></label>
<div class="col-md-4">
<button data-dnipass="<?= $dni?>" class="ver" name="ver" class="btn btn-primary">Ver líneas</button>
</div>
</div>
<table class="table userInfo" data-formpost="<?= $dni?>"></table> <!-- This is were i want to append -->
</td>
</tr>
Note that its located inside a for loop
for($i = 0 ; $i < count($usuarios); $i++){ }
And this is my Ajax/jQuery call
<script type="text/javascript">
$(document).ready(function(){
$(".ver").click(function(e){
e.preventDefault();
$.ajax({
type: 'POST',
url: 'adminVerLineas.php',
data: {
dni: $(".ver").data("dnipass")
},
success: function(data) {
alert(data);
$(".userInfo").data("formpost").append(data);
}
});
});
});
</script>
The alert works properly, but the append isn't targeting the specific table below each button
EDIT: after testing the first answer still not works, this is how I see my html on inspection.
After adding to my Ajax:
alert(data);
$(".userInfo").data("formpost", data);
--
<td colspan="2">
<!-- -->
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="ver"></label>
<div class="col-md-4">
<button data-dnipass="12345678B" class="ver" name="ver">Ver líneas</button>
</div>
</div>
<table class="table userInfo" data-formpost="12345678B"></table>
</td>
This is adminVerLineas.php
<?php
echo "<tr>
<td style='color : white'>Número</td>
<td style='color : white'> 1</td>
</tr>
<tr>
<td style='color : white' >Minutos Gastados</td>
<td style='color : white' >1 / 1</td>
</tr>
<tr>
<td style='color : white' >Megas Gastados</td>
<td style='color : white' >1 / 1</td>
</tr>
<tr>
<td style='color : white' >Tarifa</td>
<td style='color : white' >1</td>
</tr>";
?>
If you want to insert data into the data-formpost attribute you have to use $(".userInfo").data("formpost", data)
$(".userInfo").data("formpost") get the data from data-formpost
$(".userInfo").data("formpost", data) sets the data in data-formpost
if your data contains table html, then you have to use $(this).closest("tr").find(".userInfo").append(data)
I try to submit form using isset($_POST(['submit'])) in php file which access through ajax but it doesn't work. Please find the code snippets below:
<html>
<script>
$(document).ready(function() {
$.ajax({
url: './B.php',
type: 'POST',
success: function(data)
{ $("#showB").html(data);}
});
});
</script>
<body>
<table style="width: 100%">
<tr>
<td colspan="2"><div id="showB"></div></td>
</tr>
</table>
</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td> </td>
</tr>
</table>
</body>
</html>
B.php:
<html>
<?php
if(isset($_POST['submit']) && !empty($_POST['submit'])) {
$message= "halo";
}
?>
<head>
<title>Untitled 1</title>
</head>
<body>
<table>
<tr>
<td style="height: 30px"></td>
<td style="height: 30px"><?php if(!empty($message)) { echo $message; } ?></td>
<td style="height: 30px"></td>
<td style="height: 30px; width: 298px;"></td>
</tr>
<form method="post" action="halo.php">
<tr>
<td> </td>
<td>
<input name="submit" type="submit" value="Save Changes"/></td>
<td> </td>
<td style="width: 298px">
</td>
</tr>
<tr>
<td style="width: 244px; height: 26px;"><h5>Effective Width : </h5></td>
<td style="width: 262px; height: 26px;"><input name="effectivewidth" type="text"/></td>
<td style="width: 261px; height: 26px;"></td>
<td style="width: 298px; height: 26px;"></td>
</tr>
</form>
</table>
</body>
</html>
The form in B.php cannot be submit with no error. Please help. thanks
Its because you are not passing any data over post.
<script>
$(document).ready(function() {
$.ajax({
url: './B.php',
type: 'POST',
data: { submit: "submit"},
success: function(data)
{ $("#showB").html(data);}
});
});
</script>
Pass the appropriate data to $.ajax
https://api.jquery.com/jQuery.ajax/