【问题标题】:Easy Auto Complete, referencing deeply nested valuesEasy Auto Complete,引用深度嵌套的值
【发布时间】:2018-02-20 01:55:58
【问题描述】:

我正在使用一个名为“Easy Auto Complete”的简洁的自动完成程序。

该程序运行良好,但是我不知道如何引用深度嵌套的节点集。运行该节目的 javascript 使用一个标记为“listlocation”的属性,该属性会提取嵌套在 JSON 提要根之外的值。

但是,当尝试从“玩家”数据集中提取数据(例如 ID)时,我没有成功(开发人员工具告诉我我没有收到数据)。我尝试过“player”、“playerstatsentry.player”、“cumulativeplayerstats.playerstatsentry”和其他变体。

当我使用 JSON 提要,其中值位于根或 ONE 级别(根据 the documentation)时,该功能非常有效。

引用播放器数组来拉取 ID 值的最佳方法是什么?

JSON

{
"cumulativeplayerstats": {
    "lastUpdatedOn": "2018-02-13 1:59:59 PM",
    "playerstatsentry": 
        [
            { "player": { "ID": "5107" } }
            ....

JAVASCRIPT

var options = {
    url: "assets/data.json",
    listLocation: "cumulativeplayerstats.playerstatsentry.player",
    getValue: "LastName"
    list: {
        match: {
            enabled: true
        }
    }
};
$("#provider-json").easyAutocomplete(options);

【问题讨论】:

  • Javascript 中,您为LastName 编写了代码,问题是关于ID 的选择。无论如何,我为LastName 选择写了答案,你也可以为ID 属性做同样的事情。希望有帮助!!
  • 如果答案有帮助,您可以通过投票接受它。否则,如果有任何问题,请告诉我。

标签: javascript jquery json autocomplete


【解决方案1】:

插件需要objects 和property 名称的集合,它们将用作自动完成帮助。你需要在逻辑上重构data.json文件:

案例一:JSON结构为as时

{
   "cumulativeplayerstats": {
      "lastUpdatedOn": "2018-02-13 1:59:59 PM",
      "playerstatsentry": [
         {
            "player": {
               "ID": "5106"
            },
            "LastName": "Test 1"
         },
         {
            "player": {
               "ID": "5107"
            },
            "LastName": "Test 2"
         },
         {
            "player": {
               "ID": "5108"
            },
            "LastName": "Test 3"
         }
      ]
   }
}

比使用自动完成配置为:

<input id="provider-json" />
<script>
var options = {
    url: "assets/data.json",
    listLocation: function(data) {
        return data.cumulativeplayerstats.playerstatsentry;
    },
    getValue: "LastName",
    list: {
        match: {
            enabled: true
        }
    }
};
$("#provider-json").easyAutocomplete(options);

案例2:因为getValue属性也支持这个插件的功能。如果JSON结构为:

{
   "cumulativeplayerstats": {
      "lastUpdatedOn": "2018-02-13 1:59:59 PM",
      "playerstatsentry": [
         {
            "player": {
               "ID": "5106",
               "LastName": "Test 1"
            }
         },
         {
            "player": {
               "ID": "5107",
               "LastName": "Test 2"
            }
         },
         {
            "player": {
               "ID": "5108",
               "LastName": "Test 3"
            }
         }
      ]
   }
}

使用自动完成代码:

var options = {
    url: "assets/data.json",
    listLocation: function(data) {
        return data.cumulativeplayerstats.playerstatsentry;
    },
    getValue: function(item) {
        return item.player.LastName;
    },
    list: {
        match: {
            enabled: true
        }
    }
};
$("#provider-json").easyAutocomplete(options);

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2020-01-04
    • 2017-07-02
    • 2012-08-29
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多