简体   繁体   English

使用jquery根据本地存储数据动态创建HTML分区

[英]dynamically create HTML divisions based on local storage data using jquery

I've the following JSON in my local storage. 我在本地存储中有以下JSON。

userData:[{
           "name":"John",
           "dob":"2011-02-01",
           "gender":"male",
           "marital_status":"married",
           "email":"john@gmail.com"
          },
          {
           "name":"Paul",
           "dob":"2011-08-05",
           "gender":"male",
           "marital_status":"unmarried",
           "email":"paul@gmail.com"
          },
          {
           "name":"Mary",
           "dob":"2011-12-11",
           "gender":"female",
           "marital_status":"married",
           "email":"mary@gmail.com"
          }]

In the above JSON I've 3 users namely:John,Paul and Mary within an array 'userData' in my local storage. 在上面的JSON中,我有3个用户:John,Paul和Mary,位于本地存储中的“ userData”数组中。 The number of users can be >3 or <3, How can I create HTML divs(which has user icon with a delete button) based on number of users present at any given point of time. 用户数量可以是> 3或<3,如何基于在任何给定时间点出现的用户数量来创建HTML div(带有带有删除按钮的用户图标)。 I'm also performing delete user operation and hence it has to be dynamic in nature. 我也在执行删除用户操作,因此它本质上必须是动态的。 I'm trying to achieve this with jquery, but found nothing useful. 我正在尝试使用jquery来实现这一点,但是发现没有什么用。 I have so far tried to count the array elements and based on that I'm showing or hiding divs which are already coded in my HTML(for 4 users scenario) 到目前为止,我一直在尝试计算数组元素,并以此为基础显示或隐藏已经在我的HTML中编码的div(适用于4个用户的场景)

if(counter==4)
  {
    $("#avatar1").show();
    $("#avatar2").show();
    $("#avatar3").show();
    $("#avatar4").show();
}
  else if(counter==3)
  {
    $("#avatar1").show();
    $("#avatar2").show();
    $("#avatar3").show();
    $("#avatar4").hide();
  }
  else if(counter==2)
  {
    $("#avatar1").show();
    $("#avatar2").show();
    $("#avatar3").hide();
    $("#avatar4").hide();
  }
  else if(counter==1)
  {
    $("#avatar1").show();
    $("#avatar2").hide();
    $("#avatar3").hide();
    $("#avatar4").hide();
  }
  else{
  $("#container").html("<p>No Users found</p>");
  }

where, counter=number of elements in the array (userData in this case). 其中,counter =数组中元素的数量(在这种情况下为userData)。 Is there any way to do it dynamically or Am I totally wrong making no sense? 有什么办法可以动态地做到这一点,还是我完全没有道理? I don't think it is impossible, but I'm just trying to complete the task searching for solutions. 我认为这是不可能的,但是我只是在尝试完成寻找解决方案的任务。

Well you could try this: 好吧,你可以试试这个:

function ShowAvatars(number) {
    var counter = 1;
    if (number === 0) {
        $("#container").html("<p>No Users found</p>");
    } else {
        for (var i = 1; i <= 4; i++) {
            if (counter <= number) {
                $("#avatar" + i).show();
            } else {
                $("#avatar" + i).hide();
            }
            counter++;
        }
    }
}

If you want to dynamically show the data load the json and loop through the element and append to the html document. 如果要动态显示数据,请加载json并遍历该元素,然后附加到html文档。 An example is in the link the link below. 下面的链接是一个示例。

https://jsfiddle.net/princedc/qwb2ubqx/1/ https://jsfiddle.net/princedc/qwb2ubqx/1/

Assume you have div tag with id "userNames" as below 假设您具有ID为“ userNames”的div标签,如下所示

<div id="userNames">
</div>

you can load the json and loop through it to create dynamic page element and append to the above div. 您可以加载json并循环遍历以创建动态页面元素,然后附加到上述div中。 This will cater to any number of dynamic data in the json. 这将满足json中任意数量的动态数据。

var jsonData = { userData:[{
           "name":"John",
           "dob":"2011-02-01",
           "gender":"male",
           "marital_status":"married",
           "email":"john@gmail.com"
          },
          {
           "name":"Paul",
           "dob":"2011-08-05",
           "gender":"male",
           "marital_status":"unmarried",
           "email":"paul@gmail.com"
          },
          {
           "name":"Mary",
           "dob":"2011-12-11",
           "gender":"female",
           "marital_status":"married",
           "email":"mary@gmail.com"
          }]
          };

(function (){ 
    for(var i=0; i < jsonData.userData.length;i++){
       $('#userNames').append('<div>'+ jsonData.userData[i].name + '</div>');
  }
})();

Here's a solution that does everything that you want. 这是一个解决方案,可满足您的所有需求。

Creates a user avatar with their name next to it and a button. 创建一个用户头像,旁边带有其名称和一个按钮。

data.forEach((user) => {
        var row = document.createElement('div');
        row.classList.add('row');

        var outerpanel = document.createElement('div');
        outerpanel.classList.add('panel');
        outerpanel.classList.add('panel-default');
        outerpanel.style.marginTop = '5%';

        var panelbody = document.createElement('div');
        panelbody.classList.add('panel-body');

        var innerpanel = document.createElement('div');
        innerpanel.classList.add('row');
        innerpanel.innerHTML = `
            <img src="https://api.adorable.io/avatars/285/abott@adorable.png" height="50px" width="100px"/>
            <div style="vertical-align:middle; display:inline-block; padding-left: 10px; padding-right: 20px; padding-top: 10px; font-weight: bold;">
              ${user.name}
            </div>
            <div class="btn btn-xs btn-danger" style="vertical-align:middle; color:#FFF; font-weight:bold; line-height:25px;"> X </div>
        `;

        panelbody.append(innerpanel);
        outerpanel.append(panelbody);
        row.append(outerpanel);

        document.getElementById('user-container').append(outerpanel);
      });

http://plnkr.co/edit/P0asYI7fSHqDjeq4ymK9?p=preview http://plnkr.co/edit/P0asYI7fSHqDjeq4ymK9?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM