반응형
블로그 이미지
개발자로서 현장에서 일하면서 새로 접하는 기술들이나 알게된 정보 등을 정리하기 위한 블로그입니다. 운 좋게 미국에서 큰 회사들의 프로젝트에서 컬설턴트로 일하고 있어서 새로운 기술들을 접할 기회가 많이 있습니다. 미국의 IT 프로젝트에서 사용되는 툴들에 대해 많은 분들과 정보를 공유하고 싶습니다.
솔웅

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함

카테고리

Ajax 로 테이블 삽입하기

2012. 11. 6. 06:55 | Posted by 솔웅


반응형
Insert tables with Ajax



Insert a simple table


Ajax를 통해서 text를 가지고 있는 테이블을 삽입하는 방법을 살펴보죠.

아주 간단하게 구현할 수 있습니다.


Insert a table containing text elements by Ajax


<!DOCTYPE html>

<html> 

<head> 

  <meta name=viewport content="user-scalable=no,width=device-width" />

  <link rel=stylesheet href=jquery.mobile/jquery.mobile.css />

  <script src=jquery.js></script>

  <script src=jquery.mobile/jquery.mobile.js></script>

</head> 


<body> 


<div data-role=page id=home>

  <div data-role=header>

    <h1>Home</h1>

  </div>


  <div data-role=content>

    <p> Window content </p>  

  </div>

</div>


</body>

</html>


<script>


$.ajax (

  url : "action.php", 

  complete : function (xhr, result)

  {

    if (result != "success") return;

    var response = xhr.responseText;

    $("#home div:jqmData(role=content)").append (response);

  }

});   


</script>



Ajax에 의해 call 되는 서버의 action.php file 은 아래와 같습니다.


action.php file


<?
$html = "";
$html .= "<div class=ui-grid-b>";
$html .=   "<div class=ui-block-a>Element 1.1</div>";
$html .=   "<div class=ui-block-b>Element 1.2</div>";
$html .=   "<div class=ui-block-c>Element 1.3</div>";
$html .=   "<div class=ui-block-a>Element 2.1</div>";
$html .=   "<div class=ui-block-b>Element 2.2</div>";
$html .=   "<div class=ui-block-c>Element 2.3</div>";
$html .= "</div>";

echo utf8_encode ($html);
?>


action5.php

tistory443_01.html


페이지를 오픈하면 Ajax 가 server 로 call 해서 테이블을 받아오고 이것을 윈도우에 뿌려줍니다. 약간의 시간이 걸릴 수도 있습니다.





Insert a table with buttons


위의 예제에서 조금 변경을 해 보죠. 서버쪽에서 버튼을 가지고 있는 array 를 return 한다고 가정해 봅시다. 예를들어 OK button 과 Delete button을 건네 받을 겁니다.


Insert a table that contains buttons by Ajax


<!DOCTYPE html>

<html> 

<head> 

  <meta name=viewport content="user-scalable=no,width=device-width" />

  <link rel=stylesheet href=jquery.mobile/jquery.mobile.css />

  <script src=jquery.js></script>

  <script src=jquery.mobile/jquery.mobile.js></script>

  <style type=text/css>

    .ui-btn {

      width : 140px;

    }

  </style>

</head> 


<body> 


<div data-role=page id=home>

  <div data-role=header>

    <h1>Home</h1>

  </div>


  <div data-role=content>

    <p> Window content </p>  

  </div>

</div>


</body>

</html>


<script>


$.ajax (

  url : "action.php", 

  complete : function (xhr, result)

  {

    if (result != "success") return;

    var response = xhr.responseText;

    $("#home div:jqmData(role=content)").append (response);

    

    $("a").button ();

    $(".ui-grid-a").controlgroup ();

  }

});   


</script>



서버에서 return 되는 <a> links 들이 버튼으로 변경될 겁니다. 이 때 사용하는 메소드는 button () method 입니다. 이 버튼들은 controlgroup () method 로 묶은 후 border 로 둘러싸여질 겁니다. 위에 있는 ui-btn class 에 의해서 버튼들은 같은 width 를 가지게 됩니다.


action.php file


<?
$html = "";
$html .= "<div class=ui-grid-a data-type=horizontal>";
$html .=   "<div class=ui-block-a style=text-align:right>";
$html .=     "<a href=#> OK </a>";
$html .=   "</div>";
$html .=   "<div class=ui-block-b>";
$html .=     "<a href=#> Delete </a>";
$html .=   "</div>";
$html .= "</div>";

echo utf8_encode ($html);
?>


action6.php


tistory443_02.html




반응형