简体   繁体   English

从API获取点击数据

[英]Getting data on click from api

I get some data out of an API. 我从API获得一些数据。 I make two calls to the API and save this output in a variable. 我对API进行了两次调用,并将此输出保存在变量中。 Now I have two links - One for Data1 and one for Data2 . 现在,我有两个链接-一个用于Data1 ,一个用于Data2 If I click on Data1 , the first call should be loaded. 如果我单击Data1 ,则应该加载第一个调用。 Click on Data2 shows the second call should take place. 单击Data2表示应该进行第二次调用。 When someone visits the site, Data1 should be the default displayed dataset. 当有人访问该站点时, Data1应该是默认显示的数据集。

I have a nearly working solution: 我有一个几乎可行的解决方案:

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
        <script>
            $(function(){
                $("#data1").click(function(e){
                    e.preventDefault();
                    var datasets = document.getElementById("datasets");
                    var html = '';
                    var i = 0;
                    var results = <?php echo json_encode($resultsD1) ?>;
                    for (const result of results) {
                        html += '<article class="dataset"><span class="title">' + result["title"] + '</span><div class="data-meta">';
                        html += '<span class="release">' + result["release"] + '</span></div></article>';
                        i++;
                    }
                    products.innerHTML = html;
                });
                $("#data2").click(function(e){
                    e.preventDefault();
                    var datasets = document.getElementById("datasets");
                    var html = '';
                    var i = 0;
                    var results = <?php echo json_encode($resultsD2) ?>;
                    for (const result of results) {
                        html += '<article class="dataset"><span class="title">' + result["title"] + '</span><div class="data-meta">';
                        html += '<span class="release">' + result["release"] + '</span></div></article>';
                        i++;
                    }
                    products.innerHTML = html;
                });
            });
        </script>

Now if I click on Data1 the correct data is loaded. 现在,如果我单击Data1将加载正确的数据。 The same when I click on Data2 . 单击Data2时相同。 But how can I make Data1 as default when the site is loaded? 但是,如何在加载网站时将Data1为默认值? Also my code seems so bad constructed. 而且我的代码似乎构造得很糟糕。 I repeat the whole for loop two times... 我重复两次整个for循环...

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>
        $(function(){
    function setData(dataset_type){
        var html = '';
                    var i = 0;
        var products = document.getElementById("datasets");
                    var results = <?php echo json_encode($resultsD1) ?>;
        if(dataset_type == 2){
            results = <?php echo json_encode($resultsD2) ?>;
        }
                    for (const result of results) {
                        html += '<article class="dataset"><span class="title">' + result["title"] + '</span><div class="data-meta">';
                        html += '<span class="release">' + result["release"] + '</span></div></article>';
                        i++;
                    }
                    products.innerHTML = html;
    }

    // Default Call onload for dataset 1
    setData(1);

            $("#data1").click(function(e){
                e.preventDefault();
                setData(1);
            });
            $("#data2").click(function(e){
                e.preventDefault();
                setData(2);
            });     
        });
    </script>

You need to add the logic to load the data in function, and call the function on button click as well as on page load. 您需要添加逻辑以将数据加载到函数中,并在按钮单击以及页面加载时调用该函数。 As per your code, as soon as button is clicked the new data is replacing your old data, do you want to do that or you want to keep old data as it is and append new data? 根据您的代码,一旦单击按钮,新数据将替换旧数据,您是否要这样做还是要保留旧数据并追加新数据? I have added comments in the script as answered for above question 我在脚本中添加了评论,以回答上述问题

 <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> <script> function loadData(){ var datasets = document.getElementById("datasets"); var html = ''; var i = 0; var results = <?php echo json_encode($resultsD1) ?>; for (const result of results) { html += '<article class="dataset"><span class="title">' + result["title"] + '</span><div class="data-meta">'; html += '<span class="release">' + result["release"] + '</span></div></article>'; i++; } //products.append(html); // if you want to append data uncomment this and comment below products.innerHTML = html; // if you want to replace data comment this and uncomment above } $(function(){ // function call on page load loadData(); $("#data1").click(function(e){ e.preventDefault(); loadData(); }); $("#data2").click(function(e){ e.preventDefault(); var datasets = document.getElementById("datasets"); var html = ''; var i = 0; var results = <?php echo json_encode($resultsD2) ?>; for (const result of results) { html += '<article class="dataset"><span class="title">' + result["title"] + '</span><div class="data-meta">'; html += '<span class="release">' + result["release"] + '</span></div></article>'; i++; } products.innerHTML = html; }); }); </script> 

You can create a function 您可以创建一个函数

var datasets =document.getElementById("datasets");// make it global
$( window ).load(function() {

       loadData(whichButtonCliked,datasets )
});

loadData(whichButtonCliked,datasets )
{
 e.preventDefault();

                    var html = '';
                    var i = 0;
                    var results = <?php echo json_encode($resultsD2) ?>;
                    for (const result of results) {
                        html += '<article class="dataset"><span class="title">' + result["title"] + '</span><div class="data-meta">';
                        html += '<span class="release">' + result["release"] + '</span></div></article>';
                        i++;
                    }
                    products.innerHTML = html;
}

and call this function on click now you have to make condition which button is clicked 并在单击时调用此函数,现在必须确定单击哪个按钮的条件

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

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