datatables editor using dependent and ajax

datatables editor using dependent and ajax

raffy4284raffy4284 Posts: 7Questions: 2Answers: 1

My code:

  editor = new $.fn.dataTable.Editor( {
        ajax: base_url+"/Job/DataTables",
        table: "#example",
        "idSrc": "JobList.JobID",
        fields: [ {
                    label: "Job",
                    name: "Job.Name",
              },{
                   label: "Contact",
                   name: "Contact.FirstName",
              },{
                   label: "ContactID",
                   name: "Contact.ContactID",
                   type: "select",
                   options: <?php echo json_encode(range(1,50)) ?> //50 contacts temporary for now
              }
        ]
}); 
editor.dependent('JobList.PrimaryContact',function(val,data,callback) {
        $.ajax({
                url: '/Job/updatePrimaryContacts',
                type: 'post',
                datatype: 'json',
                data: {
                        "PrimaryContactID" : val
                },
                success: function(json){
                        console.log(json);
                        callback(json);
                }
        });
});

 $('#example').DataTable({
        "responsive":true,
        "scrollY": 600,
        dom: "Tfrtip",
        "sAjaxSource": base_url+"/Job/DataTables",
        "sAjaxDataProp":"data",
        columns: [
            {data: 'Job.JobName'},
            {data: 'Contact.FirstName},
            {data: 'Contact.ContactID'}
        ],
        tableTools: {
            sRowSelect: "os",
            sRowSelector: 'td:first-child',
            aButtons: [
                { sExtends: "editor_create", editor: editor },
                { sExtends: "editor_edit",   editor: editor },
                { sExtends: "editor_remove", editor: editor }
            ]
        }
    } );

And here's my controller:

public function updatePrimaryContacts(){
                $Contact = Contacts::find(Request::all()['PrimaryContactID']);
                $ret_val = array();
                $ret_val['values'] = array();
                $ret_val['values']['Contacts.ContactFirst']=$Contact['ContactFirst'];
                return json_encode($ret_val);
        }

So when a user changes the ContactID dropdown, I want it to also change the Contacts' FirstName. It goes to the controller, fetches the contact that has this first name with this corresponding ID, and returns it to the client code.
However...it's not even triggering that ajax call in the dependent() function. I know b/c I tried to do console.log(json) but that console.log doesn't even run
help?

Replies

  • allanallan Posts: 64,032Questions: 1Answers: 10,555 Site admin

    Hi,

    editor.dependent('JobList.PrimaryContact', ...

    There is no field in your Editor field list with that name (unless I'm missing something?).

    Allan

  • raffy4284raffy4284 Posts: 7Questions: 2Answers: 1

    just pm'd you

  • raffy4284raffy4284 Posts: 7Questions: 2Answers: 1
    edited August 2015

    .

This discussion has been closed.