首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将带有数组的JSON对象导入ajax自动完成响应?

如何将带有数组的JSON对象导入ajax自动完成响应?
EN

Stack Overflow用户
提问于 2019-01-06 12:04:56
回答 1查看 111关注 0票数 0

我正在开发的一个公司电子邮件生成应用程序有一个自动完成输入,可以将电子邮件主题数据自动填充到表单中。数据以JSON对象的形式返回,但是有两个对象值extrapsextraul包含多维数组。我能够在响应中获得普通键:值数据,但是我似乎不知道如何拉入数组,以便循环它们以更新表单的某些部分。

下面是一些JSON代码:

代码语言:javascript
复制
0:
    emaildate: "2019-01-10"
    extraps: Array(2)
        0: {extrap: "test paragraph", position: 1}
        1: {extrap: "another paragraph", position: 3}
        length: 2
        __proto__: Array(0)
    extraul: Array(4)
        0: {ulid: 1, position: 2, li: "list item 1", liposition: 1}
        1: {ulid: 1, position: 2, li: "list item 2", liposition: 2}
        2: {ulid: 1, position: 2, li: "list item something new", liposition: 3}
        3: {ulid: 1, position: 2, li: "A new list item", liposition: 4}
        length: 4
        __proto__: Array(0)
    id: 44
    label: "Some Kind of Email Theme - 2019-01-10"
    lastupdated: "2019-01-06 02:00:04"
    store: "Premier"
    themedesc: "Here's a description of the theme."
    themeimage: null
    themeincludeextrap: 1
    themeincludeul: 1
    themelink: "some-kind-of-email-theme"
    themelinkinclude: 1
    themename: "Some Kind of Email Theme"
    themenotes: "Some notes about it"
    themesortorder: 0
    value: "Some Kind of Email Theme"
    __proto__: Object
    length: 1
__proto__: Array(0)

下面是从autotheme.php引入的javascript:

代码语言:javascript
复制
//Autofill Theme Info based on text entry
$( "#themename" ).autocomplete({
    source: function( request, response ) {
        $.ajax({
            url: "autotheme.php",
            type: "GET",
            dataType: "json",
            data: {
                q: request.term
            },
            success: function(data) {
                console.log(data);
                response($.map(data, function(item) {
                    return {
                        id: item.id,
                        value: item.value,
                        label: item.label,
                        themename: item.themename,
                        themenotes: item.themenotes,
                        themedesc: item.themedesc,
                        themeimage: item.themeimage,
                        themeincludeextrap: item.themeincludeextrap,
                        themeincludeul: item.themeincludeul,
                        themelinkinclude: item.themelinkinclude,
                        themelink: item.themelink,
                        themeextraps: item.extraps,
                        themeextraul: item.extraul
                    }
                }))
            },
            error: function(errorThrown){
                console.log(errorThrown);
                console.log("There is an error with theme autocomplete.");
            }
        });
    },
    minLength: 2,
    select: function(event, ui) {
        if (ui.item) {
            $this = $(this);
            $('#themeid').val('');
            $('#extratext').html('');
            $('#themename').val(ui.item.themename);
            $('#themenotes').val(ui.item.themenotes);
            $('#themedesc').val(ui.item.themedesc);
            var themeimage = ui.item.themeimage;
            var themeincludeextrap = ui.item.themeincludeextrap;
            var themeincludeul = ui.item.themeincludeul;
            var themelinkinclude = ui.item.themelinkinclude;
            var themeextraps = ui.item.extraps;
            var themeextraul = ui.item.extraul;
            if(themeextraps !== undefined) {
                var extrapcount = themeextraps.length;
            }
            if(themeextraul !== undefined) {
                var extraulcount = themeextraul.length;
            }
            if((themeextraps !== undefined) || (themeextraul !== undefined)) {
                var extratextpositions = {};
                $.each(themeextraps, function(i, themeextraps) {
                    extratextpositions[themeextraps.position] = 'p';
                })
                $.each(themeextraul, function(i, themeextraul) {
                    extratextpositions[themeextraul.position] = 'ul';
                })
                $.each(extratextpositions, function(key, value) {
                    if(extratextpositions[key] == 'p') {
                        addExtraP.call(this);
                    } else {
                        addExtraUl.call(this);
                    }
                });
                $('#themelink').val(ui.item.themelink);
                if(themelinkinclude == 1) {
                    $('#themelinkinclude').prop("checked", true);
                } else {
                    $('#themelinkinclude').prop("checked", false);
                }
                event.preventDefault();
            }
        },
        open: function(event, ui) {
            $(".ui-autocomplete").css("z-index", 1000);
        },
        complete: function(){
            $("#themename").removeClass("ui-autocomplete-loading");
        }
    }
}); 

我能够获得简单的key:value值,但是我没有为数组定义值。我确信我需要一种不同的方法来吸引他们,但我不知道如何,似乎在这里的其他帖子中找不到答案。任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

发布于 2019-01-07 04:25:31

多亏了@Bibberty的帮助,我找到了解决这个问题的方法。我不确定这是不是解决这个问题最优雅或最简单的方法,但它对我很有效。我从JSON数据值创建了一个数组,然后从数据数组中的数组创建变量,并将它们添加到响应返回值中。下面是新的函数代码(或者,至少是重要的部分):

代码语言:javascript
复制
//Autofill Theme Info based on text entry
    $( "#themename" ).autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "autotheme.php",
                type: "GET",
                dataType: "json",
                data: {
                    q: request.term
                },
                success: function(data) {
                    const results = data.map(function (value, label) {
                        return [value];
                    })
                    var extraps = results[0][0]['extraps'];
                    var extraul = results[0][0]['extraul'];
                    response($.map(data, function(item) {
                        return {
                            id: item.id,
                            value: item.value,
                            label: item.label,
                            themename: item.themename,
                            themenotes: item.themenotes,
                            themedesc: item.themedesc,
                            themeimage: item.themeimage,
                            themeincludeextrap: item.themeincludeextrap,
                            themeincludeul: item.themeincludeul,
                            themelinkinclude: item.themelinkinclude,
                            themelink: item.themelink,
                            extraps: extraps,
                            extraul: extraul
                        }
                    }))
                    $("#themename").removeClass("ui-autocomplete-loading");
                },
                error: function(errorThrown){
                    console.log(errorThrown);
                    console.log("There is an error with theme autocomplete.");
                }
            });
        },
        minLength: 2,
        select: function(event, ui) {
            if (ui.item) {
                $this = $(this);
                console.log(ui.item.extraps);
                $('#themeid').val('');
                $('#extratext').html('');
                $('#themename').val(ui.item.themename);
                $('#themenotes').val(ui.item.themenotes);
                $('#themedesc').val(ui.item.themedesc);
                var themeimage = ui.item.themeimage;
                var themeincludeextrap = ui.item.themeincludeextrap;
                var themeincludeul = ui.item.themeincludeul;
                var themelinkinclude = ui.item.themelinkinclude;
                var themeextraps = ui.item.extraps;
                var themeextraul = ui.item.extraul;
                if(themeextraps !== undefined) {
                    var extrapcount = themeextraps.length;
                }
                if(themeextraul !== undefined) {
                    var extraulcount = themeextraul.length;
                }

                ...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54058533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档