Passing parameter to ajax
Passing parameter to ajax
nunomales
Posts: 6Questions: 0Answers: 0
$(function () {
var grelhaResultados = $('.datatable').DataTable({
"oLanguage": { "sUrl": "/js/dataTables-pt-PT.txt" },
"ajax": {
"url": '/Utilizadores/GetUtilizadoresForTableView',
"data": function (d) {
var filter = {};
filter.nome = $('#Nome').val();
filter.Telemovel = $('#Telemovel').val();
filter.Email = $('#Email').val();
filter.Contexto = $('#ddlContextos').val();
filter.KeyPerfil = $('#KeyPerfil').val();
filter.TiposOperacoesBloqueados = $('#ddlBloqueados').val();
filter.TiposOperacoesActivos = $('#ddlActivos').val();
d.value = filter;
debugger;
return JSON.stringify(d);
},
//"data": {
// "Nome" : 'Nuno',
// "telemovel" : $('#Telemovel').val(),
// "Email" : $('#Email').val(),
// "contexto" : $('#ddlContextos').val(),
// "keyPerfil" : $('#KeyPerfil').val(),
// "tiposOperacoesBloqueados" : $('#ddlBloqueados').val(),
// "tiposOperacoesActivos" : $('#ddlActivos').val(),
//},
"dataType": "json",
"contentType": "application/json; charset=utf-8",
"type": "Post",
"dataSrc": ""
},
"processing": true,
"filter": false,
"orderMulti": true,
"pageLength": 50,
"lengthChange": false,
"autoWidth": true,
"columns": [
{ data: "nome" },
{ data: "telemovel" },
{
"data": "email",
"render": function (data, type, row, meta) {
return renderEmailAsLink(data);
}
},
{ data: "descricaoContexto" },
{ data: "descricaoPerfil" },
{
data: "bloqueado",
render: function (data, type, row) {
return renderBoolAsCheckbox(data);
},
},
{
data: "activo",
render: function (data, type, row) {
return renderBoolAsCheckbox(data);
},
},
{
"data": "keyUtilizador",
"render": function (data, type, row, meta) {
var linkEditDetailsDelete = "";
linkEditDetailsDelete += "<a href='#' id='edit" + data + "' onmousedown='buildEditLink(" + data + ")' >Alterar</a>";
linkEditDetailsDelete += renderSpan();
linkEditDetailsDelete += "<a href='#' id='det" + data + "' onmousedown='buildDetailsLink(" + data + ")' >Detalhes</a>";
linkEditDetailsDelete += renderSpan();
linkEditDetailsDelete += "<a href='#' id='del" + data+ "' onmousedown='buildDeleteLink(" + data + ")' >Remover</a>";
return linkEditDetailsDelete;
}
}
],
"columnDefs": [
{ targets: 'no-sort-col', orderable: false },
{ "className": "text-left", "targets": 0 },
{ "className": "text-left", "targets": 1 },
{ "className": "text-left", "targets": 2 },
{ "className": "text-left", "targets": 3 },
{ "className": "text-left", "targets": 4 },
{ "className": "text-center", "targets": 5 },
{ "className": "text-center", "targets": 6 },
{ "className": "text-center", "targets": 7 }
],
"order": [],
});
This discussion has been closed.
Replies
Not actually sure what your question is.
ajax.data
is what you would use to control the data being sent to the server, which it looks like you are already using.Allan
I can not pass parameters to the ajax request at startup or filtering.
when the function arrives the parameters are always empty what I am doing wrong
//"data": {
solution do 1st ajax after initialize datatable
Maybe I'm missing something but are the elements populated with data when you initialize Datatables, for example:
$('#Nome').val()
?Are you reinitializing Datatables to perform filtering?
Maybe you can provide a link to your page or build a test case showing the issue.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
In your ajax data function what happens if you specifically assign values to the variable filter, for example:
In place of
filter.nome = $('#Nome').val();
just as a test. Are the parameters then passed to your server?The problem seems to be outside of Datatables. More information or a link to your web page is needed to help.
Kevin
I already tried this and it did not work. I'm using a version.10.10.15
As Kevin notes, we'd need a link to a page showing the issue to be able to offer any further help.
Allan