Passing parameter to ajax

Passing parameter to ajax

nunomalesnunomales 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": [],
    });

Replies

  • nunomalesnunomales Posts: 6Questions: 0Answers: 0
        [HttpPost]
        public JsonResult GetUtilizadoresForTableView(Models.UtilizadoresFiltroViewModel value)
        {
            JsonResult retVal = null;
    
            using (JsonWsClient client = new JsonWsClient(WebApp.CurrentEmpresa.AppServicesUri))
            {
                var p = new UtilizadoresFiltroParams()
                {
                    Nome = value.Nome,
                    Telemovel = value.Telemovel,
                    Email = value.Email,
                    Contexto = value.Contexto,
                    KeyPerfil = value.KeyPerfil,
                    TiposOperacoesActivos = value.TiposOperacoesActivos,
                    TiposOperacoesBloqueados = value.TiposOperacoesBloqueados
                };
    
                string result = client.Post<UtilizadoresFiltroParams>("/utilizadores/GetListaForTableView", p);
    
                retVal = Json(JsonConvert.DeserializeObject<List<Models.UtilizadorTableViewItem>>(result));
            }
            return retVal;
        }
    
  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    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

  • nunomalesnunomales Posts: 6Questions: 0Answers: 0

    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

  • nunomalesnunomales Posts: 6Questions: 0Answers: 0

    //"data": {

        //    "Nome" : 'Nuno',
        //    "telemovel" : $('#Telemovel').val(),
        //    "Email" : $('#Email').val(),
        //    "contexto" : $('#ddlContextos').val(),
        //    "keyPerfil" : $('#KeyPerfil').val(),
        //    "tiposOperacoesBloqueados" : $('#ddlBloqueados').val(),
        //    "tiposOperacoesActivos" : $('#ddlActivos').val(),
        //},
    
  • nunomalesnunomales Posts: 6Questions: 0Answers: 0

    solution do 1st ajax after initialize datatable

  • kthorngrenkthorngren Posts: 21,301Questions: 26Answers: 4,946

    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:

    filter.nome = 'bob';
    filter.telemovel = '2313';
    ....
    

    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

  • nunomalesnunomales Posts: 6Questions: 0Answers: 0

    I already tried this and it did not work. I'm using a version.10.10.15

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    As Kevin notes, we'd need a link to a page showing the issue to be able to offer any further help.

    Allan

This discussion has been closed.