Datatables ajax reload is ignoring dynamically updated request parameters - datatable

I am using server side jquery datatable 1.10.16. I am want to reload the data by requesting the datatable ajax with updated parameters.
To reload the datatable I am using the below datatable api:
$('#user_data').DataTable().ajax.reload();
My datatable initailization is given below:
$('#user_data').DataTable({
// to hide search filter
"searching": false,
"lengthMenu": [[5, 25, 50], [5, 25, 50]],
processing: true,
serverSide: true,
ajax: {
"url": '/admin/getStats',
"data": {user_id: user_id, date_range: JSON.stringify(DATE_RANGE)},
},
columns: [
{data: 'name', name: 'name'},
{data: 'date', name: 'date'},
{data: 'country', name: 'country'}
]
});
DATE_RANGE and user_id are global variables which are updated like this:
$('#dateSelector').on('apply.daterangepicker', function(ev, picker) {
DATE_RANGE[0] = picker.startDate.format('DD-MM-YYYY');
DATE_RANGE[1] = picker.endDate.format('DD-MM-YYYY');
console.log(DATE_RANGE); // here I am getting updated value properly
// reloading the datatable , but the parameters are the previous one
$('#user_data').DataTable().ajax.reload();
});
How to pass updated parameters ?

Disable caching for the table in the oSettings [this param is changing based on the DT version. Refer documentation of the version you are using.]
After calling
table.ajax.reload();
call this immediately:
table.ajax.clearAjaxParams();
Again this changes with version based. Refer documentation.

Finally solved it somehow. In future people may need:
$(document).ready(function() {
fetchData();
});
function fetchData() {
$('#user_data').DataTable({
// to hide search filter
"searching": false,
"lengthMenu": [[5, 25, 50], [5, 25, 50]],
processing: true,
serverSide: true,
ajax: {
"url": '/admin/getStats',
"data": {user_id: user_id, date_range: JSON.stringify(DATE_RANGE)},
},
columns: [
{data: 'name', name: 'name'},
{data: 'date', name: 'date'},
{data: 'country', name: 'country'}
]
});
}
$('#dateSelector').on('apply.daterangepicker', function(ev, picker) {
.....
....
$('#user_data').DataTable().destroy();
fetchData();
});

I think if you just redraw the dataTable, a new post request with the updated parameters will be sent to the server.
$('#dateSelector').on('apply.daterangepicker', function(ev, picker) {
$('#user_data').DataTable().draw();
}

Related

How to pass user_id in ajax route datatable Laravel

I have a datatable in Laravel, here is my code
$(function() {
var user_id = $(this).data('user_id');
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var table = $('.data-table').DataTable({
paging: true,
info: true,
autoWidth: false,
responsive: true,
processing: true,
serverSide: true,
ajax: "{{ route('detail.user', 'user_id')}}",
columns: [{
data: 'DT_RowIndex',
name: 'DT_RowIndex',
orderable: false,
searchable: false,
},
{
data: 'title',
name: 'title',
orderable: false,
},
{
data: 'content',
name: 'content',
orderable: false,
visible: false,
},
{
data: 'progress',
name: 'progress'
},
{
data: 'status',
name: 'status'
},
{
data: 'kpi',
name: 'kpi'
},
{
data: 'target_selesai',
name: 'target_selesai'
},
{
data: 'action',
name: 'action',
orderable: false,
searchable: false
},
]
});
$('body').on('click', '.detailProduct', function() {
...
});
$('#saveBtn').click(function(e) {
...
});
$('body').on('click', '.deleteProduct', function() {
...
});
});
I want to get user_id value from url parameter (ex: http://127.0.0.1:8000/detail/1000000004 ), I've tried code above, but it will return empty value.
Seems like I write wrong code to get url parameter. Because, If I change the ajax: "{{ route('detail.user', 'user_id')}}", into ajax: "{{ route('detail.user', '1000000004')}}", the program will works as expected. What sould I do? thanks in advance
You can not set variable in PHP code using JavaScript. You should not use PHP code on dynamic JavaScript code. There are 2 alternatives :
Use static path instead of Laravel route, just change ajax: "{{ route('detail.user', 'user_id')}}" to ajax: "/detail/"+user_id
Send user_id variable using Laravel blade view arg. on your controller define user_id like below:
return view('index',
['user_id', 1000000004]
);
then call it on the blade as php variable ($user_id) example ajax: "{{ route('detail.user', $user_id)}}"
be sure url in routes/web.php, provide one variable passing, ex /name-routes/{id}
and then in your controller function take the data id, function function_name($id){ }
and the last be sure format return json with format be like { data : [ name : "value name 1", name : "value_name2" ] }

Laravel + datatable+ progress bar real time

i use datatable net and yajra/laravel-datatables
I need to organize the work in this way. The "load" button sends a request for calc all the data. The calculation should be done without waiting for the result of the execution of the request for calculation from the page. And the state of the calculation must be displayed in real time in the progress bar.
In case the “load” button is pressed, the current calculations should stop and restart.
If the page has been closed, the calculations should be stopped.
I assumed that this can be done using "JOBS" + "laravel echo". But how in the “JOBS” to follow that the page has been closed or the button has been pressed. And I don’t know how to do it more correctly.
my code now
table = $('#table').DataTable({
processing: true,
searching: true,
iDisplayLength: 50,
bStateSave: true,
order: [[0, "asc"]],
dom:
"<'row'<'col-sm-4'l><'col-sm-2'B><'col-sm-6'f>>" +
"<'row'<'col-sm-12'tr>>" +
"<'row'<'col-sm-5'i><'col-sm-7'p>>",
renderer: 'bootstrap',
ajax: {
url: "{{ route('data.tables.get.data') }}",
type: 'POST',
"headers": {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
"data": function (d) {
d.start_date = moment.unix(start_date_pic).format("YYYY-MM-DD");
d.end_date = moment.unix(end_date_pic).format("YYYY-MM-DD");
}
},
autoFill: true,
columns: [
{data: 'id', name: 'id', orderable: true,searchable: false},
{data: 'name', name: 'name', orderable: true,searchable: false},
}
});

How to receive other datatables columns

A question regarding AJAX and Laravel. I'm using jquery datatables plugin. Right now everything is working fine and my data is returned as expected please. This is my columns section
columns: [
{data: 'id', name: 'id'},
{data: 'slug', name: 'slug'},
{data: 'title',
render: function (data, type, row, meta) {
return ''+data+''
}, name: 'title' },
{ data: 'body' , name: 'body' },
{ data: 'author' , name: 'author'},
{ data: 'created_at', name: 'created_at' },
]
My question is how do I add the slug to the url of the title?
I assume you're using datatables jquery plugin and you can use row parameter for access the other columns..
render: function (data, type, row, meta) {
return ''+data+''
}, name: 'title' },

JQGrid not loading data from array variable

I have a problem that is confounding me for a few days. Initially, I tested a hard-coded approach within my javascript function which loads local variable containing a simple array as follows:
var myGridData = [
{ ID: "55505", Item: "Mortgage foreclosure", Class: "36", Status: "Pending" },
{ ID: "55506", Item: "Food truck parks", Class: "43", Status: "Pending" }];
for (var i = 0; i <= myGridData.length; i++)
jQuery("#popGrid").jqGrid('addRowData', i + 1, myGridData[i]);
And the above hard-coded approach works. But when I assign the data dynamically to the my GridData variable (based on user selections from another grid, my grid populates with empty rows, one row for each character in the variable (i.e. 154 rows using the above - instead of the two expected). I've double checked the two strings and they are identical (including the correct qualifiers, brackets, etc. so I am at a loss. Any ideas?
Thanks in advance,
Neale
Thanks Oleg, I can look into filling grid during its creation…once I understand everything a bit better. I’m kind of stabbing in the dark. Below, I’ve included code I use to obtain the array data string from the “main” page – which is then passed to the dialog via a hidden field. I’ve followed that up with my JQGrid parameters and colModel:
var temp = document.getElementById('<%=txtArray.ClientID%>').value;
var myGridData = temp;
jQuery("#popGrid").jqGrid({
multiselect: true,
data: myGridData,
datatype: 'local',
colNames: [
'ID',
'Item',
'Class',
'Status',
],
colModel: [
{ name: 'ID', index:'ID', key: true, width:50 },
{ name: 'Item', index: 'Item', key: true, width:100 },
{ name: 'Class', index: 'Class', key: true, width: 150 },
{ name: 'Status', index: 'Status', key: true, width: 250 },
],
multiSort: true,
sortable: true,
loadonce : true,
rownum: 5,
width: 850,
height: 100,
scrollOffset: 1,
shrinkToFit: true,
sortname: 'ID',
viewrecords: true,
gridview: true,
sortorder: "desc",
pager: '#popPager'
});

RowEditor Data Validation in ExtJS 4

I´ve designed an application based on the MVC Pattern. So I also defined my proxies, fields and validators in my model. Here for example is a model for a list of countries:
Model
Ext.define('LT.model.Country',{
extend: 'Ext.data.Model',
fields: [{name: 'id',type: 'int'},
{name: 'name', type: 'string'},
],
validations: [
{type: 'length', field: 'name', min: 2}
],
proxy: {
type: 'rest',
url: '/country',
reader: {
type: 'json'
},
writer: {
type: 'json'
}
}
});
And here is the store for using this model:
Store:
Ext.define('LT.store.Country', {
extend: 'LT.base.store.Base',
model: 'LT.model.Country'
});
The store is shown in a Grid Panel, where i use the RowEditor Plugin to enable adding and editing rows directly in grid view
Grid Panel:
Ext.define('LT.view.country.List' ,{
extend: 'Ext.grid.Panel',
alias : 'widget.country-list',
plugins: [
Ext.create('Ext.grid.plugin.RowEditing', {
clicksToEdit: 2,
clicksToMoveEditor: 1
})
],
store : 'Country',
columns: [
{header: 'ID', dataIndex: 'id', width: 50},
{header: 'Name', dataIndex: 'name', flex: 3, editor: 'textfield'},
],
tbar: [{
xtype: 'button',
action: 'add',
text: 'Add'
},{
xtype: 'button',
action: 'delete',
text: 'Delete'
}]
});
My problem now is, that the Validation Errors are not show if you edit data in grid view. If the data doesn´t match the validation criterias, then it´s not submitted to the proxy (which is fine), but the user also doesn´t get an error message, that the inserted data is invalid.
I found some (not really beautiful) workarounds - but maybe anyone knows another solution to add validation functionality to rowediting in extjs?
Thanks in advance & cheers,
Michael
Integrating Ext.grid.panel validation and Ext.data.Model.validations
The above response talks about the approach that you can follow

Resources