[Springboot] ๋Œ“๊ธ€ ๊ฒŒ์‹œํŒ #3 (ajax์‚ฌ์šฉํ•œ ๋Œ“๊ธ€ crud)

728x90

๐Ÿ”” TODO :  Ajax๋ฅผ ์‚ฌ์šฉํ•œ , ๋Œ“๊ธ€ ๋“ฑ๋ก , ๋Œ“๊ธ€ ์ˆ˜์ •, ์‚ญ์ œ

๐Ÿ“˜ project : ์ธํ…”๋ฆฌ์ œ์ด - SpringBootMybatisMini

 

form : content.jsp
dto : MbAnswerDto

controller : MbanswerController.java
Mapper : mbanswersql.xml
service : MbanswerMapperInter.java

 

 

์Šคํ”„๋ง๋ถ€ํŠธ ๋™์ž‘๊ด€์ • ๊ฐ„๋‹จ

  1. ํด๋ผ์ด์–ธํŠธ๊ฐ€ JSP ํŽ˜์ด์ง€์— ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์ œ์ถœํ•ฉ๋‹ˆ๋‹ค.
  2. JSP ํŽ˜์ด์ง€์—์„œ ์ปจํŠธ๋กค๋Ÿฌ์—๊ฒŒ ์š”์ฒญ์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค.
  3. ์ปจํŠธ๋กค๋Ÿฌ๋Š” ์š”์ฒญ์„ ๋ฐ›๊ณ  ํ•„์š”ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  4. ์ปจํŠธ๋กค๋Ÿฌ๋Š” ์„œ๋น„์Šค๋กœ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ์œ„์ž„ํ•ฉ๋‹ˆ๋‹ค.
  5. ์„œ๋น„์Šค๋Š” ํ•„์š”ํ•œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์‹คํ–‰ํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
  6. ์„œ๋น„์Šค๊ฐ€ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ์™„๋ฃŒํ•˜๋ฉด, ๊ฒฐ๊ณผ๋ฅผ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  7. ์ปจํŠธ๋กค๋Ÿฌ๋Š” JSP ํŽ˜์ด์ง€๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
  8. JSP ํŽ˜์ด์ง€๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋™์  ์›น ํŽ˜์ด์ง€๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๊ฒฐ๋ก  : ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ํ›„ ๋ฐ˜ํ™˜๋˜๋Š” ๊ณณ์€  ์ปจํŠธ๋กค๋Ÿฌ์ด๋‹ค.


์Šคํ”„๋ง๋ถ€ํŠธ๊ฐ€ ๋™์ž‘ํ•˜๋Š”  ๊ณผ์ • ๋Œ€๋กœ ์„ค๋ช…์„ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 

 

form

 <div style="margin : 50px 150px;">
    <table class="table table-bordered" style="width:800px;">
        <tr>
          <td>
            <h4><b>${dto.subject}</b>
              <span style="font-size:0.7em; color:gray; float: right">
                  ๐Ÿ”” ์กฐํšŒ์ˆ˜ : ${dto.readcount}&nbsp;&nbsp;
                  ๐Ÿ•ฐ ์ž‘์„ฑ์ผ : <fmt:formatDate value="${dto.writeday}" pattern="yyyy-MM-dd HH:mm:ss"/>
              </span>
            </h4>
              <span><b id="myid" myid="${sessionScope.myid}">์ž‘์„ฑ์ž : ${dto.name} (${dto.myid})</b></span>

              <c:if test="${dto.uploadfile!= 'no'}">
                <span style="float:right">
                  <a href="download?clip=${dto.uploadfile}">
                    <i class="bi bi-arrow-down-circle">&nbsp;</i><b>${dto.uploadfile}</b>
                  </a>
                </span>
              </c:if>
          </td>
        </tr>
      <tr>
        <td>
          <c:if test="${bupload ==true}">
          <img src="../savefile/${dto.uploadfile}" style="width:350px; height:400px;">
          </c:if>
          <br><br>
          <pre>
            ${dto.content}
          </pre>
            <br>
            <b>์กฐํšŒ ์ˆ˜ : ${dto.readcount}</b> &nbsp;&nbsp;&nbsp;
            <b>๋Œ“๊ธ€ ์ˆ˜ : <span class="acount"></span></b>
        </td>
      </tr>

        <%--๋Œ“๊ธ€--%>
        <tr>
            <td>
                <input type="hidden" id="num" value="${dto.num}">
                <div class="alist"></div>
                <c:if test="${sessionScope.loginok != null}">
                    <div class="aform">
                        <div>
                            <input type="text" class="form-control" style="width:500px; display: inline-flex" placeholder="๋Œ“๊ธ€ ์ž…๋ ฅ" id="content" >
                            <button type="button" class="btn btn-outline-danger" id="btnansweradd" num="${dto.num}">๋“ฑ๋ก</button>
                        </div>
                    </div>
                </c:if>
            </td>
        </tr>

      <c:if test="${sessionScope.loginok!=null}">
      <tr>
        <td>
          <button type="button" class="btn btn-outline-dark" onclick="location.href='form?currnePage=${currentPage}'" style="width:100px;">๊ธ€์ž‘์„ฑ</button>
          <button type="button" class="btn btn-outline-dark" onclick="location.href='list?currnePage=${currentPage}'" style="width:100px;">๋ชฉ๋ก</button>
          <c:if test="${sessionScope.loginok!=null and sessionScope.myid ==dto.myid}">
          <button type="button" class="btn btn-outline-dark" onclick="location.href='update?num=${dto.num}&currentPage=${currentPage}'" style="width:100px;">์ˆ˜์ •</button>
          <button type="button" class="btn btn-outline-dark" onclick="location.href='delete?num=${dto.num}&currentPage=${currentPage}'" style="width:100px;">์‚ญ์ œ</button>
          </c:if>

        </td>
      </tr>
      </c:if>
    </table>
  </div>
</body>
  • ์ด form์—์„œ๋Š” client๊ฐ€ ์š”์ฒญํ•œ ๋ฐ์ดํ„ฐ๋ฅผ list๋กœ ๋„์šด ๊ฒƒ์ด๊ณ , ๊ทธ list์—์„œ ๋Œ“๊ธ€์„ ๋‹ฌ๊ธฐ ์œ„ํ•œ form์ž…๋‹ˆ๋‹ค.
    • form์•ˆ์— <div.alist> ์—ฌ๊ธฐ div์— ๋Œ“๊ธ€์„ ๋„์šธ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค. ์ด div์— ๋Œ“๊ธ€์„ ๋„์šฐ๊ธฐ ์œ„ํ•ด์„œ ajax๋น„๋™๊ธฐ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌ๋ฅผ      ํ•ฉ๋‹ˆ๋‹ค.

 

๋‹ค์Œ์œผ๋กœ๋Š” Ajax๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ ค๋ฉด Controller , service ,mapper์—์„œ ๋ฏธ๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค๊ณ  sql์ž‘์„ฑ์„ ๋๋‚ด๋†”์•ผ์ง€ ๋ฐ”๋กœ๋ฐ”๋กœ ajax์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค

 

#1) dto

import java.sql.Timestamp;
import org.apache.ibatis.type.Alias;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

@Data
@Alias("mbdto")
public class MbAnswerDto {
	private String idx;
	private String num;
	private String name;
	private String myid;
	private String content;
	@JsonFormat(pattern = "yyyyMMdd")
	private Timestamp writeday;
}

 

  • DB์— ์ƒ์„ฑํ•ด๋‘” column ๋“ค์ด๋ž‘ ์ด๋ฆ„์„ ๊ฐ™๊ฒŒ ์ƒ์„ฑ์„ ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค (datatype์€ ๋‹ฌ๋ผ๋„ ์ƒ๊ด€์—†์Šต๋‹ˆ๋‹ค)
  • ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ๊ฑด idx๋Š” -> primary key์ด๊ณ 
    • num์€ Mbanswer๋ง๊ณ  ๊ทธ์ „์— ์ƒ์„ฑํ•œ memboard num์ด๋ž‘ foreign key๋ฅผ ์ฃผ์–ด ์ฐธ์กฐ๋ฅผ ์‹œํ‚จ ๊ฒƒ ์ž…๋‹ˆ๋‹ค.
  • num์€ ๋Œ“๊ธ€์„ ๋‹ฌ๊ธฐ ์ „, ์ž‘์„ฑ ๋œ ๊ธ€์„ ๋ฒˆํ˜ธ๋ฅผ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค
    • ์ฆ‰ ์ž‘์„ฑ๋œ ๊ธ€์€ num์œผ๋กœ ๊ตฌ๋ณ„์ด๊ณ , idx๋Š” ๊ทธ ์ž‘์„ฑ๋œ ๊ธ€num ์— ๋Œ“๊ธ€์„ ๋‹ค๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ฐจ๋ณ„ํ™”๋ฅผ ๋‘ฌ์•ผํ•จ.

 

#2) service ์ƒ์„ฑ

-์ค‘์š”ํ•œ๊ฑด service๋ฅผ ์ง์ ‘์ ์œผ๋กœ ์ƒ์„ฑํ•˜์ง€๋Š” ์•Š์•˜์ง€๋งŒ, ์„œ๋น„์Šค ์—ญํ• ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ํด๋ž˜์Šค ์ž…๋‹ˆ๋‹ค

- ์™œ ์ผ๊นŒ?? ๋ผ๊ณ  ์ƒ๊ฐํ•ด๋ดค๋Š”๋ฐ ๊ทธ๊ฒƒ์— ๋Œ€ํ•œ ๋‹ต๋ณ€์€ ๋‹ค์‹œ ๊ณต๋ถ€ํ•˜๊ณ  ์—…๋กœ๋“œ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

package boot.data.mapper;

import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import boot.data.dto.MbAnswerDto;

@Mapper
public interface MbanswerMapperInter {

	public void insertMbanswer(MbAnswerDto dto);
	public List<MbAnswerDto> getAllAnswers(String num);  //num ๊ฐ’์— ๋”ฐ๋ฅธ ์ •๋ณด ๋„˜๊ธฐ๊ธฐ
	public MbAnswerDto getAnswer(String idx); //idx๊ฐ’์— ๋”ฐ๋ฅธ answer ๊ฐ’๋“ค ๊ฐ€์ ธ์˜ค๊ธฐ
	public void updateMbanswer(MbAnswerDto dto);
	public void deleteMbanswer(String idx);
}
  • @Mapper ์–ด๋…ธํ…Œ์ด์…˜์€ -> Marker์ด๋‹ค.  ์ฆ‰ ์ด๊ฒŒ mapper๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ณณ์ด๋‹ค ๋ผ๋Š”๊ฑธ ์•Œ๋ ค์ฃผ๋Š” ์–ด๋…ธํ…Œ์ด์…˜
    • @Mapper์„ ์ƒ์œ„ ์–ด๋…ธํ…Œ์ด์…˜์€ @Repository ์ด๋‹ค  -> @Repository๋Š” bean์— ๋“ฑ๋ก์„ ์‹œ์ผœ์ค€๋‹ค. 

 

#3) mapper 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="boot.data.mapper.MbanswerMapperInter">
    <insert id="insertMbanswer" parameterType="mbdto">
        insert into mbanswer(num,name,myid,content,writeday) values (#{num},#{name},#{myid},#{content},now())
    </insert>
    <select id="getAllAnswers" resultType="mbdto" parameterType="String">
        select * from mbanswer where num=#{num} order by idx desc;
    </select>

    <select id="getAnswer" resultType="mbdto" parameterType="String">
        select * from mbanswer where idx=#{idx}
    </select>

    <update id="updateMbanswer" parameterType="mbdto">
        update mbanswer set content=#{content} where idx=#{idx}
    </update>

    <delete id="deleteMbanswer" parameterType="String">
        delete from mbanswer where idx=#{idx}
    </delete>
</mapper>
  • mapper์— ๋“ค์–ด๊ฐ€๋Š” id๋“ค์€ service์—์„œ ์ƒ์„ฑํ•œ ๋ฉ”์†Œ๋“œ ์ด๋ฆ„์ด๋ž‘ ๊ผญ ๊ฐ™๊ฒŒ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. 4

 

#4) controller

package boot.data.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import boot.data.dto.MbAnswerDto;
import boot.data.mapper.MbanswerMapperInter;
import boot.data.service.MemberService;

@RestController
@RequestMapping("/mbanswer")
public class MbanswerController {

	@Autowired
	MbanswerMapperInter mapper;
	@Autowired
	MemberService memberService;

	@PostMapping("/ainsert")
	public void insert(@ModelAttribute MbAnswerDto dto, HttpSession httpSession) {
		//์„ธ์…˜์—์„œ ์ €์žฅํ•œ ์•„์ด๋””๋ฅผ dto์— ์ €์žฅ์‹œํ‚จ๋‹ค.
		String myid = (String)httpSession.getAttribute("myid");
		dto.setMyid(myid);
		//์„ธ์…˜์—์„œ ์ €์žฅ๋œ ์ด๋ฆ„์„ dto์— ์ €์žฅ์‹œํ‚จ๋‹ค.
		String name =memberService.getName(myid);
		dto.setName(name);

		mapper.insertMbanswer(dto);
	}

	@GetMapping("/alist")
	public List<MbAnswerDto> alist(@RequestParam String num)
	{
		return mapper.getAllAnswers(num);
	}

	@GetMapping("/mbdelete")
	public void adelete(@RequestParam String idx) {
		mapper.deleteMbanswer(idx);
	}


}
  • Ajax๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ๋–„๋Š” @Responsebody || @RestController ๋ฅผ ๊ผญ ์„ ์–ธํ•˜๊ณ  ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค ์•„๋‹ˆ๋ฉด ์ปดํŒŒ์ผ์—๋Ÿฌ

 

  • insertํ• ๋•Œ๋Š” @Modelattribute๋ฅผ ํ†ตํ•ด MbanswerDto ๊ฐ’์„ ๋‹ค ๋ณด๋‚ด์ค€๋‹ค. 
    • ๊ทธ๋ฆฌ๊ณ  httpsession์„ ์‚ฌ์šฉํ•ด์„œ , ์„ธ์…˜์—์„œ ์ €์žฅํ•œ ์•„์ด๋””๋ฅผ ๋ณด๋‚ด์ค„ dto์— ๋‹ด์•„์„œ ๋ณด๋‚ด์ค€๋‹ค.
    • ๋‹ค๋ฅธ๋ฐฉ์‹์œผ๋กœ ๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด๋‘” ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด์„œ name๊ฐ’์„ ๊ฐ™์ด dto์— ๋‹ด์•„์„œ ๋ณด๋‚ด์ค€๋‹ค. 

 

  • List๋กœ ์ถœ๋ ฅํ•  ๋•Œ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ, ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’์€ ๋‹น์—ฐํžˆ dto๊ฐ’๋“ค์ด๊ณ , ์ด dto๊ฐ’๋“ค์„ list์— ๋‹ด์•„์„œ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค
    • ๊ทธ๋ฆฌ๊ณ  num๊ฐ’์— ๋งž๋Š” list๋ฅผ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— num๊ฐ’ ๋˜ํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ’์œผ๋กœ ๊ฐ™์ด ๋ณด๋‚ด์ฃผ๊ณ  service์—์„œ ๋ฏธ๋ฆฌ ์ž‘์„ฑํ•ด๋‘” ๋ฉ”์†Œ๋“œ์— num์„ ๋‹ด์•„์„œ return ํ•ด์ค€๋‹ค

 

  • delete๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ๋Œ“๊ธ€์— primary key์ธ idx์— ๋”ฐ๋ผ์„œ ์‚ญ์ œํ•ด์ค€๋‹ค.

 

#5) ๋งˆ์ง€๋ง‰์œผ๋กœ form์—์„œ ์œ„ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋“ค์„ ๊ฐ€์ง€๊ณ  ajax ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋ณด์—ฌ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. 

    <script>
        $(function(){

            list();

            $("#btnansweradd").click(function (){

                var num = $(this).attr("num")
                var content = $("#content").val();
                var myid  = $("#myid").attr("myid");
                loginok = "${sessionScope.loginok}";
                //alert(num+"_"+content+"_"+myid+"_"+loginok);

                if(content.length==0) {
                    alert("๋Œ“๊ธ€์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”");
                    return;
                }

                $.ajax({
                    type:"post",
                    url:"/mbanswer/ainsert",
                    dataType:"html",
                    data : {"num":num,"content":content},
                    success:function (data){
                        alert("๋“ฑ๋ก๋˜์—ˆ์Šต๋‹ˆ๋‹ค.")


                        //๋Œ“๊ธ€ ์ˆ˜์ •์ฐฝ ๋„์šฐ๊ธฐ
                        list();
                        //์ž…๋ ฅ๊ฐ’ ์ดˆ๊ธฐํ™”
                        $("#content").val("");
                    }
                })

            })

             //๋Œ“๊ธ€ ์‚ญ์ œ
             $(document).on("click","#del",function(){
                 var idx= $(this).attr("idx");
                 var a = confirm("์‚ญ์ œํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?");
                 //alert(idx);

                 if(a) {

                 $.ajax({
                     type:"get",
                     dataType :"json",
                     url : "/mbanswer/mbdelete",
                     data : {"idx":idx},
                     success:function(){
                            location.reload();

                        }
                    })
                 }
             })



            // $("i.update").click(function(){
            //
            // })





        })
        function list() {
            var num = $("#num").val();
            //alert(num);
            var loginok = "${sessionScope.loginok}";
            var myid = "${sessionScope.myid}";
            //alert(loginok + myid);

            $.ajax({
                type:"get",
                dataType :"json",
                url : "/mbanswer/alist",
                data : {"num":num},
                success:function(res){

                    $("span.acount").text(res.length);

                    var s = "";
                    $.each(res, function (i, dto) {
                        s += "<br>";
                        s += "<table class='table table-striped' style='width:800px; height:100px;'>"
                        s += "<input type='hidden' id='idx' value='idx'>";
                        s += "<tr>";
                        s += "<td><b> ๐Ÿ– ์ž‘์„ฑ์ž : " + dto.name + "</b>" + "<span class='day' style='float:right'> ์ž‘์„ฑ๋‚ ์งœ : "+dto.writeday+"&nbsp;&nbsp;";
                        if(loginok!=null || myid ==dto.myid) {
                        s += "<i class='bi bi-trash3-fill' id='del' idx='"+dto.idx+"' style='cursor: pointer'></i> &nbsp; <i class='bi bi-clipboard-check update' style='cursor: pointer'></i></span></td>";
                        }
                        s += "</tr>";
                        s += "<tr>";
                        s += "<td> ๐Ÿ“‹ ๋Œ“๊ธ€ : " +dto.content + "</td>";
                        s += "</tr>"
                        s += "</table>"
                    });

                    $("div.alist").html(s);

                }
            })
            //๋Œ“๊ธ€ ์ˆ˜์ •์ฐฝ ๋„์šฐ๊ธฐ
        }
    </script>

 

  • ๋Œ“๊ธ€ ๋“ฑ๋ก ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด, ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ  ๋Œ“๊ธ€์„ ํด๋ฆญํ•ด์„œ ์„œ๋ฒ„๋กœ ์ „์†ก๋  ๋•Œ๋Š” list๋กœ ์ถœ๋ ฅ ๋  ๊ฐ’์„ ๋ฏธ๋ฆฌ ์„ค์ •ํ•ด๋‘๊ณ  ajax ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค˜์•ผํ•ฉ๋‹ˆ๋‹ค.  ex) var content = $("#content").val();
    • ๋Œ“๊ธ€ ์ฐฝ์ด null๊ฐ’ ์ฆ‰ ๋Œ“๊ธ€์ฐฝ์— ์•„๋ฌด๊ฒƒ๋„ ์—†์ด ์„œ๋ฒ„๋กœ ์ „์†กํ•˜๋ คํ•˜๋ฉด ๋Œ“๊ธ€ ์ž…๋ ฅํ•˜๋ผ๋Š” ์•Œ๋ฆผ์ด ๋œฌ๋‹ค
  • ๊ทธ๋ฆฌ๊ณ  ์œ„ ๊ณผ์ •์ด ์ž˜ ๋˜์—ˆ๋‹ค๋ฉด ajax๋ฅผ ํ†ตํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค.

 

๋‹ค์Œ์œผ๋กœ๋Š” ajax์ฒ˜๋ฆฌ๋œ ๋ฐ์ดํ„ฐ๋“ค์„ list๋กœ ๋„์›๋‹ˆ๋‹ค.

  • ์ด๊ฒƒ ๋˜ํ•œ ajax๋กœ ์ฒ˜๋ฆฌ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ์กด์—๋Š” ๋Œ“๊ธ€๋“ค์ด ๋‹ค ๋ณด์ด๊ณ , ๋‚ด๊ฐ€ ๋Œ“๊ธ€์„ ๋“ฑ๋กํ•˜๋ฉด์€ ๋ฐ”๋กœ ๋งจ์œ„์— ์˜ฌ๋ผ์˜ค๊ฒŒํ•œ๋‹ค.
    • $("span.acount").text(res.length); ์ด ์ฝ”๋“œ๋Š” ๋Œ“๊ธ€์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ์ด๋‹ค.

 

๋‹ค์Œ์€ ์‚ญ์ œ ์ž…๋‹ˆ๋‹ค.

  • ์‚ญ์ œํ•˜๋Š” ๋ฒ„ํŠผ์— ๋ฏธ๋ฆฌ idx๊ฐ’์„ ์‹ฌ์–ด๋†“์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๋ฒ„ํŠผ์ด ํด๋ฆญ๋˜๋ฉด์€ idx๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋Š”๊ฒŒ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค
    • var idx= $(this).attr("idx");
  • ๊ทธ๋ฆฌ๊ณ  ๋ฌด๋‚œํ•˜๊ฒŒ ajax์ฒ˜๋ฆฌ๋ฅผํ•ด์„œ ์‚ญ์ œ ํ•˜๋ฉด ๋์ž…๋‹ˆ๋‹ค

 

๋‹ค์Œ์€ ์ˆ˜์ • ์ž…๋‹ˆ๋‹ค.

728x90