$.ajax load select option via dependent()

$.ajax load select option via dependent()

CapamaniaCapamania Posts: 233Questions: 81Answers: 5
edited August 2016 in Editor

I'm trying to load the select option for field_02 via dependent() and $.ajax. The select option in the form is empty though. That's my setup:

editor = new $.fn.dataTable.Editor( {
        ajax: "source.php",
        table: "#mytable",
        fields: [ {
                label: "Field 01",
                name: "field_01"
            },{
                label: "Field 02",
                name: "field_02",
                type: "select"
            },
            ...
        ]
    } );
    
editor.dependent( 'value', function ( val, data, callback ) {
    $.ajax( {
        url: 'source.php',
        dataType: 'json',
        success: function(data) {
            $.each(data.data, function (i, item) {
                test = item.value;
                console.log(test)
                //callback( test )
            });
        },             
    } );
} );

// console.log(test) returns me the values:

Value 01
Value 02
Value 03

How do I get the values in the Select form?

source.php looks like this
{"data":[{"DT_RowId":"row_1","account_id":"1","user_id":"1","value":"Value 01"},{"DT_RowId":"row_2","account_id":"1","user_id":"1","value":"Value 02"},{"DT_RowId":"row_3","account_id":"1","user_id":"1","value":"Value 03"}],"options":[],"files":[]}

This question has an accepted answers - jump to answer

Answers

  • CapamaniaCapamania Posts: 233Questions: 81Answers: 5

    And is it also possible to send a variable to the source similar to the dataTable instance?

    table = $('#mytable').DataTable( {
            dom: "Blfrtip",        
            ajax: {
                  url: "source.php",
                  type: "POST",
                  data: function (d) { 
                            d.UserID = userID; 
                            } },
            serverSide: true,
            ...              
        } ); 
    

    Or some filtering when building the select form?

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    Answer ✓

    And is it also possible to send a variable to the source similar to the dataTable instance?

    Yes, you can specify Ajax options when you call dependent().

    Regarding the first issue, can you give me a link to the page so I can debug it please?

    Thanks,
    Allan

  • CapamaniaCapamania Posts: 233Questions: 81Answers: 5

    Hi Allan, I got it working. I'm not loading the select values via dependent() now, I switched to the JOIN method like for sites.name from your example here: https://editor.datatables.net/examples/simple/join.html

    Many thanks!

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin

    That's great to hear - thanks for posting back!

    Allan

This discussion has been closed.