Datatable Ajax Validation - php

Get the result from API and return the result in JSON and show in another file with data table ajax. Sometimes API not working and it returns false or error in the alert. My question is how do we set validation in data table ajax? If API doesn't respond then they give set an alert. How can we set the alert in data table ajax?
My code is.
$('#playeraction').DataTable( {
"processing": true,
"serverSide": false,
"ajax": {
"url": 'daily_action.php',
"type": "POST",
"dataSrc": "results"
},
"columns": [
{ "data": "country" },
{ "data": "city" },
{ "data": "zipcode" },
{ "data": "currency" },
{ "data": "gender" },
{ "data": "Birthday" },
{ "data": "status" },
{ "data": "RegistrationDate" },
{ "data": "FtdDate" },
{ "data": "Deposit" },
{ "data": "TotalDeposit" },
{ "data": "Cashout" },
{ "data": "TotalCashout" },
{ "data": "Bonuses" },
{ "data": "Revenue" },
{ "data": "Chargeback" },
{ "data": "ReverseChargeback" },
{ "data": "SideGamesBets" },
{ "data": "SideGamesWins" },
{ "data": "JackpotContribution" },
{ "data": "playerid" }
],
});

According to the documentation at https://datatables.net/reference/option/ajax - you can pass in any options which you would be able to pass to jquery's $.ajax() function directly, so you should be able to define an "error" callback (see http://api.jquery.com/jquery.ajax/ for details), e.g.
"ajax": {
"url": 'daily_action.php',
"type": "POST",
"dataSrc": "results",
"error": function(jqXHR, textStatus, errorThrown)
{
alert("An error occurred when loading data");
console.log("data loading error: " + jqXHR.status + " " + jqXHR.statusText + ". Observe the request in the browser's Network tab for more detail.");
}
},

Related

How to implement Ajax call in Datatables server side processing

I have a 20,0000+ data in sever side I have to plot in datatable based on their gender.
I have three categories
All-show all data in list
women+kids data list
men data list
First time load Its all fine by choosing radio button ,In some cases I got all previous data list but the count still shows
Code :
function load_data(preferredGender) {
$("#user_table").DataTable({
"processing": true,
"serverSide": true,
"dataType": "json",
"deferRender": true,
"ajax": {
"url": "<?php echo base_url(); ?>user/list_customers",
"data": {
preferredGender
},
},
"columns": [
{
"data": "row"
},
{
"data": "email"
},
],
});
}
$('#table-id').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "URL",
"type": "POST",
"data": function ( data ) {
data.gender = $('#gender').val();
},
"dataSrc": function ( json ) {
return json.data;
}
},
"columnDefs": [
{
"targets": [ ],
"orderable": false,
},
],
"fnDrawCallback": function() {
},
});
Hi all Thanks for your valuable time.I have fixed the issue
Issue in Sending request to get data on each click and the responding time and data.
on before send in ajax call aborting if any previous request exits and aborting
beforeSend:function(){
if (userTable && userTable.hasOwnProperty('settings')) {
userTable.settings()[0].jqXHR.abort();
}
}
If you have any alternative for this please suggest me
Thanks

Datatable default ordering not applying

I have this code for jQuery Datatables, it works fine, everything is working as it should aside from the order function is not sorting the column on page load (or at all).
What am I doing wrong here? I've read Datatables documentation and this appears to be correct.
$(document).ready(function() {
$('#data').DataTable( {
"ajax": {
"dataType": 'json',
"url": "package.json",
"dataSrc": "search_result",
"order": [ 0, "desc" ]
},
"columns": [
{ "data": "num" },
{ "data": "domain_name",
"render": function(data, type, row, meta)
{
if(type === 'display')
{
data = '<a class="intel_link" href="http://www.' + data + '" target="_blank">' + data + '</a>';
}
return data;}
},
{ "data": "query_time"},
{ "data": "create_date" },
{ "data": "update_date" },
{ "data": "expiry_date" }
]
} );
} );
Problem is with sort attr, its written inside ajax block.
$(document).ready(function() {
$('#data').DataTable( {
"ajax": {
"dataType": 'json',
"url": "package.json",
"dataSrc": "search_result"
},
"order": [ 0, "desc" ]
"columns": [
{ "data": "num" },
{ "data": "domain_name",
"render": function(data, type, row, meta)
{
if(type === 'display')
{
data = '<a class="intel_link" href="http://www.' + data + '" target="_blank">' + data + '</a>';
}
return data;}
},
{ "data": "query_time"},
{ "data": "create_date" },
{ "data": "update_date" },
{ "data": "expiry_date" }
]
} );
} );
For more detail ref Datable Order

How to make Datatable Jquery dynamically

I need to make a table dynamically with DataTable Jquery, the table must be Server-side processing type and in the same request (Ajax) and answer the columns are made and obviously the results.
DatosColumna=""//Json with Response of ajax
$('#table_query_info').dataTable({
columnDefs: DatosColumna,//Here its the headers
destroy: true,
processing: true,
serverSide: true,
ajax: {
url: myBaseUrl + "consults/find_docs",
type: "post",
data: function (d) {
d.datos = make_info()//Send Json with info
},
statusCode: {
200: function (resp) {
alert("OK");//That Good
}
}
}
});
I tried with this code but it has not worked
Thank you very much
steps for resolution:
make a html code
<table class="table" id="tablaindex" style="width:100%"></table>
Obtengo las columnas, antes de inicializar la tabla
$.ajax({
type: "POST",
url: "AtencionTurno/ColumnasDatatable",
data: TblOficina,
success: function (response) {
inicializarTabla(response);
}, error: function (request, status, error) {
console.log("Error");
}
});
The reques obtain the follow json
[
{
"title": "Column 1",
"data": "Id_Turno",
"orderable": false
},
{
"title": "Column 2",
"data": "Turno",
"orderable": false
},
{
"title": "Column 3",
"data": "Clasificacion",
"orderable": false
},
{
"title": "Column 4",
"data": "Concepto",
"orderable": false
},
{
"title": "Column 5",
"data": "Hora",
"orderable": false
}]
now I initialize the table with the columns that were obtained
inicializarTabla = function (columnas) {
var dataTable = $('#tablaindex').DataTable({
"processing": true,
"serverSide": true,
stateSave: true,
orderable: false,
"order": [],
columns: columnas,
"lengthChange": false,
searching: false,
colReorder: false,
"ajax": {
url: "AtencionTurno/IndexTurnos",
type: "post",
data: TblOficina
}
});
};
response from server:
{
"draw": "1",
"recordsTotal": 4,
"recordsFiltered": 4,
"data": [
{
"Id_Turno": "358",
"Turno": "ALAT1",
"Clasificacion": "euismod",
"Concepto": "Convallis",
"Hora": "11:00:12",
"Tiempo_Espera": "2018/07/03 11:00:12",
"Tipo_Cliente": "Calendario",
"Transferencia": "",
"null": "358"
},
{
"Id_Turno": "354",
"Turno": "ALAK23",
"Clasificacion": "euismod",
"Concepto": "Pagos",
"Hora": "16:36:14",
"Tiempo_Espera": "2018/06/29 16:36:14",
"Tipo_Cliente": "Utiles Escolares ",
"Transferencia": "",
"null": "354"
}
]
}

Trouble saving to json file with php and jquery

Forgive me, I am still learning php and jquery. I am trying to save changes to a json file from data attributes.
I have a .json file below
[{
"name": "test1",
"value": "1",
"ID": ""
},
{
"name": "test1",
"value": "1",
"ID": ""
},
{
"name": "test2",
"value": "1",
"ID": ""
}]
I load the file like this:
$str_data = file_get_contents("data.json");
$data = json_decode($str_data, true);
foreach($data as $key => $val)
echo "<li><a data-num='". $val['value'] ."'>". $val['value'] ."</a></li>";
I can change the data attributes like this:
$('li a').click(function(e) {
e.preventDefault();
var value = +$(this).attr("data-num");
console.log(value);
value = value + 1;
console.log(value);
$(this).attr('data-num', value);
$(this).text(value);
});
This is were I'm stuck. I can I save/update the .json file with the new values based upon the updated data attributes?
$("#submit").click(function() {
$.ajax({
type: 'POST',
url: 'save_to_json.php',
success: function(data){
// do something on success
},
error: function(){
// do something on error
}
});

Passing UserID into Datatables

Working a Project here and I need some help. I have a php page and an html page, trying to pass the username of the php into a DataTable that is in my html file. The code that I have for each is:
PHP is below:
public function __construct(Guard $auth)if (Auth::check() ||Auth::attempt()) {
$auth_id = Auth::user()->id;}
My HTML portion is the following:
var table = $('#example').DataTable( {
// Makes one continuous line
"autoWidth": false,
// How many rows to return
"pageLength": 25,
// Setup the search box with the current username to filter the values on the screen - jsg 2/12/2016
"search": {
"search": $('auth')
}, dom: "Bfrtip",
ajax: "../php/staff.php",
columns: [
{
data: null,
defaultContent: '',
className: 'select-checkbox',
orderable: false
},
{ data: "instructor", visible: false },
{ data: "first_name" },
{ data: "last_name" },
{ data: "category" },
{ data: "Metric_text" },
{ data: "response_value" },
{ data: "fkey_course_id", visible: false },
{ data: "course_code" },
{ data: "course_number" },
{ data: "course_section"}
],
My Question is how can I get $auth_id into the search portion of the HTML coding. As you can see I've tried this with $('auth') but its giving me an object error. If I trying a name like "search": "Test" then test populates and it works. Basically I want to pass the username into the search box of the DataTable so it ill show only the usernames rows.
you can use Ajax.
The code looks like this:
$(document).ready(function() {
$('#example').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "scripts/ids-objects.php",
"columns": [
{ "data": "first_name" },
{ "data": "last_name" },
{ "data": "position" },
{ "data": "office" },
{ "data": "start_date" },
{ "data": "salary" }
]
} );
} );
Look at this sample.
Hope it can help you.

Categories