循环遍历数据并从csv文件加载数据

Loop through data and load data from csv file

本文关键字:数据 文件 加载 csv 遍历 循环      更新时间:2023-09-26

我很难遍历这两组数据。有了1组数据,它就可以工作了,但现在我想添加更多的3或4组数据。此外,我想从本地csv文件加载数据,我将上传和更新数据,我将保存文件并覆盖它。http://jsfiddle.net/kmc3ohab/7/

var data = {
    "box1": {
        "bar1": "80%",
        "bar2": "60%",
        "bar3": "40%",
        "bar4": "50%",
        "total": "60%",
    },
     "box2": {
        "bar1": "80%",
        "bar2": "60%",
        "bar3": "40%",
        "bar4": "50%",
        "total": "80%",
    },
};
$(document).ready(function() {
    $(".score-text").html(data.box1.total);
    $(".data").each(function( index, value ) {
        width = eval('data.box1.bar' + (index+1));
        value.innerText = width;
        value.style.width = width;
    });
});
    
body {
	background: #efefef;
	width: 100%;
	margin: 0px;
	text-align: center;
}
h2 {
	font-family: 'Noto Sans', serif;
	color: #b71f38;
	font-weight: 300;
	margin: 0px;
}
h3 {
	font-family: 'Noto Sans', serif;
	color: #444444;
	font-weight: 200;
	margin: 0px;
}
#colLeft {
	width: 50%;
	float: left;
}
#colRight {
	width: 50%;
	float: right;
}
#row {
	background: #e2e2e2;
	width: auto;
	height: 230px;
	margin: 15px;
	border-radius: 5px;
}
#insideColLeft {
	width: 30%;
	float: left;
}
#insideColRight {
	width: 69%;
	float: right;
	padding-top: 8px;
	padding-right: 5px;
}
.circle {
	margin-left: auto;
	margin-right: auto;
	border-radius: 50%;
	width: 150px;
	position: relative;
	background: #b71f38;
}
.circle:before {
	content: "";
	display: block;
	padding-top: 100%;
}
.circle-inner {
	position: absolute;
	top: 0;
	left: 0;
	bottom: 0;
	right: 0;
	text-align: center;
}
.score-text {
	margin: auto;
	position: absolute;
	top: 0;
	left: 0;
	bottom: 0;
	right: 0;
	height: 1em;
	line-height: 1em;
	font-size: 30px;
	font-family: 'Fjalla One', sans-serif;
	color: #ffffff;
}
.date {
	font-family: 'Fjalla One', sans-serif;
	text-align: center;
	color: #333333;
}
ul.graph {
	margin: 0;
	padding: 0;
	list-style-type: none;
}
	
ul.graph li {
	margin: 10px;
	height: 25px;
	background: #ccc;
	color: #fff;
}
	
ul.graph li.data {
	background: #f4ebb8;
}
	
<div id="row">
      <h2>Title</h2>
      <h3>Subtitle</h3>
      <div id="insideColLeft">
        <div class="circle">
          <div class="circle-inner">
            <div class="score-text">
            </div>
          </div>
        </div>
        
      </div>
      <div id="insideColRight">
        <ul class="graph">
          <li class="data">bar 1</li>
          <li class="data">bar 2</li>
          <li class="data">bar 3</li>
          <li class="data">bar 4</li>
        </ul>
      </div>
    </div>
<div id="row">
      <h2>Title</h2>
      <h3>Subtitle</h3>
      <div id="insideColLeft">
        <div class="circle">
          <div class="circle-inner">
            <div class="score-text">
            </div>
          </div>
        </div>
        
      </div>
      <div id="insideColRight">
        <ul class="graph">
          <li class="data">bar 1</li>
          <li class="data">bar 2</li>
          <li class="data">bar 3</li>
          <li class="data">bar 4</li>
        </ul>
      </div>
    </div>

您必须注意document-ready-function中的box-index。在你的代码中有一个固定的"box1",但是。data的索引将上升到7。因此,将索引除以4,然后可以计算框索引。您还必须循环遍历score-text元素。

$(document).ready(function() {
   $(".score-text").each(function( index, value ) { 
       value.innerText = data['box'+(index+1)].total;
   });
   $(".data").each(function( index, value ) {  
       var boxIndex = Math.floor(index/4);
       width = data['box'+(boxIndex+1)]['bar'+(index+1-boxIndex*4)];
       value.innerText = width;
       value.style.width = width;
   });
});

我也更新了你的例子。http://jsfiddle.net/kmc3ohab/11/