• Home
  • About
    • Jiwon Jeong photo

      Jiwon Jeong

      끊임없이 배우며 성장하는 엔지니어

    • Learn More
    • Email
    • Github
  • Posts
    • All Posts
    • All Tags
    • All Categories
  • Projects

Tab UI

05 May 2021

Reading time ~2 minutes

Tab UI 실습 - 동적으로 값 받아오기 (중요!!)

[
    {
        "name" : "crong",
        "favorites" : ["golf", "facebook"]
    },
    {
        "name" : "jk",
        "favorites" : ["soccer", "apple"]
    },
    {
        "name" : "honux",
        "favorites" : ["game", "oragne"]
    },
    {
        "name" : "pobi",
        "favorites" : ["book", "youtube"]
    }
]
<html>
<header>
    <link rel="stylesheet" href="tabui.css">
    <style>
    h2 {
    text-align: center
}
h2,
h4 {
    margin: 0px;
}
.tab {
    width: 600px;
    margin: 0px auto;
}
.tabmenu {
    background-color: bisque;
}
.tabmenu>div {
    display: inline-block;
    width: 140px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    cursor: pointer;
}
.content {
    padding: 5%;
    background-color: antiquewhite;
}
</style>
</header>

<body>
    <h2> TAB UI TEST</h2>

    <div class="tab">
        <div class="tabmenu">
            <div>crong</div>
            <div>jk</div>
            <div>pobi</div>
            <div>honux</div>
        </div>
        <section class="content">
            <h4>hello jk</h4>
            <p>golf, facebook</p>
        </section>
    </div>
    <script>
        function makeTemplate(data, clickedName) {
            var html = document.getElementById("tabcontent").innerHTML;
            // console.log(html, data);
            var resultHTML = "";
            for (var i = 0; i < data.length; i++) {
                if (data[i].name === clickedName) {
                    resultHTML = html.replace("{name}", data[i].name)
                                    .replace("{favorites}", data[i].favorites.join(" "));
                    break;
                }
            }
            document.querySelector(".content").innerHTML = resultHTML;
        }
        function sendAjax(url, clickedName) {
            var oReq = new XMLHttpRequest(); 
            oReq.addEventListener("load", function () {
                // console.log(oReq.responseText)
                var data = JSON.parse(oReq.responseText);
                makeTemplate(data, clickedName);
            });
            oReq.open("GET", url);
            oReq.send();
        }
        var tabmenu = document.querySelector(".tabmenu");
        tabmenu.addEventListener("click", function (evt) {
            // console.log(evt.target);
            sendAjax("./tebui.json", evt.target.innerText);
        });
    </script>

    <script id="tabcontent" type="my-template">
            <h4>hello {name}</h4>
            <p>{favorites}</p>
       </script>
</body>

</html>

위 코드 단계별로 쪼개보기

  • 1단계
    • 하나의 tab 클릭했을 때 이벤트 발생처리
    • html 템플리팅 구성
        <script>
        var tabmenu = document.querySelector(".tabmenu");
        tabmenu.addEventListener("click", function (evt) {
            // console.log(evt.target);
            sendAjax("./tabui.json", evt.target.innerText);
        });
        </script>

        <script id="tabcontent" type="my-template">
            <h4>hello {name}</h4>
            <p>{favorites}</p>
       </script>
  • 2단계
    • Ajax 처리
        <script>
        function sendAjax(url, clickedName) {
            var oReq = new XMLHttpRequest(); 
            oReq.addEventListener("load", function () {
                // console.log(oReq.responseText)
                var data = JSON.parse(oReq.responseText);
                makeTemplate(data, clickedName);
            });
            oReq.open("GET", url);
            oReq.send();
        }
       </script>
  • 3단계
    • HTML Templating 요소 집어넣기
        <script>
        function makeTemplate(data, clickedName) {
            var html = document.getElementById("tabcontent").innerHTML;
            // console.log(html, data);
            var resultHTML = "";
            for (var i = 0; i < data.length; i++) {
                if (data[i].name === clickedName) {
                    resultHTML = html.replace("{name}", data[i].name)
                                    .replace("{favorites}", data[i].favorites.join(" "));
                    break;
                }
            }
            document.querySelector(".content").innerHTML = resultHTML;
        }
       </script>


WebProgramming Share Tweet +1