Google 表转 Html Json 数据 - 单击显示新框显示数据

我正在尝试通过 JSON 格式从 google 工作表获取数据并以 HTML 形式显示。这很好。

我想要什么,单击项目或箭头

https://img1.mukewang.com/64cdb1bc000178e705790540.jpg

打开侧边栏并使用列表获取项目的相关数据,例如

  1. 单号:1207

  2. 客户名称: 客户

  3. 电子邮件:exmple2@gmail.com

  4. 交货:下周日

像Whatsapp一样查看人们的详细信息onclick

https://img1.mukewang.com/64cdb1c900013f5506530249.jpg

  $.getJSON("https://spreadsheets.google.com/feeds/list/1XaFRnQfNPRP86UPNcdgQuCCH6AeVe5FZOxBHaIPZDpM/od6/public/values?alt=json", function(data) {


            var sheetData = data.feed.entry;


            var i;

            for (i = 0; i < sheetData.length; i++) {


                var name = data.feed.entry[i]['gsx$slipno']['$t'];

                var id = data.feed.entry[i]['gsx$id']['$t'];

                var clientname = data.feed.entry[i]['gsx$clientname']['$t'];


                // var email = data.feed.entry[i]['gsx$email']['$t'];

                // var delivery = data.feed.entry[i]['gsx$delivery']['$t'];


                document.getElementById('demo').innerHTML +=

                    ('<tr class="dd d-flex justify-content-around">' +

                        '<td>' +

                        " <span id='" + 't' + id + "'>" + name + '</span>' +

                        '<span class="cn">' + clientname + '</span>' +

                        '</td>' +

                        '<td class="ml-auto gg">' +

                        '</td>' +

                        '</tr>');

            }

        });

      tbody>tr>td>span {

                    text-align: left;

                    display: block;

                }

                

                .dd {

                    border-bottom: rgb(202, 202, 202) solid 1px;

                    display: block;

                }

                

                .cn {

                    font-weight: 700;

                }

                

                .gg::before {

                    content: ">";

                }


拉风的咖菲猫
浏览 110回答 1
1回答

长风秋雁

我创建了一个名为的全局数组,myData.当调用服务时,该数组将被重置并添加记录。每条记录都是为了包含您要使用的数据而构建的。myData索引被保存到记录中,以便在选择元素时可以使用它来查找记录。我决定向onclick要用来显示详细信息的元素添加方法,即clientname箭头图标。单击某个元素会传递与记录相关联的索引,该索引用于引用中的数据myData.var myData = [];// an example function that will get the data by index so it can be used however you wantfunction showDetails(index) {&nbsp; &nbsp;var selectedData = myData[index];&nbsp; &nbsp;alert(JSON.stringify(selectedData, null, 2));}$.getJSON("https://spreadsheets.google.com/feeds/list/1XaFRnQfNPRP86UPNcdgQuCCH6AeVe5FZOxBHaIPZDpM/od6/public/values?alt=json", function(data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myData = []; // reset whenever data loads&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var sheetData = data.feed.entry;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (i = 0; i < sheetData.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var dataPoint = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: data.feed.entry[i]['gsx$slipno']['$t'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: data.feed.entry[i]['gsx$id']['$t'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clientname: data.feed.entry[i]['gsx$clientname']['$t'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delivery: data.feed.entry[i]['gsx$delivery']['$t']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myData.push(dataPoint); // add data point to array to reference later&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // var email = data.feed.entry[i]['gsx$email']['$t'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // var delivery = data.feed.entry[i]['gsx$delivery']['$t'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('demo').innerHTML +=&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ('<tr class="dd d-flex justify-content-around">' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<td>' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; " <span id='" + 't' + dataPoint.id + "'>" + dataPoint.name + '</span>' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<span class="cn" onclick="showDetails(' + i + ');">' + dataPoint.clientname + '</span>' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '</td>' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<td class="ml-auto gg" onclick="showDetails(' + i + ');">' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '</td>' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '</tr>');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });tbody>tr>td>span {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text-align: left;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; display: block;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .dd {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; border-bottom: rgb(202, 202, 202) solid 1px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; display: block;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .cn {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font-weight: 700;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .gg::before {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; content: ">";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; <title>Document</title>&nbsp; &nbsp; <!-- Font Awesome -->&nbsp; &nbsp; <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css">&nbsp; &nbsp; <!-- Google Fonts -->&nbsp; &nbsp; <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap">&nbsp; &nbsp; <!-- Bootstrap core CSS -->&nbsp; &nbsp; <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet">&nbsp; &nbsp; <!-- Material Design Bootstrap -->&nbsp; &nbsp; <link href="https://cdnjs.cloudflare.com/ajax/libs/mdbootstrap/4.19.1/css/mdb.min.css" rel="stylesheet">&nbsp; &nbsp; <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>&nbsp; &nbsp; <!-- Bootstrap tooltips -->&nbsp; &nbsp; <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.4/umd/popper.min.js"></script>&nbsp; &nbsp; <!-- Bootstrap core JavaScript -->&nbsp; &nbsp; <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js"></script>&nbsp; &nbsp; <!-- MDB core JavaScript -->&nbsp; &nbsp; <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mdbootstrap/4.19.1/js/mdb.min.js"></script></head><body>&nbsp; &nbsp; <nav class="navbar navbar-expand-lg navbar-light bg-light">&nbsp; &nbsp; &nbsp; &nbsp; <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo01" aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="navbar-toggler-icon"></span>&nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; &nbsp; <div class="collapse navbar-collapse" id="navbarTogglerDemo01">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class="navbar-brand" href="#"> brand</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ul class="navbar-nav&nbsp; mt-2 mt-lg-0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="nav-item active">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </nav>&nbsp; &nbsp; <div class="container text-center my-4">&nbsp; &nbsp; &nbsp; &nbsp; <!-- Table&nbsp; -->&nbsp; &nbsp; &nbsp; &nbsp; <table class="table " id="testTable">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- Table head -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Google Sheet Data</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- Table head -->&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- Table body -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tbody id="demo">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tbody>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- Table body -->&nbsp; &nbsp; &nbsp; &nbsp; </table>&nbsp; &nbsp; &nbsp; &nbsp; <!-- Table&nbsp; -->&nbsp; &nbsp; </div></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript