access global variable in ajax call (MVC project)

access global variable in ajax call (MVC project)

montoyammontoyam Posts: 568Questions: 136Answers: 5
edited August 2020 in DataTables 1.10

I defined my controller so that it accepts parameters, which are to be used in the where statement (I am wanting to do this instead of using request.Form[] )

I am having no troubles in the controller. This issue is that when I do a table.ajax.reload() it doesn't seem to be using the global variable

$(document).ready(function () {
    $.fn.dataTable.moment('m/d/yyyy h:mm:ss A');
    var selectedCaseID = 0;

    var CalendaredEventsTable = $('#CalendaredEvents').DataTable({
        dom: "Bfrtip",
        ajax: 'api/CalendaredEvents?caseID=' + selectedCaseID + '&actionID=0',
        columns: [
            { data: "Actions.ActionName", title: "Event" },
            { data: "CalendaredEvents.EventDate", title: "Date Set" },
            {
                data: "CalendaredEvents.ClientNotified",
                title: "Client Notified",
                render: function (data, type, row) {
                    return (data == 1) ? "Yes" : "No";
                }
            },
            { data: "CalendaredEvents.EFileDate", title: "Date E-Filed" },
            { data: "CalendaredEvents.OrderSignedDate", title: "Date Signed" },
            
            {
                data: null,
                title: "Added By",
                render: function (data, type, row) {
                    return row.EnteredByFirstName + ' ' + row.EnteredByLastName;
                }
            },

            { data: "CalendaredEvents.DateAdded", title: "Date Added" }
        ],
        select: true,
        lengthChange: false,
        buttons: [
            { extend: "create", editor: CalendaredEventsEditor, enabled: false },
            { extend: "edit", editor: CalendaredEventsEditor },
            { extend: "remove", editor: CalendaredEventsEditor }
        ]
    });

    CasesTable.on('select', function (e) {
        selectedCaseID = CasesTable.row({ selected: true }).data().Cases.CaseID;
        toggleAndRefresh();

        //.def = set default value
        CaseActionsEditor
            .field('CaseActions.CaseID')
            .def(selectedCaseID);
        CaseDefendantsEditor
            .field('CaseDefendants.CaseID')
            .def(selectedCaseID);
        CaseNotesEditor
            .field('CaseNotes.CaseID')
            .def(selectedCaseID);
        CalendaredEventsEditor
            .field('CalendaredEvents.CaseID')
            .def(selectedCaseID);

    });

    CasesTable.on('deselect', function () {
        selectedCaseID = 0;
        toggleAndRefresh();
    });

selectedCaseID seems to be working everywhere except in line 7 above: ajax: 'api/CalendaredEvents?caseID=' + selectedCaseID + '&actionID=0',

the ajax call always has a zero for caseID.

(oh, by the way, toggleAndRefresh() does ajax.reload() on the datatable in question along with other datatables.)

Again, I know I can use this:

        ajax: {
            url: 'api/CaseNotes',
            type: 'post',
            data: function (d) {
                d['CaseFilterID'] = selectedCaseID;
            }
        },

but I am going to be calling this controller from a few other functions so I want to try to get away using request.Form data

I have used parameters in other ajax calls and they work:

            if (actionID != 0 && actionID != '') {
                $.ajax({
                    url: 'api/Actions?actionID=' + actionID,
                    dataType: 'json',
                    success: function (response) {
                                            //code here
                    }
                })
            }

This question has an accepted answers - jump to answer

Answers

  • montoyammontoyam Posts: 568Questions: 136Answers: 5

    I just discovered how you can change the ajax url for the Editor, but it doesn't seem to work for the DataTable:

            CalendaredEventsEditor.ajax('api/CalendaredEvents?caseID=' + selectedCaseID + '&actionID=0');
            //CalendaredEventsTable.ajax('api/CalendaredEvents?caseID=' + selectedCaseID + '&actionID=0');
    
  • montoyammontoyam Posts: 568Questions: 136Answers: 5
    Answer ✓

    wohoo.. I found it: https://datatables.net/reference/api/ajax.url()

    so inside my toggleAndRefresh function I have:

            CalendaredEventsEditor.ajax('api/CalendaredEvents?caseID=' + selectedCaseID + '&actionID=0');
            CalendaredEventsTable.ajax.url('api/CalendaredEvents?caseID=' + selectedCaseID + '&actionID=0').load();
            CalendaredEventsTable.ajax.reload();
    
This discussion has been closed.