[英]Load Random Data into HTML from JSON File?
When I design interfaces, I can't be bothered to create believable dummy text. 在设计界面时,不必费心创建可信的伪文本。 At the same time, however, I don't want to just copy and paste the same exact data over and over, because then the interface doesn't look terribly realistic.
但是,与此同时,我不想只是一遍又一遍地复制和粘贴相同的数据,因为那样的话,界面看起来就不太现实了。
I have generated a Master JSON Schema that contains the most common types of data I use. 我已经生成了一个主JSON架构 ,其中包含我使用的最常见的数据类型。 I'd like to be able to do something like this when I'm writing HTML:
在编写HTML时,我希望能够执行以下操作:
<ul>
<li>{first_name}</li>
<li>{first_name}</li>
...
<li>{first_name}</li>
</ul>
OR 要么
<ul>
<li data="{first_name}"></li>
<li data="{first_name}"></li>
...
<li data="{first_name}"></li>
</ul>
...whereby every instance of {first_name}
is replaced with a random first name from my JSON file. ...由此
{first_name}
每个实例都被替换为我的JSON文件中的随机名字。 Likewise for any other variable I have in there ( last_name
, email
, address
, country
, sentence
, etc... ) 同样,对于我在那里的任何其他变量(
last_name
, email
, address
, country
, sentence
等)
Is there a way to do this without PHP in something like jQuery? 有没有一种方法可以在没有 jQuery之类的PHP的情况下做到这一点? I imagine it'd have to be something like:
我想那一定是这样的:
foreach(var) {
return randomData(var);
}
Ideally I'd have a very simple, generalized function that would comb through the UI looking for any and all tags and replace each one with a random piece of data from the master schema JSON file. 理想情况下,我将拥有一个非常简单的通用函数,该函数会在UI中进行梳理以查找所有标签,并用主模式JSON文件中的随机数据替换每个标签。
between http://chancejs.com/ and http://handlebarsjs.com/ you can generate lots of repeatable, seeded random data structures... 在http://chancejs.com/和http://handlebarsjs.com/之间,您可以生成许多可重复的种子随机数据结构...
// get references to DOM elements var tplElm = document.getElementById('template'); var tgtElm = document.getElementById('target'); // compile handlebars template var template = Handlebars.compile(tplElm.innerText); // initialise chance with seed // change the seed to change the output data var chance = new Chance(1234567890); // create array of random data var people = []; for(var i=0;i<10;i++){ people.push({ first_name: chance.name() }); } // apply data to template, and insert into page tgtElm.innerHTML = template({ people: people });
<!-- load dependencies --> <script src="https://cdnjs.cloudflare.com/ajax/libs/chance/0.5.6/chance.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.3/handlebars.min.js"></script> <!-- define template --> <script id="template" type="text/template"> <ul> {{#people}} <li>{{first_name}}</li> {{/people}} </ul> </script> <!-- placeholder for output --> <div id="target"></div>
Something like this will give you what you want: 这样的事情会给你你想要的东西:
var json = [{ "first_name": "bob"}, {"first_name": "sam"}, {"first_name": "bill"}];
var randomnum = Math.floor((Math.random() * 3));
console.log(json[randomnum].first_name);
You can access this data using AJAX and then get elements using Math.random
. 您可以使用AJAX访问此数据,然后使用
Math.random
获取元素。
Then, with the help of jQuery you can dynamically generate li
items. 然后,借助jQuery,您可以动态生成
li
项目。
Let's suppose you have a div element like <div id="anyId"></div>
where you will put your ul
and li
s. 假设您有一个
<div id="anyId"></div>
这样的div元素,您将在其中放置ul
和li
。
function getElementsFromMasterSchema(count, callback) {
var items = [];
var usedIds = {};
$("h3").text("Request sent...Loading...");
timer = setInterval(function() {
$("h3").text("Loading..." + (time++) + " seconds passed.");
}, 1000);
$.ajax({
url: "https://www.mockaroo.com/37dcc3b0/download?count=100&key=37b5a7c0",
method: "GET"
}).done(function(dt) {
var length = dt.length;
while (items.length < count) {
var randomItem = dt[Math.round(Math.random() * length)];
if (usedIds[randomItem.id] !== true) {
usedIds[randomItem.id] = true;
items.push(randomItem);
}
}
callback(items);
});
}
getElementsFromMasterSchema(10, function(result) {
var ul = $("<ul/>");
for (var i = 0; i < result.length; i++) {
$("<li/>").text(result.first_name).appendTo(ul);
}
$("#anyId").append(ul);
});
Note that you need to make requests from the same domain or set Access-Control-Allow-Origin
header in order to make cross-domain requests. 请注意,您需要从同一域发出请求或设置
Access-Control-Allow-Origin
标头才能发出跨域请求。
However , this method is working slowly. 但是 ,此方法工作缓慢。 It takes from 5 to 20 seconds to load this file.
加载该文件需要5到20秒。 Loading a large file from a server and using only some of data is a bad approach.
从服务器加载大文件并仅使用部分数据是一种不好的方法。
You definitely need to implement this algorithm on a server side. 您绝对需要在服务器端实现此算法。 Something like SQL
SELECT TOP n FROM x ORDER BY newid()
( SELECT * FROM x ORDER BY RAND() LIMIT n
). 类似于SQL
SELECT TOP n FROM x ORDER BY newid()
( SELECT * FROM x ORDER BY RAND() LIMIT n
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.