代码之家  ›  专栏  ›  技术社区  ›  Haizat Haneefa

从JSON数据在a帧中生成新行

  •  0
  • Haizat Haneefa  · 技术社区  · 9 年前

    我做了一个A帧360度视图。我从一个名为 seat_perlis.json . 现在我想在场景中制作一系列面板。下面是我的jQuery代码。

    <script>
    AFRAME.registerComponent ('jsonreader',
    {
    
    init: function()
    {
    var sceneEl = document.querySelector('a-scene');
    var el = document.createElement('a-text');
    
    
    $.getJSON( "seat_perlis.json", function( response ) {
    
         console.log(response);
    
      //push data to items
    
        $.each( response.data, function( key, val ) {
          $('#jadah').append(`\
            <a-entity layout="type: box; margin: 1.6" scale="0.8 0.8" position="0 -1 -4">\
           <a-text value='${val.name} \n ${val.code_name} \n ${val.candidate_seats[0].candidate.name}' color="red" id="entity1-1"></a-text>\
    
            </a-entity>\
            `);
    
       });//end each
    
     }); //end getJSON
    
    }
    });
    

    下面是JSON文件的示例

    {
    "data": [
      {
            "id": 1,
            "state_id": 1,
            "name": "Titi Tinggi",
            "code_name": "N1",
            "type": "DUN",
            "hot_seat": true,
            "election_year": 2013,
            "registered_voters": 9159,
            "spoilt": 0,
            "turnout": 7332,
            "turnout_percent": 80,
            "majority": 1486,
            "majority_percent": 20,
            "race_malay": 6734,
            "race_chinese": 2128,
            "race_india": 258,
            "race_bumi": null,
            "race_nonbumi": null,
            "race_others": 54,
            "state": {
                "id": 1,
                "name": "Perlis",
                "code": "PER"
            },
            "candidate_seats": [
                {
                    "id": 1,
                    "candidate_id": 2538,
                    "seat_id": 1,
                    "party_id": 1,
                    "verified_vote": 3925,
                    "unverified_vote": null,
                    "status": "WON",
                    "candidate": {
                        "id": 2538,
                        "name": "Khaw Hock Kong",
                        "image": null
                    }
                },
                {
                    "id": 2,
                    "candidate_id": 2540,
                    "seat_id": 1,
                    "party_id": 100,
                    "verified_vote": 968,
                    "unverified_vote": null,
                    "status": null,
                    "candidate": {
                        "id": 2540,
                        "name": "Yaacob Bin Man",
                        "image": null
                    }
                },
                {
                    "id": 3,
                    "candidate_id": 2539,
                    "seat_id": 1,
                    "party_id": 300,
                    "verified_vote": 2439,
                    "unverified_vote": null,
                    "status": "",
                    "candidate": {
                        "id": 2539,
                        "name": "Teh Seng Chuan",
                        "image": null
    

    链接是预期结果-> [图片][1]

    现在JSON数据已经存在,但它直接上升了。我不知道怎么打破它。

    1 回复  |  直到 9 年前
        1
  •  0
  •   oha    9 年前

    您可以为json数据中的每个条目创建一个新的布局实体,即:

    <a-entity layout="type: box; margin: 1.6" scale="0.8 0.8" position="0 -1 -4">
       <a-text value="... entry 1 ..."></a-text>
    </a-entity>
    <a-entity layout="type: box; margin: 1.6" scale="0.8 0.8" position="0 -1 -4">
       <a-text value="... entry 2 ..."></a-text>
    </a-entity>
    <a-entity layout="type: box; margin: 1.6" scale="0.8 0.8" position="0 -1 -4">
       <a-text value="... entry 3 ..."></a-text>
    </a-entity>
    

    您希望(如果我理解正确的话)像这样的内容,而不总是生成新的布局实体:

    <a-entity layout="type: box; margin: 1.6" scale="0.8 0.8" position="0 -1 -4">
       <a-text value="... entry 1 ..."></a-text>
       <a-text value="... entry 2 ..."></a-text>
       <a-text value="... entry 3 ..."></a-text>
    </a-entity>
    

    所以你应该把所有的 a-text 相同的节点 a-entity ,而不是 #jadah .

    另一方面,你给出了所有 a-文本 元素具有相同的ID!