IT/development

[html] button์— type์„ ๋ช…์‹œํ•˜์ž(feat. default submit)

์•Œ ์ˆ˜ ์—†๋Š” ์‚ฌ์šฉ์ž 2023. 7. 9.

๋ชฉ์ฐจ

    ajax๋กœ ํŽ˜์ด์ง•์„ ๊ตฌํ˜„ ํ–ˆ๋Š”๋ฐ ํŽ˜์ด์ง€ ์ด๋™์ด ๋˜์—ˆ๋‹ค๊ฐ€ ๋‹ค์‹œ ๊ฐ™์€ ํŽ˜์ด์ง€๊ฐ€ ํ˜ธ์ถœ์ด ๋˜์—ˆ๋‹ค.

    ์ฝ˜์†” ๋ณด๋ฉด 2ํŽ˜์ด์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ์ •์ƒ์ ์œผ๋กœ ๊ฐ€์ ธ์™”๋‹ค๊ฐ€ ๋‹ค์‹œ 1ํŽ˜์ด์ง€๊ฐ€ ํ˜ธ์ถœ์ด ๋˜๊ณ  ์žˆ๋‹ค.

    ๊ฐ„๋‹จํ•œ ์›์ธ ์ด์—ˆ๋Š”๋ฐ ๊ฝค ๋งŽ์€ ์‹œ๊ฐ„ ์‚ฝ์งˆ ๋์— ๋‚˜์ค‘์— ๋‹ค์‹œ ๋ด์•ผ์ง€ ํ•˜๊ณ  ๋„˜์–ด ๊ฐ”์—ˆ๋‹ค๊ฐ€ ๋‹ค์‹œ ๋ด์„œ ํ•ด๊ฒฐ ํ–ˆ๋‹ค.

    before source ๐Ÿ™‚

    /**
         * ํŽ˜์ด์ง€๋„ค์ด์…˜์„ ๊ทธ๋ฆฐ๋‹ค.
         * @param paging
         */
        function drawPagination(paging) {
    
        	let pageHtml = "";
        	pageHtml += "<ul class='pagination'>";
    
        	//first
        	const first = parseInt(paging.firstPage);
        	pageHtml += "<li class='paginate_button page-item first'>";
    
        	if (paging.currentPage === paging.firstPage) {
        		pageHtml += "<button class='page-link' disabled>";
        	} else {
        		pageHtml += "<button class='page-link' style='cursor: pointer;' onclick='movePage("+ first +")'>";
        	}
        	pageHtml += "<span class='hidden'>first page</span>";
        	pageHtml += "</button>";
        	pageHtml += "</li>"
    
        	//prev
        	const prev = parseInt(paging.currentPage) -1;
        	pageHtml += "<li class='paginate_button page-item prev'>";
    
        	if (paging.firstPage === paging.currentPage) {
        		pageHtml += "<button class='page-link' disabled>";
        	} else {
        		pageHtml += "<button class='page-link' style='cursor: pointer;' onclick='movePage("+ prev +")'>";
        	}
        	pageHtml += "<span class='hidden'>prev page</span>";
        	pageHtml += "</button>";
        	pageHtml += "</li>";
    
        	//โ‘  ~ โ‘ฉ
        	for (let i = paging.firstPageNo; i <= paging.lastPageNo; i++) {
        		pageHtml += "<li class='paginate_button page-item";
        		//ํ˜„์žฌ ํŽ˜์ด์ง€๊ฐ€ ์ธ๋ฑ์Šค์™€ ๊ฐ™์œผ๋ฉด active class ์ถ”๊ฐ€
        		if (paging.currentPage === i) {
        			pageHtml += " active'>";
        			pageHtml += "<button class='page-link' style='cursor: pointer;' onclick='movePage("+ i +")'>";
        			pageHtml += i;
        			pageHtml += "</button>";
        			pageHtml += "</li>"
        		} else {
        			pageHtml += "'>";
        			pageHtml += "<button class='page-link' style='cursor: pointer;' onclick='movePage("+ i +")'>";
        			pageHtml += i;
        			pageHtml += "</button>";
        			pageHtml += "</li>"
        		}
        	}
    
        	//next
        	const next = parseInt(paging.currentPage) + 1;
        	pageHtml += "<li class='paginate_button page-item next'>";
    
        	if (paging.currentPage === paging.lastPage) {
        		pageHtml += "<button class='page-link' disabled>";
        	} else {
        		pageHtml += "<button class='page-link' style='cursor: pointer;' onclick='movePage("+ next +")'>";
        	}
        	pageHtml += "<span class='hidden'>next page</span>";
        	pageHtml += "</button>";
        	pageHtml += "</li>"
    
        	//last
        	const last = parseInt(paging.lastPage);
        	pageHtml += "<li class='paginate_button page-item last'>";
    
        	if (paging.currentPage === paging.lastPage) {
        		pageHtml += "<button class='page-link' disabled>";
        	} else {
        		pageHtml += "<button class='page-link' style='cursor: pointer;' onclick='movePage("+ last +")'>";
        	}
        	pageHtml += "<span class='hidden'>last page</span>";
        	pageHtml += "</button>";
        	pageHtml += "</li>"
    
        	pageHtml += "</ul>";
        	//ํŽ˜์ด์ง€๋„ค์ด์…˜ ์˜์—ญ์— ๋ฐ˜์˜
        	$(".tbl-paging").html(pageHtml);
        }

    after source ๐Ÿ˜Ž

    /**
         * ํŽ˜์ด์ง€๋„ค์ด์…˜์„ ๊ทธ๋ฆฐ๋‹ค.
         * @param paging
         */
        function drawPagination(paging) {
    
        	let pageHtml = "";
        	pageHtml += "<ul class='pagination'>";
    
        	//first
        	const first = parseInt(paging.firstPage);
        	pageHtml += "<li class='paginate_button page-item first'>";
    
        	if (paging.currentPage === paging.firstPage) {
        		pageHtml += "<button type='button' class='page-link' disabled>";
        	} else {
        		pageHtml += "<button type='button'  class='page-link' style='cursor: pointer;' onclick='movePage("+ first +")'>";
        	}
        	pageHtml += "<span class='hidden'>first page</span>";
        	pageHtml += "</button>";
        	pageHtml += "</li>"
    
        	//prev
        	const prev = parseInt(paging.currentPage) -1;
        	pageHtml += "<li class='paginate_button page-item prev'>";
    
        	if (paging.firstPage === paging.currentPage) {
        		pageHtml += "<button type='button'  class='page-link' disabled>";
        	} else {
        		pageHtml += "<button type='button'  class='page-link' style='cursor: pointer;' onclick='movePage("+ prev +")'>";
        	}
        	pageHtml += "<span class='hidden'>prev page</span>";
        	pageHtml += "</button>";
        	pageHtml += "</li>";
    
        	//โ‘  ~ โ‘ฉ
        	for (let i = paging.firstPageNo; i <= paging.lastPageNo; i++) {
        		pageHtml += "<li class='paginate_button page-item";
        		//ํ˜„์žฌ ํŽ˜์ด์ง€๊ฐ€ ์ธ๋ฑ์Šค์™€ ๊ฐ™์œผ๋ฉด active class ์ถ”๊ฐ€
        		if (paging.currentPage === i) {
        			pageHtml += " active'>";
        			pageHtml += "<button type='button'  class='page-link' style='cursor: pointer;' onclick='movePage("+ i +")'>";
        			pageHtml += i;
        			pageHtml += "</button>";
        			pageHtml += "</li>"
        		} else {
        			pageHtml += "'>";
        			pageHtml += "<button type='button'  class='page-link' style='cursor: pointer;' onclick='movePage("+ i +")'>";
        			pageHtml += i;
        			pageHtml += "</button>";
        			pageHtml += "</li>"
        		}
        	}
    
        	//next
        	const next = parseInt(paging.currentPage) + 1;
        	pageHtml += "<li class='paginate_button page-item next'>";
    
        	if (paging.currentPage === paging.lastPage) {
        		pageHtml += "<button type='button'  class='page-link' disabled>";
        	} else {
        		pageHtml += "<button type='button'  class='page-link' style='cursor: pointer;' onclick='movePage("+ next +")'>";
        	}
        	pageHtml += "<span class='hidden'>next page</span>";
        	pageHtml += "</button>";
        	pageHtml += "</li>"
    
        	//last
        	const last = parseInt(paging.lastPage);
        	pageHtml += "<li class='paginate_button page-item last'>";
    
        	if (paging.currentPage === paging.lastPage) {
        		pageHtml += "<button type='button'  class='page-link' disabled>";
        	} else {
        		pageHtml += "<button type='button'  class='page-link' style='cursor: pointer;' onclick='movePage("+ last +")'>";
        	}
        	pageHtml += "<span class='hidden'>last page</span>";
        	pageHtml += "</button>";
        	pageHtml += "</li>"
    
        	pageHtml += "</ul>";
        	//ํŽ˜์ด์ง€๋„ค์ด์…˜ ์˜์—ญ์— ๋ฐ˜์˜
        	$(".tbl-paging").html(pageHtml);
        }

    ๋‹ฌ๋ผ์ง„ ๋ถ€๋ถ„์€ ๋™์ ์œผ๋กœ ๋งŒ๋“  button์— type์„ button์œผ๋กœ ์„ค์ •ํ–ˆ๋‹ค.

    ์ด๋ฒˆ์— ์•Œ๊ฒŒ๋œ ์‚ฌ์‹ค์ธ๋ฐ button์— type์„ ๋ช…์‹œํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ default๊ฐ’์€ submit์ด๋ผ๊ณ  ํ•œ๋‹ค.

    ๊ทธ๋Ÿฌ๋‹ˆ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ ๋•Œ๋งˆ๋‹ค ajax๋กœ 2ํŽ˜์ด์ง€๋ฅผ ๊ฐ€์ ธ์˜จ ๋‹ค์Œ form submit์„ ํ•œ๋ฒˆ ๋” ํ•˜๊ฒŒ ๋œ๊ฑฐ๋‹ค.

    form action์— ์•„๋ฌด๊ฐ’๋„ ์ฃผ์ง€ ์•Š์•˜๊ธฐ์— ํ˜„์žฌ ํŽ˜์ด์ง€๋กœ submit์„ ํƒœ์›Œ์„œ ๊ณ„์† ๊ฐ™์€ ํŽ˜์ด์ง€๊ฐ€ ๋กœ๋“œ ๋œ ๊ฑฐ๋‹ค.

    ์‚ฌ์‹ค์„ ์•Œ๊ฒŒ ๋œ ํ›„ form action์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์ฃผ์†Œ๋ฅผ ์คฌ๋”๋‹ˆ ์˜ˆ์ƒ๋Œ€๋กœ 404 ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

    ์ด ์‚ฝ์งˆ์„ ํ†ตํ•ด ๋А๋‚€ ๊ตํ›ˆ: button์„ ๋งŒ๋“ค ๋•Œ ๋ช…์‹œ์ ์œผ๋กœ ํ•ญ์ƒ type์„ ์„ ์–ธํ•˜๋„๋ก ํ•˜์ž.

    ์ „์ฒด ์†Œ์Šค(์ƒ˜ํ”Œ์šฉ)

    ๋ฆฌํŒฉํ† ๋ง X

    <html xmlns:th="http://www.thymeleaf.org">
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <script src="https://code.jquery.com/jquery-latest.min.js"></script>
    <style>
        li {
            list-style: none;
            float: left;
        }
    </style>
    </head>
    <body>
    <form id="searchForm">
        <select name="type" id="type">
            <option value="">์ „์ฒด</option>
            <option value="T">์ œ๋ชฉ</option>
            <option value="C">๋‚ด์šฉ</option>
            <option value="W">์ž‘์„ฑ์ž</option>
        </select>
        <input type="text" name="keyword" id="keyword">
        <button id="searchBtn">๊ฒ€์ƒ‰</button>
        <a th:href="@{/board/reg}">๊ธ€์“ฐ๊ธฐ</a>
        <div id="count"></div>
    <table>
        <thead>
            <th>๋ฒˆํ˜ธ</th>
            <th>์ œ๋ชฉ</th>
            <th>๋‚ด์šฉ</th>
            <th>์ž‘์„ฑ์ž</th>
            <th>๋“ฑ๋ก์ผ</th>
        </thead>
        <tbody id="boardBody"></tbody>
    </table>
        <div class="tbl-paging"></div>
    </form>
    
    <script th:inline="javascript" type="text/javascript">
        //๊ตฌ๋ถ„
        const menu = "board";
        //๊ฒ€์ƒ‰์กฐ๊ฑด
        let searchData = "";
    
        $(function () {
    
            getList("", 1, menu);
        });
    
        //๊ฒ€์ƒ‰
        $("#searchBtn").on("click", function () {
            searchData = $('#searchForm').serialize();
            getList(searchData, 1, menu);
        });
    
        /**
         * ํŽ˜์ด์ง€ ์ด๋™
         * @param currentPage
         */
        function movePage(currentPage){
        	//๊ฒ€์ƒ‰์กฐ๊ฑด์ด ์žˆ์„ ๊ฒฝ์šฐ์—๋งŒ ๊ฒ€์ƒ‰์กฐ๊ฑด ์ถ”๊ฐ€(๊ฒ€์ƒ‰์กฐ๊ฑด ์œ ์ง€ํ•œ ์ฑ„ ํŽ˜์ด์ง€ ์ด๋™)
        	(searchData != null) ? getList(searchData, currentPage, menu) : getList("", currentPage, menu);
        }
    
        /**
         * ajax ํ†ต์‹ ์œผ๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ์กฐํšŒํ•œ๋‹ค.
         * @param searchParam ๊ฒ€์ƒ‰์กฐ๊ฑด
         * @param currentPage ํ˜„์žฌ ํŽ˜์ด์ง€
         * @param type ๋ฉ”๋‰ดํƒ€์ž…
         */
        function getList(searchParam, currentPage, type){
            console.log("getList!!!!");
        	$.ajax({
        		url: "/api/" + type +"?currentPage=" + currentPage,
        		type:"get",
        		//IE ๋ธŒ๋ผ์šฐ์ € ์‚ฌ์šฉ ์•ˆํ•œ๋‹ค๋Š” ๊ฐ€์ •ํ•˜์— ์ฃผ์„์ฒ˜๋ฆฌ
        		// cache: false,
        		data: searchParam,
        		success: function (data){
        			//draw tbody
        			drawTbody(data, type);
        			//draw pagination
        			drawPagination(data.paging);
        		},
        		error:function(e){
        		}
        	});
        }
    
        /**
         * tbody์— html์„ ๋ฎ์–ด์“ด๋‹ค.
         * @param data
         * @param gubun
         */
        function drawTbody(data, gubun) {
            //drawHtml, draw ๋Œ€์ƒ tbody
            let htmlData, targetTbody = "";
            targetTbody = "boardBody";
    
            // foreach start
            for (let i = 0; i < data.list.length; i++) {
                htmlData += "<tr ";
                htmlData += "data-sno=" + data.list[i].boardSno + ">";
                htmlData += "<td>";
                htmlData += data.list[i].no;
                htmlData += "</td>";
                htmlData += "<td>";
                htmlData += "<a href='#' onclick='detail(\"" + data.list[i].boardSno + "\")'>";
                htmlData += data.list[i].title;
                htmlData += "</a>";
                htmlData += "</td>";
                htmlData += "<td>";
                htmlData += data.list[i].content;
                htmlData += "</td>";
                htmlData += "<td>";
                htmlData += data.list[i].userId;
                htmlData += "</td>";
                htmlData += "<td>";
                htmlData += data.list[i].regDate;
                htmlData += "</td>";
                htmlData += "<td>";
                htmlData += "<a href='#' onclick='delBoard(\"" + data.list[i].boardSno + "\")'>์‚ญ์ œ</a>";
                htmlData += "</td>";
                htmlData += "</tr>";
            }
            // foreach end
            //tbody์— ๋ฐ˜์˜
            $("#" + targetTbody + "").html(htmlData);
            countHtml = "";
            countHtml += data.count + "๊ฑด";
            $("#count").text(countHtml);
        }
        // end function getList()
    
        //์ƒ์„ธ๋ณด๊ธฐ
        function detail(sno) {
            location.href = "/board/mod/" + sno;
        }
    
        function delBoard(sno) {
            $.ajax({
                url: "/api/board/" + sno,
                type:"delete",
                //IE ๋ธŒ๋ผ์šฐ์ € ์‚ฌ์šฉ ์•ˆํ•œ๋‹ค๋Š” ๊ฐ€์ •ํ•˜์— ์ฃผ์„์ฒ˜๋ฆฌ
                // cache: false,
                success: function (data) {
                    if(data.code === "1") alert("์‚ญ์ œ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
                    location.href = "/board"
                },
                error:function(e){
                    console.log(e);
                }
            });
        }
    
        /**
         * ํŽ˜์ด์ง€๋„ค์ด์…˜์„ ๊ทธ๋ฆฐ๋‹ค.
         * @param paging
         */
        function drawPagination(paging) {
    
        	let pageHtml = "";
        	pageHtml += "<ul class='pagination'>";
    
        	//first
        	const first = parseInt(paging.firstPage);
        	pageHtml += "<li class='paginate_button page-item first'>";
    
        	if (paging.currentPage === paging.firstPage) {
        		pageHtml += "<button type='button' class='page-link' disabled>";
        	} else {
        		pageHtml += "<button type='button'  class='page-link' style='cursor: pointer;' onclick='movePage("+ first +")'>";
        	}
        	pageHtml += "<span class='hidden'>first page</span>";
        	pageHtml += "</button>";
        	pageHtml += "</li>"
    
        	//prev
        	const prev = parseInt(paging.currentPage) -1;
        	pageHtml += "<li class='paginate_button page-item prev'>";
    
        	if (paging.firstPage === paging.currentPage) {
        		pageHtml += "<button type='button'  class='page-link' disabled>";
        	} else {
        		pageHtml += "<button type='button'  class='page-link' style='cursor: pointer;' onclick='movePage("+ prev +")'>";
        	}
        	pageHtml += "<span class='hidden'>prev page</span>";
        	pageHtml += "</button>";
        	pageHtml += "</li>";
    
        	//โ‘  ~ โ‘ฉ
        	for (let i = paging.firstPageNo; i <= paging.lastPageNo; i++) {
        		pageHtml += "<li class='paginate_button page-item";
        		//ํ˜„์žฌ ํŽ˜์ด์ง€๊ฐ€ ์ธ๋ฑ์Šค์™€ ๊ฐ™์œผ๋ฉด active class ์ถ”๊ฐ€
        		if (paging.currentPage === i) {
        			pageHtml += " active'>";
        			pageHtml += "<button type='button'  class='page-link' style='cursor: pointer;' onclick='movePage("+ i +")'>";
        			pageHtml += i;
        			pageHtml += "</button>";
        			pageHtml += "</li>"
        		} else {
        			pageHtml += "'>";
        			pageHtml += "<button type='button'  class='page-link' style='cursor: pointer;' onclick='movePage("+ i +")'>";
        			pageHtml += i;
        			pageHtml += "</button>";
        			pageHtml += "</li>"
        		}
        	}
    
        	//next
        	const next = parseInt(paging.currentPage) + 1;
        	pageHtml += "<li class='paginate_button page-item next'>";
    
        	if (paging.currentPage === paging.lastPage) {
        		pageHtml += "<button type='button'  class='page-link' disabled>";
        	} else {
        		pageHtml += "<button type='button'  class='page-link' style='cursor: pointer;' onclick='movePage("+ next +")'>";
        	}
        	pageHtml += "<span class='hidden'>next page</span>";
        	pageHtml += "</button>";
        	pageHtml += "</li>"
    
        	//last
        	const last = parseInt(paging.lastPage);
        	pageHtml += "<li class='paginate_button page-item last'>";
    
        	if (paging.currentPage === paging.lastPage) {
        		pageHtml += "<button type='button'  class='page-link' disabled>";
        	} else {
        		pageHtml += "<button type='button'  class='page-link' style='cursor: pointer;' onclick='movePage("+ last +")'>";
        	}
        	pageHtml += "<span class='hidden'>last page</span>";
        	pageHtml += "</button>";
        	pageHtml += "</li>"
    
        	pageHtml += "</ul>";
        	//ํŽ˜์ด์ง€๋„ค์ด์…˜ ์˜์—ญ์— ๋ฐ˜์˜
        	$(".tbl-paging").html(pageHtml);
        }
    
    
    </script>
    
    </body>
    </html>

    ๊ฐœ์ธ ์Šคํ„ฐ๋”” ๊ธฐ๋ก์„ ๋ฉ”๋ชจํ•˜๋Š” ๊ณต๊ฐ„์ด๋ผ ํ‹€๋ฆฐ์ ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    ํ‹€๋ฆฐ ์  ์žˆ์„ ๊ฒฝ์šฐ ๋Œ“๊ธ€ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

    ๋Œ“๊ธ€