Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
81
rated 0 times [  82] [ 1]  / answers: 1 / hits: 19545  / 10 Years ago, fri, november 14, 2014, 12:00:00

I'm sure there are a few (better) ways to do this, but I can't get any way to work. I'm trying to have datatables load new data (from different data source) when a button is clicked.



Here's what I have:



$(document).ready(function() {

$('#datatable2').dataTable( {

ajax: {
url:simple4.php,
type:GET
} ,

paging: true,
pageLength: 20,
order: [[ 2, asc ]],
aoColumns: [
{ bSortable: false, width: 25% },
{ bSortable: true, width: 30% },
{ bSortable: true, width: 15% },
{ bSortable: true, width: 15% },

{ bSortable: true, width: 15% },
{ bSortable: false, width: 0%, visible:false },

],
});

$( #option2 ).click(function() {
table.ajax.url( 'simple3.php' ).load();
});
});


The initial table (from simple4.php) loads fine. I'd like it to change when I click on a button (with id=option2 in this case), but nothing happens when I click the button.



Just in case, here's the button code in case I'm missing something obvious:



<label class=btn btn-default>
<input type=radio name=options id=option2 value=1 autocomplete=off> Compare 1 and 2
</label>


Not sure what the issue is. Any insight would be helpful.



UPDATE: see answers below explanation of the issue. One thing I didn't do, which apparently makes a major difference is using dataTable versus DataTable. You need a capital D and capital T. Here's the fixed code that's working now:



$(document).ready(function() {
var table = $(#datatable2).DataTable({

ajax: {
url:simple3.php,
type:GET
} ,

paging: true,
pageLength: 20,
order: [[ 2, asc ]],
aoColumns: [
{ bSortable: false, width: 25% },
{ bSortable: true, width: 30% },
{ bSortable: true, width: 15% },
{ bSortable: true, width: 15% },

{ bSortable: true, width: 15% },
{ bSortable: false, width: 0%, visible:false },

],

});
$( #option2 ).click(function() {
table.ajax.url( simple4.php ).load();
});
});


One more thing...my function that was supposed to fire when I clicked on my radio button wasn't working. Had to change from this:



$( #option2 ).click(function() {
table.ajax.url( simple4.php ).load();
});


To this:



$('input[id=option2]').change(function(){
table.ajax.url( simple4.php ).load();
});

More From » php

 Answers
3

First, as the others have said the variable 'table' is not set.



Second, when you call



var table = $('#datatable2').dataTable({.....}) 


You are returning a jQuery object that won't have access to any of the DataTables API



To get a DataTables API instance you need to make a call like this:



var table = $('#datatable2').DataTable({....});


This should work, assuming that your data returned by your url is properly formed.



Source: https://datatables.net/faqs/#api


[#68802] Wednesday, November 12, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alanisannettep

Total Points: 695
Total Questions: 96
Total Answers: 91

Location: Australia
Member since Sat, May 27, 2023
1 Year ago
;