javascript - 使用 Mustache 将 JSON 文件集成到 html 中

标签 javascript html json mustache

我认为我相当接近,我已经看过一些将外部 JSON 文件集成到带有 Mustache 的 html 中的示例,但目前它不起作用。 JSON 文件充满了琐碎的问题,这是我尝试链接的示例问题:

{
  "jsonquestions": [
    {
      "id":1,
      "q_category_id":0,
      "q_text":"Which of the following is regar-ded as the real founder of portugese power in India",
      "q_options_1":"Pedro Cabral",
      "q_options_2":"Almeida",
      "q_options_3":"Vasco da Gama",
      "q_options_4":"Alfonso de Albuquerque",
      "q_correct_option":4,
      "q_difficulty_level":2,
      "q_date_added":"2013-06-10T04:00:00.000Z"
    }
  ]
}

该文件名为 questions.json,我正在尝试将其链接到此 html 表:

<!DOCTYPE html>

<html class="no-js" lang="">
<head>
    <link rel="stylesheet" href="index.css">
    <meta charset="utf-8">
    <meta content="ie=edge" http-equiv="x-ua-compatible">

    <title>Quiz</title>
    <meta content="" name="description">
    <meta content="width=device-width, initial-scale=1" name="viewport">
</head>

<body>

    <hr>

    <div id="control">
    <h1>Questions</h1>Data generated from <a href=
    "https://market.mashape.com/pareshchouhan/trivia">
    https://market.mashape.com/pareshchouhan/trivia</a>
    <br><br>

        Search questions:
        <input type="text" id="searchText">
        <button id="search">Search Questions</button>
        <select id="sortquestions">
            <option id="idascend">Ascending ID</option>
            <option id="iddescend">Descending ID</option>
            <option id="alphabetascend">Ascending Alphabetical</option>
            <option id="alphabetdescend">Descending Alphabetical</option>
            <option id="diffascend">Ascending Difficulty</option>
            <option id="diffdescend">Descending Difficulty</option>
        </select>
        <button id ="sort">Sort</button>
    <br><br>

        Filter by difficulty:
        <input type="radio" name="difficulty" id="1" value="1" checked> 1
        <input type="radio" name="difficulty" id="2" value="2"> 2
        <button id="difficultybutton">Filter</button>
        <button id="random">Randomise</button>
        <button id="quiz">Begin Quiz!</button>
    <br><br>
    </div>  

    <table id="anchor"></ul>

    <script id="questions-template" type="text/template">  
    {{#jsonquestions}}
    <table id="questions">
        <thead>
            <tr>
                <th>ID</th>

                <th>Question</th>

                <th>Answer 1</th>

                <th>Answer 2</th>

                <th>Answer 3</th>

                <th>Answer 4</th>

                <th>Correct Answer</th>

                <th>Difficulty</th>
            </tr>
            <tr>
                <td>{{id}}</td>

                <td>{{q_text}}</td>

                <td>{{q_options_1}}</td>

                <td>{{q_options_2}}</td>

                <td>{{q_options_3}}</td>

                <td>{{q_options_4}}</td>

                <td>{{q_correct_option}}</td>

                <td>{{q_difficulty_level}}</td>
            </tr>
        </thead>

        <tbody>
        </tbody>
    </table>
    {{/jsonquestions}}
    </script>

    <script> 
    $(function() {
        $.getJSON('js/questions.json', function(data) {
            var template = $('#jsonquestions-template').html();
            var info = Mustache.to_html(template, data);
            $('#anchor').html(info);
        });
    });
    </script>

    </br></br>
    <div id="answers" style="display:none";>
        <input type="radio" name="answer" value="1" id="answer1" checked><label id="answerlabel1"></label></br>
        <input type="radio" name="answer" value="2" id="answer2"><label id="answerlabel2"></label></br>
        <input type="radio" name="answer" value="3" id="answer3"><label id="answerlabel3"></label></br>
        <input type="radio" name="answer" value="4" id="answer4"><label id="answerlabel4"></label></br>
        </br>
        <button id="giveanswer">Answer</button>
        </br></br>
        <label id="answerresult"></label>
        </br></br>
        <label id="score"></label>
    </div>

    <script src="js/jquery.min.js"></script> 
    <script src="js/mustache.min.js"></script>
    <script src="js/trivia.js"></script>
    <script src="js/search.js"></script>
    <script src="js/difficulty.js"></script>
    <script src="js/random.js"></script>
    <script src="js/quiz.js"></script>
    <script src="js/sort.js"></script>
</body>
</html>

我有什么遗漏的吗?谢谢。

最佳答案

看起来您缺少“渲染”函数调用 Mustache.render(template, data); (to_html 基本上已被弃用)我构建了一个部分 Plunker展示它的工作原理,它嵌入在下面。我将 json 托管在 https://api.myjson.com/bins/2oxsm

// Code goes here

	$(function(){
        var data;
        $.getJSON("https://api.myjson.com/bins/2oxsm")
            .done(function(dat) {
                data = dat;
                console.log("success",dat)
                render();
            })
            .fail(function() {
                console.log( "error",arguments);
            })
            .always(function() {
                console.log( "complete" );
            });
            function render(){
                // mustache
                var tmpl = $('#questions-template').html();
                var html = Mustache.render(tmpl, data);
                $('#questions').find('tbody').html(html);
            }  
	});
    // lets make them clickable
    $(function(){
    
        var choices = [];
        $(document).on("click","td.choice",updateAnswers)
        
        function updateAnswers(e){
            
            var $t = $(this),
                $p = $t.parents('tr'),
                $answer = $p.find('.answer'),
                correct = $answer.data('value'),
                good = $t.data('value') === correct;

            $p.find('td')
                .toggleClass("checked",false)
                .toggleClass("correct",false);
            $t.addClass("checked");
            $answer.toggleClass("correct",good);
            $t.toggleClass("correct",good);
            $("#choices").html('');
            $('td.checked').each(show);
        }
        
        function show(i,v){
            // send this data to the backend?
            var $v = $(v),
                $p = $v.parents('tr'),
                div = $("<div/>").html($p.index()+1 + ":" + $v.html());
            $("#choices").append(div);
            div.toggleClass('correct',$v.is(".correct"))
        }
        
    })
    	
/* Styles go here */
* {
  font: 0.95em arial; }

#choices {
  color: white;
  background-color: #F00; }
  #choices div {
    padding: 1em; }
    #choices div.correct {
      background-color: green; }

table#questions {
  border-collapse: collapse; }
  table#questions thead {
    background: #000;
    color: #FFF;
    font-weight: bold; }
    table#questions thead th {
      white-space: nowrap;
      padding: 1em; }
  table#questions td {
    border: 1px solid black;
    padding: 0.5em;
    border-spacing: 1px; }
    table#questions td.choice {
      background-color: #666;
      color: white;
      cursor: pointer; }
      table#questions td.choice.checked {
        background-color: red;
        color: white; }
      table#questions td.choice.correct {
        background-color: green; }
    table#questions td.answer {
      background-color: pink; }
      table#questions td.answer.correct {
        background-color: lime; }
<script src="//cdnjs.cloudflare.com/ajax/libs/mustache.js/0.7.2/mustache.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

        <table id="questions">
        <thead>
            <tr>
                <th>ID</th>
                <th>Question</th>
                <th>Answer 1</th>
                <th>Answer 2</th>
                <th>Answer 3</th>
                <th>Answer 4</th>
                <th>Correct</th>
                <th>Difficulty</th>
            </tr>
            <tbody></tbody>
        </table>
        <div id="choices">
            
        </div>
        <script id="questions-template" type="text/template">  
            {{#jsonquestions}}
            <tr>
                <td>{{id}}</td>
                <td>{{q_text}}</td>
                <td class="choice" data-value="1">{{q_options_1}}</td>
                <td class="choice" data-value="2">{{q_options_2}}</td>
                <td class="choice" data-value="3">{{q_options_3}}</td>
                <td class="choice" data-value="4">{{q_options_4}}</td>
                <td class="answer" data-value="{{q_correct_option}}"></td>
                <td>{{q_difficulty_level}}</td>
            </tr>
            {{/jsonquestions}}
        </script>

(您真的想为每个问题重复表格标题吗?)

$(function(){
    var data;
    $.getJSON("./data.json")
        .done(function(dat) {
            data = dat;
            render();
        })
        .fail(function() {
            console.error( "error",arguments);
        })
        .always(function() {
            console.info( "complete" );
        });
        function render(){
            // mustache
            var tmpl = $('#template').html();
            var html = Mustache.render(tmpl, data);
            $('#target').html(html);
        }  
});

关于javascript - 使用 Mustache 将 JSON 文件集成到 html 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33026095/

相关文章:

javascript - 如何选择仅具有特定属性的元素?

javascript - Fancybox 没有导航按钮,但有画廊作品

javascript - 将 KineticJS 与 box2dweb 一起使用

php - 如何从 PHP 访问我的 json 编码数据到 mysql 查询中

json - MongoDB 更改 DBrefs

ios - JSON 图片解析 IOS Swift 3.0

javascript - 为什么 Safari 允许基于滚动的视频按预期工作,而其他浏览器则不允许?

javascript - jquery div 隐藏和显示、z-index 问题和鼠标悬停/鼠标悬停问题

javascript - 在 android chrome 浏览器上运行 ffmpeg.wasm 时出现错误 "WebAssembly.Memory(): could not allocate memory"

jquery - 更新网页后显示弹出屏幕