This is the code...
var oTable;
var aData;
var row;
$(document).ready(function() {
$('#example tbody tr').live('click', function (event) {
$(oTable.fnSettings().aoData).each(function (){
$(this.nTr).removeClass('row_selected');
});
$(event.target.parentNode).addClass('row_selected');
aData = oTable.fnGetData(this); // get datarow
row = $(this).closest("tr").get(0);
});
/* Add a click handler for the delete row */
$('#delete').click( function() {
$.ajax({
type: "GET",
url: "<?php echo base_url(); ?>Test/DeleteData",
data: "id="+aData[0],
success: function(msg){
//oTable.fnDeleteRow(aData);
var anSelected = fnGetSelected( oTable );
oTable.fnDeleteRow( anSelected[0] );
oTable.fnDeleteRow(oTable.fnGetPosition(row));
}
});
} );
/* Init the table */
oTable = $('#example').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "<?php echo base_url(); ?>Test/Listener",
"sPaginationType": "full_numbers",
"iDisplayLength": 1,
"bSortClasses": false,
"aoColumns": [ { "bVisible": false} , null, null ]
} );
} );
/* Get the rows which are currently selected */
function fnGetSelected( oTableLocal )
{
var aReturn = new Array();
var aTrs = oTableLocal.fnGetNodes();
for ( var i=0 ; i<aTrs.length ; i++ )
{
if ( $(aTrs[i]).hasClass('row_selected') )
{
aReturn.push( aTrs[i] );
}
}
return aReturn;
}
What is happening :-
The server side data gets deleted on clicking delete link.But the Datatable doesnt refresh after deleting a row on server.The usual "processing" message comes after deletion on server.It stays there.The UI row stays there highlighted with message "Processing" in the middle of the page
What i have observed is :-
1)The response of Ajax source after the delete url is called is this :-
{"sEcho":1,"iTotalRecords":1,"iTotalDisplayRecords":1,"aaData":[["11","PD101-DH1234","adsasd"]],"sColumns":"PartId,PartNumber,PartDescription"}
2)After using firebug,i observed no abnormalities.Both the delete and get source requests are executed perfectly with response OK.
3)I am using FireFox 4.1
4)I am using DataTables 1.8.1 build.
Now Question :-
What do I change in the above code so that the datatable UI row gets deleted and datatable refreshes to bring the remaining rows ???
Please help me on this. I am new to datatables. This problem isn't going away. I have tried everything ! Waiting for replies :)
Have you tried calling fnDraw() after deleting the row?
oTable.fnDraw();
Related
First sorry for my bad english.
I am trying to create button in my datatable. The button for delete data in that row. Delete function works. The problem is after the delete, my Datatable did not refresh the content (Only when I refresh the page, the content refreshed).
I already try some suggestion in other's stackoverflow.
My datatble id = "data'
var data as global variable.
This is my JavaScript Code (inside document.ready) :
var temp = $('#data').DataTable
({
"columns":
[
null, null, null, null,
null, null, null,
{"width": "17%"}
]
});
table = JSON.parse('"' + temp + '"');
This is my code for button function
function delete(kode) {
var x = confirm("Do you want to delete this data?");
if (x == true)
{
jQuery.ajax({
type : "POST",
url : "***"
dataType : 'json',
success : function()
{
table.ajax.reload();
alert("delete success");
},
error : function()
{
table.ajax.reload();
alert("delete fail");
}
});
} }
Error in console :
Uncaught Type-error: Cannot read property 'ajax' of undefined
at Object.error (hotel:191)
at u (VM226 jquery.js:2)
at Object.fireWith [as rejectWith] (VM226 jquery.js:2)
at k (VM226 jquery.js:2)
at XMLHttpRequest. (VM226 jquery.js:2)
You need to write below in ajax success. It will retrieve or refresh all the data of it.
var $lmTable = $("#data").dataTable({bRetrieve: true});
$lmTable.fnDraw();
I'm using the datatables library to display some data and would like to update this every 30 seconds with data fetched form a URL. I've followed the api documentation and have implemented the code below to do this:
$( document ).ready(function() {
var table = $('#performance_summary').DataTable( {
ajax: 'https://myjasonurl.com'
} );
setInterval( function () {
table.ajax.reload();
}, 30000 );
});
When the page loads I can see that the correct URL is called to retrieve data, and data is returned in the correct format to display properly in the table (I've checked this works by loading this into the table directly). Unfortunately the resulting datatable when using the ajax call states that it is "loading" but never loads/shows the data, does anyone have any idea of how I fix this?
Try the DataTable().ajax.url function,
Following code works fine for me:
$(document).ready(function() {
var table = $('#performance_summary').DataTable({
paging: false,
searching: false,
ajax: "https://api.myjson.com/bins/897v1",
columns: [{
"title":"Test",
"data": "test"
}]
});
setInterval( function () {
$('#performance_summary').DataTable().ajax.url(
"https://api.myjson.com/bins/897v1"
).load();
}, 3000 );
});
here's the fiddle: https://jsfiddle.net/ju2bmtm7/84/
I'm facing an issue with ajax that several users here also encountered but the proposed solution do not seem to work for my case.
in my index.php file, I have:
<pre>
<script>
function ButtonManager()
{
$( "button" ).click(function()
{
var context = $(this).attr('type');
var page_type = $(this).attr('page');
var referrer = $(this).attr('referrer');
var form_type = $(this).attr('form');
var object_id = $(this).attr('object');
var postData = 'page_type='+page_type+'&form_type='+form_type+'&referrer='+referrer+'&id='+object_id;
$( '#indicator' ).css( "display", "block" );
if (context == 'post_form')
{
var formData = $('#submit_content').serialize();
postData = postData+'&context=post_form&'+formData;
}
if ((context == 'load_form') || (context == 'filter_form'))
{
postData = postData +'&context=load_form';
if (context == 'filter_form')
{
var filter1 = $('select[name=filter1]').val();
var filter2 = $('select[name=filter2]').val();
var filter3 = $('select[name=filter3]').val();
postData = postData + '&filter1='+filter1+'&filter2='+filter2+'&filter3='+filter3;
}
}
$.ajax
({
type: 'POST',
url: 'php/sl.php',
data: postData,
cache: false,
async: false,
dataType: 'html',
success: function(result)
{
ManageLayer(form_type+'_content');
$('#'+form_type+'_content').html(result);
$( '#indicator' ).css( "display", "none" );
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.responseText);
$( '#indicator' ).css( "display", "none" );
},
});
});
}
</script>
<button type="load_form" page="home" referrer="navigation" form="edit" object="">test</button>
</pre>
when a click on the test button, the script calls sl.php to retrieve some html with other buttons in it.
in the output I get from the server I have added:
<pre>
<script>
var myvar=ButtonManager();
</script>
<button type="post_form" page="home" referrer="navigation" form="edit" object="">test2</button>
</pre>
The goal of the ButtonManager function is to manage all my buttons in one function so it needs to be available/known everywhere (in index.php where it's loaded and in all the output I can get from sl.php).
I have added the var myvar=ButtonManager() line because it's the only way I have found to make sure the function is known by the server output. The drawback is that the function is executed multiple times instead of one even if I don't click on the test2 button.
So I'm looking either for a way to prevent my function from being executed multiple times or an alternative to make the function available everywhere.
I don't know what approach would be the best, I'm a casual developper programming for fun and javascript / ajax is not the language I know the best.
Thanks
Laurent
I got the answer from another forum but I wanted to share it in case others are having the same problem.
Code to be used should be like this:
<pre>
$( document ).on("click", "button", function() {
</pre>
It makes the function available to objects that do not exist yet.
I'm trying to copy a row, but I'm using the data from a database, and I'm returning it with JSON. I'd like to know if it's possible to make a copy of a row, and if can I delimited the number of times that I can copy the rows. For example, I need to copy a row with id = 6, but I need to make 5 copies of this row. How can I do that?
You could make main work on server side not on client
In jqgrid decraration something like this:
grid.jqGrid({
...
loadComplete: function(data) {
var ids = grid.jqGrid('getDataIDs');
for (var i=0;i<ids.length;i++) {
var id=ids[i];
var rowData = thisGrid.jqGrid('getRowData', id);
grid.jqGrid('setCell', id, 'buttons', '<input type="button" class="copy" value="Copy">');
}
$('button.copy').click(function (e) {
var id = $(e.target).parents('tr')[0].id;
$.ajax({
url: 'some_url',
dataType : "json",
data: {'id': id},
timeout: 10000,
type: 'POST',
success: function (data) {
grid.trigger('reloadGrid');
},
});
});
}
....
});
In php part something like this
$array = array(
'success'=>false
);
if(isset($_POST['id])) {
if($db -> num_rows($q = $db->query("SELECT FROM ... WHERE `id` = ".intval($_POST['id'])) == 1) {
$row = $db -> fetch_assoc($q);
$db->query("INSERT INTO .....);
$array['success'] = true;
}
}
echo json_encode($array);
If you want to copy more than one time you could add some dialog on client side to input number of copies
use getRowData To Grab a Single row from jqgrid and then create an array by var s = new Array() and push the single row no of times you want ..
create a new column inside jqgrid .. using formatter Formatter In jQgrid place a link inside that column write handler for click over that link .. on that handler use getrowdata and grab the fields and then make a ajax call to send to the server...
Currently I'm trying to make an ajax entry delete function, when I click on delete the entry is being deleted correctly. When I click on delete, the whole row should disappear, instead of only the td where the delete url is placed (that's happening now, the script can't stand closing tags or something).
This is the code
$(function() {
$(".delete_button").click(function() {
var id = $(this).attr("id");
var dataString = 'id='+ id ;
var parent = $(this).parent();
$.ajax({
type: "POST",
url: "core/actions/delete.php",
data: dataString,
cache: false,
beforeSend: function()
{
parent.animate({'backgroundColor':'#fb6c6c'},300).animate({ opacity: 0.35 }, "slow");;
},
success: function()
{
parent.slideUp('slow', function() {$(this).remove();});
}
});
return false;
});
});
and the last td of the tr
<td>X</td>
I think the javascript should be changed a little, but don't know what/where.
Do you know how to? Or do can you point me to the best ajax delete entry tutorial? (tried several..)
var parent = $(this).parent();
In the context where you wrote the line above "this" refers to the <td> which is why it disappears. You could change to $(this).parents('tr:first');
http://jsfiddle.net/Ed3mW/