Table data is not cleared after each ajax call. New data is appended below the previous data
$('#show_data').remove();
I have tried this but its not working
View
<table id="example2" class="table table-bordered table-hover">
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Date</th>
<th>Time</th>
<th>Action</th>
</tr>
</thead>
<tbody id="show_data">
</tbody>
</table>
Script
<script>
$(document).ready(function () {
//Date picker
$('#datepicker').datepicker({
autoclose: true,
format: 'yyyy-mm-dd',
});
$("#datepicker").attr("autocomplete", "off");
$(document).on('submit','#myform',function(){
event.preventDefault();
var date = $('#datepicker').val();
console.log(date);
$('#show_data').remove();
$.ajax({
type:"POST",
url:"<?php echo base_url().'admin/Dashboard/get_blog'; ?>",
data:{date:date},
dataType: "json",
success:function(data) {
console.log(data[0].blog_id);
let i;
for(i=0;i<data.length;i++){
$('#show_data').append('<tr><td>' + data[i].article_name + '</td>' +
'<td>' + data[i].description + '</td>' +
'<td>' + data[i].date + '</td>' +
'<td>' + data[i].time + '</td>' +
'</tr>');
}
},
error: function() {
alert("oops...");
},
});
});
});
</script>
Present Output
Input-> 2019-05-25 -> Submit
Output-> Data 1
Data 2
Input-> 2019-05-24 -> Submit
Output-> Data 1
Data 2
Data 3
Expected Output
Input-> 2019-05-25 -> Submit
Output-> Data 1
Data 2
Input-> 2019-05-24 -> Submit
Output-> Data 3
Its because you append your response to previous data. Try this way
$.ajax({
type:"POST",
url:"<?php echo base_url().'admin/Dashboard/get_blog'; ?>",
data:{date:date},
dataType: "json",
success:function(data) {
console.log(data[0].blog_id);
let i;
let html= '';
for(i=0;i<data.length;i++){
html += '<tr><td>' + data[i].article_name + '</td>' +
'<td>' + data[i].description + '</td>' +
'<td>' + data[i].date + '</td>' +
'<td>' + data[i].time + '</td>' +
'</tr>');
}
$('#show_data').html(html);
},
error: function() {
alert("oops...");
},
});
You can try change code
$('#show_data').remove();
to
$('#show_data').empty();
empty() function will clear content of the element. Docs is here https://api.jquery.com/empty/
Related
I am trying to add pagination links in my view but can't use "$data->links()" because I've passed data in my ajax and response in table tbody. Also, I would be appreciated if you could help me to complete my pagination according to my current structure.
function getAllUsers() {
$.ajax({
url: base_url + '/users/listing',
method: 'GET',
success: function(result) {
var html = '';
$.each(result.data, function(i, row) {
html += '<tr>'
html += '<td class="text-center">' + ++i + '</td>'
html += '<td>' + row.name + '</td>'
html += '<td>' + row.email + '</td>'
html += '<td>' + row.phone + '</td>'
html += '<td>' + row.created_at + '</td>'
html += '<td class="text-center"><ul class="table-controls">'
html += '<li></li>'
html += '<li></li>'
html += '</ul></td>'
html += '</tr>';
})
$('table tbody').html(html)
}
});
}
// Routes
Route::get('/users', 'Backend\UserController#index')->name('users');
Route::get('/users/listing', 'Backend\UserController#listing');
//Controller
public function index() {
return view('backend/users/listing');
}
public function listing() {
$users=User::paginate(5);
return $users;
}
<div class="table-responsive">
<table class="table table-bordered table-hover table-striped table-checkable table-highlight-head mb-4">
<thead>
<tr>
<th class="text-center">No.
</th>
<th class="">Name</th>
<th class="">Email</th>
<th class="">Phone</th>
<th class="">Register at</th>
<th class="text-center">Actions</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
When using the ->paginate() helper in your model, you don't only get the data, you get the urls for next and previous links as well on the response:
{
....
"first_page_url": "http://laravel.app?page=1",
"last_page_url": "http://laravel.app?page=4",
"next_page_url": "http://laravel.app?page=2",
"data": ....
}
So, your success function should look similar to this:
success: function(result) {
var html = '';
var nextlink = result.next_page_url;
$.each(result.data, function(i, row) {
....
}
}
My function does not run when pressing the button generated from the refresh table function, please help what makes it not work and I check the console, there is no jquery function visible
here is refresh table function RefreshTable()
function refreshTable()
{
$.ajax({
url: "{{ url('/tabeltrxbarang') }}",
dataType: 'json',
success: function (data) {
var html = '<table class="table table-responsive" width="100%">' +
'<tbody>' +
'<tr class="bg-yellow">' +
'<th class="small-col" width="5%"">No</th>' +
'<th class="name text-center"><b>Nama</b></th>' +
'<th class="small-col"></th>' +
'<th class="small-col" alig="center"></th>' +
'<th class="small-col"></th>' +
'<th class="name">Total</th>' +
'<th class="small-col"></th>' +
'</tr>';
for(var i = 0; i < data.length; i++){
var total = addCommas(data[i].total);
html += '<tr class="bg-gray color-palette">' +
'<td class="small-col">'+ (i + 1) +'</td>' +
'<td class="name" align="left"><b>' + data[i].nama_barang + '</b></td>' +
'<td class="small-col text-center">' +
'<button id="' + data[i].kode_barang + '" class="btn btn-warning btn-xs tambah">' +
'<i class="fa fa-fw fa-plus"></i>'+
'</button>' +
'</td>'+
'<td class="small-col" alig="center">'+ data[i].qty +'</td>' +
'<td class="small-col text-center">' +
'<button id="' + data[i].kode_barang + '" class="btn btn-warning btn-xs kurang">' +
'<i class="fa fa-fw fa-minus"></i>' +
'</button>' +
'</td>'+
'<td class="name" align="right">' + total + '</td>' +
'<td class="small-col">' +
'<button id="' + data[i].kode_barang + '" class="btn btn-danger btn-xs batal">' +
'<i class="fa fa-fw fa-trash-o"></i>' +
'</button>' +
'</td>' +
'</tr>';
}
html += '</tbody>' +
'</table>';
$('#tabeltrxbarang').html(html);
},
error: function (data) {
console.log(data);
},
});
}
I want click this button with value id
<button id="' + data[i].kode_barang + '" class="btn btn-warning btn-xs tambah">
here button click event for run myfunction
$('button.btn.btn-warning.btn-xs.tambah').click(function(){
var id = $(this).attr('id');
TambahQtyBarang(id);
});
and here the function must run
function TambahQtyBarang(kodebarang){
$.ajax(
{
type : "POST",
url : "{{ url('/trx_tambah_qty') }}",
data : {kode_barang:kodebarang},
dataType: "text",
success : function(response)
{
refreshTable();
},
error: function (response) {
console.log('error barang');
},
});
}
here the controller from the URL
public function TambahQty0(Request $request)
{
$kode_barang = $request->kode_barang;
$kode_outlet = Session::get('kode_outlet');
$no_trx = Session::get('no_trx');
$data_trx_barang = TrxBarangmodel::where('kode_barang', $kode_barang)
->where('no_trx', $no_trx)
->where('kode_outlet', $kode_outlet)
->where('status', '2')->first();
$qty = (int)$data_trx_barang->qty + 1;
$total = (double)$qty * (double)$data_trx_barang->harga_jual;
$data_trx_barang_qty = TrxBarangmodel::where('kode_barang', $kode_barang)
->where('no_trx', $no_trx)
->where('kode_outlet', $kode_outlet)
->where('status', '2')
->update(array('qty' => (int)$qty , 'total' => (double)$total));
}
my jquery function is not displayed in Console and can't update the database with that jquery function. What wrong with my script.
Thanks in advance
Instead of this
$('button.btn.btn-warning.btn-xs.tambah').click(function(){
var id = $(this).attr('id');
TambahQtyBarang(id);
});
try to replace this code and check.
$("body").on('click', 'button.btn.btn-warning.btn-xs.tambah', function() {
var id = $(this).attr('id');
TambahQtyBarang(id);
});
This question already has answers here:
jQuery parse JSON multidimensional array
(2 answers)
Closed 3 years ago.
I am submitting the form from ajax, and after ajax success i want to display all data in table tr td.
Below is my response which i got on ajax success and i want to load inside table on ajax success, but its displaying blank table.
{"raildata":null,"killdata":[{"id":146,"acct_id":1885,"AcctNo":"UP2357"},{"id":145,"acct_id":1885,"AcctNo":"UP2357"}]}
Below is my jquery ajax code which i tried, but its not displaying data after form submit on success.
$(document).ready(function(){
$('#killfrm').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: '<?= Router::url(['controller' => 'Killsheets', 'action' => 'addKillsheet']) ?>',
data: $('form').serialize(),
beforeSend: function(){
$('#ibox1').children('.ibox-content').toggleClass('sk-loading');
},
success: function(response) {
var trHTML = '';
$(response).each(function (i,value) {
trHTML += response.killdata.map(function(killdata) {
return '<tr class="gradeA"><td>' + killdata.id + '</td><td>' + killdata.AcctNo + '</td></tr>';
});
trRailHTML += response.raildata.map(function(raildata) {
return '<tr class="gradeA"><td>' + raildata.rail_no + '</td><td>' + raildata.scale_no + '</td><td><button title="View" class="btn btn-default btn btn-xs tblbtn">View</button></td></tr>';
});
});
$('#txtcount').val(sum);
$('#listRail').html(trRailHTML);
$('#listKill').html(trHTML);
},
error: function(response) {
console.log(response);
}
});
});
});
Below is my HTML table
<table class="table table-bordered">
<thead>
<tr>
<th>Sheet#</th>
<th>Acc #</th>
<th>Action</th>
</tr>
</thead>
<tbody id="listKill"> </tbody>
</table>
you can pass $.each:
script:
$(document).ready(function(){
$('#killfrm').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: '<?= Router::url(['controller' => 'Killsheets', 'action' => 'addKillsheet']) ?>',
data: $('form').serialize(),
beforeSend: function(){
$('#ibox1').children('.ibox-content').toggleClass('sk-loading');
},
success: function(response) {
let trHTML = '';
let killData = response.killdata;
let raildata = response.raildata;
$.each(killData, function(kill) {
let killid = kill.id;
let killacct_id = kill.acct_id;
let killAcctNo = kill.AcctNo;
trHTML += '<tr>';
trHTML += '<td>'+killid+'</td>';
trHTML += '<td>'+killacct_id+'</td>';
trHTML += '<td>'+killAcctNo+'</td>';
trHTML += '</tr>';
});
$('#listRail').html(raildata);
$('#listKill').html(trHTML);
},
error: function(response) {
console.log(response);
}
});
});
});
you get result html trHTML in id of #listKill:
html:
<table class="table table-bordered">
<thead>
<tr>
<th>Sheet#</th>
<th>Acc #</th>
<th>Action</th>
</tr>
</thead>
<tbody id="listKill">result goes here </tbody>
</table>
you can get any response data from response.objectkey = get value
for get json data in ajax response you need to pass ajax options :
dataType:"json"
go with below example:
$.ajax({
type: 'post',
url: 'Your Url',
data: $('form').serialize(),
dataType:'json',
success: function(response) {
response == here you get object you can get any value by object key
}
});
your json response:
{"raildata":null,"killdata":[{"id":146,"acct_id":1885,"AcctNo":"UP2357"},{"id":145,"acct_id":1885,"AcctNo":"UP2357"}]}
you can get result by using response variable like below
console.log(response.killdata);
console.log(response.raildata);
try above if you get any help
Table body is appended like this:
Assin id to your table
<table class="table table-bordered" id='my_table'>
Add this row to add the new rows
$("#my_table tbody").append(trHTML);
I would like to add pagination to my existing table which I have programmed using ajax.
I have tried multiple pagination solutions and none of them looks like its compatible with my existing table which I have right now
Table in index.php
<div class="table-responsive">
<table class="table table-bordered table-striped">
<thead>
<th width="5%"></th>
<th width="10%">Serial No.</th>
<th width="20%">Equipment Type</th>
<th width="15%">Document Remarks</th>
<th width="10%">Supplier</th>
<th width="10%">Date In</th>
<th width="10%">Customer</th>
<th width="10%">Date Out</th>
</thead>
<tbody></tbody>
</table>
</div>
Script for showing table in index.php
<script>
$(document).ready(function () {
function fetch_data()
{
$.ajax({
url: "select.php",
method: "POST",
dataType: "json",
success: function (data)
{
var html = '';
for (var count = 0; count < data.length; count++)
{
html += '<tr>';
html += '<td><input type="checkbox" value="' +
data[count].id + '" id="' + data[count].id + '" data-serial_number="' +
data[count].serial_number + '" data-equipment_type="' +
data[count].equipment_type + '" data-document_remarks="' +
data[count].document_remarks + '" data-supplier="' + data[count].supplier +
'" data-date_scan="' + data[count].date_scan + '" data-customer="' +
data[count].customer + '" data-date_out="' + data[count].date_out + '"
class="check_box" /></td>';
html += '<td>' + data[count].serial_number + '</td>';
html += '<td>' + data[count].equipment_type + '</td>';
html += '<td>' + data[count].document_remarks + '</td>';
html += '<td>' + data[count].supplier + '</td>';
html += '<td>' + data[count].date_scan + '</td>';
html += '<td>' + data[count].customer + '</td>';
html += '<td>' + data[count].date_out + '</td></tr>';
}
$('tbody').html(html);
}
});
}
fetch_data();
}
</script>
select.php
include('database_connection.php');
$query = "SELECT * FROM inventory ORDER BY id DESC";
$statement = $connect->prepare($query);
if ($statement->execute()) {
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
$data[] = $row;
}
echo json_encode($data);
}
?>
In your fetch_data() function you will need to include page data in your POST request:
limit should be the number of items viewable per page
offset should be limit multiplied by the page number (starting at 0)
Ex. below would request the second page of results (10 results per page)
$.ajax({
url: "select.php",
method: "POST",
dataType: "json",
data: {limit: 10, offset: 10},
// etc.
The in your PHP script you will need to read these variables and add them to your query:
$limit = $_POST['limit'];
$offset= $_POST['offset'];
$query = "SELECT * FROM inventory ORDER BY id DESC LIMIT = ? OFFSET= ?";
$statement = $connect->prepare($query);
if ($statement->execute([$limit, $offset])) {
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {
$data[] = $row;
}
echo json_encode($data);
}
I am trying to fetch movie information from omdbapi.So far i have this code which is extracting data from imdb using omdb api.But i want import that data to my database.how do i accomplish that.
my code look like this
<form>
<input type="text" id="tst"/>
<input type="button" value="search" id="btn"/>
</form>
<table class="table table-hover" id="imdb">
<thead>
<tr>
<th>Poster</th>
<th>Title</th>
<th>Year</th>
<th>Rated</th>
<th>Runtime</th>
<th>Genre</th>
<th>Director</th>
<th>Actors</th>
<th>Plot</th>
</tr>
</thead>
<tbody></tbody>
</table>
This is jquery code i am using to fetch the movie information
$(document).ready(function () {
$('#btn').click(function(){
var imdbid=$('#tst').val();
var url = "http://www.omdbapi.com/?i="+imdbid+"&plot=short&r=json"
$.ajax({
url:url,
dataType:'json',
success:function (json) {
var tr;
tr = $('<tr/>');
tr.append("<td><img src=" + json.Poster + " width='200' height='297'></td>");
tr.append("<td>" + json.Title + "</td>");
tr.append("<td>" + json.Year + "</td>");
tr.append("<td>" + json.Rated + "</td>");
tr.append("<td>" + json.Runtime + "</td>");
tr.append("<td>" + json.Genre + "</td>");
tr.append("<td>" + json.Director + "</td>");
tr.append("<td>" + json.Actors + "</td>");
tr.append("<td>" + json.Plot + "</td>");
$('#imdb').append(tr);
}
})
})
});
You can create a json object from your api response(in same function where you are creating html).
JSONData = JSON.stringify({
"Title": Title,
"Year": Year,
"Rated" : Rated,
...........so on
});
postDataToserver(JSONData);
And you can define your function to post data like -
function postDataToserver(JSONData)
$.ajax({
url: "your_server_script_path",
type:"POST",
data:JSONData,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(response) {
error: function (error) {
}
});
If you want to send in simple post format not as JSON object then you don't need to use JSON.stringify and similarly in function remove contentype information. Hope this will help you.