Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

X-Editable - How to pull form data using ajax

I am using x-editable and would like to know how to populate my select element using jquery and ajax.

[EDIT - For clarity]

This is my code:

jQuery(document).ready(function() {

    //toggle `popup` / `inline` mode
    $.fn.editable.defaults.mode = 'popup';

    var getSource = function() {
        var url = "/api/rpc/payments/status_options";
        $.ajax({
            type:  'GET',
            async: true,
            url:   url,
            dataType: "json",

            success: function(responseObject){
            }

        });

    };

    //make status editable
    $('.payments-click').editable({
        type: 'select',
        title: 'Select status',
        placement: 'right',
        value: 2,
        source: getSource()
        /*
         //uncomment these lines to send data on server
         ,pk: 1
         ,url: '/post'
         */
    });

});

I am trying to get the source:

source: getSource()

From the function but am not 100% sure how to return the data from the Ajax call.

like image 238
HappyCoder Avatar asked Sep 14 '26 06:09

HappyCoder


1 Answers

I resolved this with the help of this post: How do I return the response from an asynchronous call?

Here is my solution:

jQuery(document).ready(function() {

    //toggle `popup` / `inline` mode
    $.fn.editable.defaults.mode = 'popup';

    function getSource() {
        var url = "/api/rpc/payments/status_options";
        return $.ajax({
            type:  'GET',
            async: true,
            url:   url,
            dataType: "json"
        });
    }

    getSource().done(function(result) {

        $('.payments-click').editable({
            type: 'select',
            title: 'Select status',
            placement: 'right',
            value: 2,
            source: result
            /*
             //uncomment these lines to send data on server
             ,pk: 1
             ,url: '/post'
             */
        });


    }).fail(function() {
        alert("Error with payment status function!")
    });

});
like image 147
HappyCoder Avatar answered Sep 15 '26 20:09

HappyCoder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!