How can I add ajax data into a checkbox?

How can I add ajax data into a checkbox?

yivnyivn Posts: 3Questions: 3Answers: 0
edited May 2015 in Editor

Hi Alan,
here's my javascript:

editor1.on('preOpen', function (e) {
                e.preventDefault();
                
                $.ajax({
                    url: '/api/boss/vipCredit',
                    data: {id:table.row( '.selected' ).data().sc_channel_credits.channelId},

                    success:function(data) {

                        console.log(data);
                        editor1.add({
                            label: "会员卡信息:",
                            name: "sc_org_vips[].name",
                            type: "checkbox",
                            options:data
                        });

                    }
                });

            });

and here's the ajax data format:

[{"id":1,"name":"vip1"}]

When I tried to launch it , there's some error occured.
Would you please tell me how to insert the checkbox value by the option method or there's a correct method to add it?

One more question, due to the ajax delay, which event should I insert the ajax to add the checkbox before the editor opened?

Answers

  • allanallan Posts: 63,364Questions: 1Answers: 10,449 Site admin

    What I would recommend is that you use the update() method of the checkbox field type. Have the sc_org_vips[].name field in the standard fields array, and then update the options using your Ajax call with something like:

    editor1.on('preOpen', function (e) {
        $.ajax({
            url: '/api/boss/vipCredit',
            data: {id:table.row( '.selected' ).data().sc_channel_credits.channelId},
            success:function(data) {
                editor1.field( 'sc_org_vips[].name' ).update( data );
            }
        });
    });
    

    Regards,
    Allan

This discussion has been closed.