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

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함

카테고리


반응형

버튼을 다루는 것도 리스트를 다루는 것하고 비슷합니다. 여기서는 button () method를 사용해서 HTML element를 jQuery Mobile button으로 바꾸게 됩니다. 이 버튼들은 button standard component 에 맞게 display 됩니다.


Dynamically create a button


HTML 페이지에서 버튼을 dynamically 생성하는 것을 알아 보겠습니다.  그것을 하기 위해서 <a> element 를 만들고 거기에 data-role="button" attribute 를 달겠습니다. 버튼의 다른 기능들도 (data-inline, data-icon, etc..) 그 element의 attributes에서 정해줄 수 있습니다.


Dynamic creation of a button


<!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>


var html = "";

html += "<a id=btn href=# data-role=button data-icon=check data-iconpos=right>";

html +=     "Click the button";

html += "</a>";

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


$("#btn").bind ("click", function (event)

{

  alert ("click");

});


</script>



tistory430_01.html


이 코드를 실행하고 버튼을 누르면 아래와 같이 될 겁니다.




<a> element 는 다이나믹하게 들어갔습니다. 그리고 jQuery Mobile 버튼을 만들도록 하고 있죠. 다이나믹하게 들어간 <a> element 에 jQuery Mobile 버튼이 만들어지게 되는 겁니다.

반응형

리스트 다루기 예제

2012. 10. 29. 05:08 | Posted by 솔웅


반응형
Examples of list manipulation


Create lists containing sub-lists


이 글에서 우리는 리스트의 아이템을 클릭하면 리스트 안에 sublist 가 나오도록 할 겁니다. sub-list 는 클릭 된 아이템 밑에 나오겠죠. 그리고 한번 더 그 아이템을 클릭하면 sub-list를 숨기겠습니다. 이 기능은 accordion menu 의 핵심이죠.


Manage sub-lists in a list


<!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>  

    <ul data-role=listview data-inset=true>

      <li data-role=list-divider data-icon=arrow-d>

        <a href=#> List 1 </a></li>

      <li> Element 1.1 </li>

      <li> Element 1.2 </li>

      <li> Element 1.3 </li>

      <li> Element 1.4 </li>

      <li> Element 1.5 </li>

      <li data-role=list-divider data-icon=arrow-d>

        <a href=#> List 2 </a></li>

      <li> Element 2.1 </li>

      <li> Element 2.2 </li>

      <li> Element 2.3 </li>

      <li> Element 2.4 </li>

      <li> Element 2.5 </li>

      <li data-role=list-divider data-icon=arrow-d>

        <a href=#> List 3 </a></li>

      <li> Element 3.1 </li>

      <li> Element 3.2 </li>

      <li> Element 3.3 </li>

      <li> Element 3.4 </li>

      <li> Element 3.5 </li>

    </ul>

  </div>

</div>


</body>

</html>


<script>


$("li:not(:jqmData(role=list-divider))").hide ();


$("li:jqmData(role=list-divider)").bind ("vclick", function (event)

{

  $(this).nextUntil ("li:jqmData(role=list-divider)").toggle ();

});


</script>



tistory429_01.html


이 소스를 브라우저에서 열면 처음에는 리스트의 title들만 보입니다. 그리고 sub-list 들은 보이지 않죠. 이 리스트의 title들은 <li> elements 에 data-role="list-divider" attribute 를 가지고 있습니다.  바로 이 부분을 스크립트에서 처음 부분에서 받아서 사용하게 되는 겁니다.



Hide list items that are not titles of list


$("li:not(:jqmData(role=list-divider))").hide ();


bind () statement 는 title element들에서 click events 가 발생하는 것을 observe 하는데 사용합니다. 그 다음에는 sub-title 을 숨기고 보이고 하는 기능들을 진행하게 되죠.

프로그램이 처음 실행 될 때는 모든 list 들이 close 된 상태입니다.




Click on the first element (List 1):




sub-list 는 list 안에 있습니다. 이 리스트를 클릭하면 그 sub-list 가 보이기도 하고 숨기도 하죠.


Change the icon for a list item


이전 프로그램을 조금 더 발전시킬까요. 리스트가 open 인 상태에서 title에 있는 icon 을 바꾸고 싶습니다. 화살표 방향이 다른걸로 표시하고 싶습니다. close 되면 다시 원래 화살표 방향으로 돌아오구요.

To achieve this, we must look in more detail, how is designed the HTML code created by jQuery Mobile to display a list item containing the icon.

이를 위해서는 jQuery Mobile 에 의해 생성된 HTML 코드를 봐야 합니다.





위에 코드를 보시면 icon은 <span> element 의 ui-icon class 에 의해 표시됩니다. 또한 ui-icon-arrow-d class도 아이콘을 표시하는데 영향을 미칩니다. up arrow 아이콘을 표시하기 위해 ui-icon-arrow-u class를 바꾸겠습니다.


data-icon attribute 가 리스트 아이템 내의 아이콘을 표시하는데 별 역할을 안 하고 있는 부분을 잘 보세요. 이 attribute는 리스트 아이템을 생성할 때에만 사용됩니다. 그리고 그 이후의 modification은 영향을 미치지 않습니다.


Change the icon displayed in a list item when clicking it


<!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>  

    <ul data-role=listview data-inset=true>

      <li data-role=list-divider data-icon=arrow-d>

        <a href=#> List 1 </a></li>

      <li> Element 1.1 </li>

      <li> Element 1.2 </li>

      <li> Element 1.3 </li>

      <li> Element 1.4 </li>

      <li> Element 1.5 </li>

      <li data-role=list-divider data-icon=arrow-d>

        <a href=#> List 2 </a></li>

      <li> Element 2.1 </li>

      <li> Element 2.2 </li>

      <li> Element 2.3 </li>

      <li> Element 2.4 </li>

      <li> Element 2.5 </li>

      <li data-role=list-divider data-icon=arrow-d>

        <a href=#> List 3 </a></li>

      <li> Element 3.1 </li>

      <li> Element 3.2 </li>

      <li> Element 3.3 </li>

      <li> Element 3.4 </li>

      <li> Element 3.5 </li>

    </ul>

  </div>

</div>


</body>

</html>


<script>


$("li:not(:jqmData(role=list-divider))").hide ();


$("li:jqmData(role=list-divider)").bind ("vclick", function (event)

{

  $(this).nextUntil ("li:jqmData(role=list-divider)").toggle ();

  var $span = $(this).find ("span.ui-icon");

  if ($span.hasClass ("ui-icon-arrow-d")) 

  {

    $span.removeClass ("ui-icon-arrow-d");

    $span.addClass ("ui-icon-arrow-u");

  }

  else 

  {

    $span.removeClass ("ui-icon-arrow-u");

    $span.addClass ("ui-icon-arrow-d");

  }

});


</script>



tistory429_02.html

(그대로 따라 했는데 왜 제 브라우저에서는 화살표 아이콘이 안 나올까요?

혹시 화살표 아이콘 나오시는 분은 저에게 방법 좀 알려 주세요.)

For example, when List 3 is open:





Managing the click on the icon of an item in a static list


이제 static 리스트 아이템에서 아이콘을 클릭했을 때 처리하는 방법을 알아보겠습니다. 예를 들어 한 리스트에 5개의 아이템을 넣었습니다. 각 element는 각각 delete icon이 있고 이 delete icon을 클릭 하면 해당 element를 delete 합니다.


그러려면 각 <li> item 의 아이콘에 클릭이 일어날 경우 이를 다룰 수 있어야 겠죠. 위의 예제에서 보았듯이 이 아이콘은 <span> element 의 ui-icon CSS class에 의해 표시됩니다.  아래 샘플 코드를 보세요.


Manage click on the delete icon of list items


<!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>  

    <ul data-role=listview data-inset=true>

      <li data-icon=delete> <a href=#>Element 1 </a></li>

      <li data-icon=delete> <a href=#>Element 2 </a></li>

      <li data-icon=delete> <a href=#>Element 3 </a></li>

      <li data-icon=delete> <a href=#>Element 4 </a></li>

      <li data-icon=delete> <a href=#>Element 5 </a></li>

    </ul>

  </div>

</div>


</body>

</html>


<script>


$("li .ui-icon").bind ("click", function (event)

{

  $(this).closest ("li").remove ();

});


</script>






위와 같이 표시 되는데 제대로 작동되지는 않습니다. 그 이유는 아직 리스트의 HTML 이 jQuery Mobile code 로 transform 하지 않았기 때문이죠. 제대로 하기 위해서는 리스트가 finalize 되기까지 기다려야 합니다. 그러니까 bind () method 는 리스트가 finalize 한 이후에 나와야겠죠.


So we write:


Wait until the list was created to manage the click on the delete icon


<!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>  

    <ul data-role=listview data-inset=true>

      <li data-icon=delete> <a href=#>Element 1 </a></li>

      <li data-icon=delete> <a href=#>Element 2 </a></li>

      <li data-icon=delete> <a href=#>Element 3 </a></li>

      <li data-icon=delete> <a href=#>Element 4 </a></li>

      <li data-icon=delete> <a href=#>Element 5 </a></li>

    </ul>

  </div>

</div>


</body>

</html>


<script>


$("ul").bind ("listviewcreate", function (event)

{

  $("li .ui-icon").bind ("click", function (event)

  {

    $(this).closest ("li").remove ();

  }).css ("z-index", 10);

});


</script>

tistory429_03.html




Manage the click on the icon of an item in a dynamically created list (solution 1)


이제 리스트는 원래 HTML 내에서 존재하지는 않고 JavaScript 에 의해서 dynamically 생성된다는 것을 알 수 있습니다.


그러면 두번째 방법도 고려해 볼 수 있습니다. (물론 이전 방법도 훌륭한 방법입니다) 두번째 방법은 listviewcreate event를 사용해서 리스트가 생성되기를 기다리는 대신 jQuery Mobile listview () method를 사용해서 오리지널 HTML 코드를 transform 하도록 하는 겁니다. 그러면 jQuery Mobile conventions에 맞게 display 할 수 있습니다.


The program becomes:


Direct use of the listview () method


<!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>


var html = "";

html += "<ul data-inset=true>";

html += "<li data-icon=delete> <a href=#>Element 1 </a></li>";

html += "<li data-icon=delete> <a href=#>Element 2 </a></li>";

html += "<li data-icon=delete> <a href=#>Element 3 </a></li>";

html += "<li data-icon=delete> <a href=#>Element 4 </a></li>";

html += "<li data-icon=delete> <a href=#>Element 5 </a></li>";

html += "</ul>";

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


$("#home").bind ("pagecreate", function ()

{

  $("ul").listview ();

  

  $("li .ui-icon").bind ("click", function (event)

  {

    $(this).closest ("li").remove ();

  });

});


</script>



tistory429_04.html

이 소스도 제 경우엔 제대로 동작하지 않네요. 혹시 되시는 분 저에게 정보 좀 부탁드립니다.


listview () method call은 생성된 리스트를 jQuery Mobile 리스트로 dynamically convert 해 줍니다. 이 메소드는 window 가 이미 생성된 상태에서 call 될 수 있습니다. 그래서 여전히 pagecreate event를 사용하고 있는 겁니다.


Manage the click on the icon of an item in a dynamically created list (solution 2)


바로 전 샘플 코드에 대해 더 알아보죠. 리스트의 HTML 에 data-role="listview" attribute를 넣었습니다. 이것은 콤포넌트를 생성하기 위해 listview () method 를 call 하는 것을 막아 주는 겁니다.


어쨌든 리스트 아이템에 있는 아이콘에 click event가 일어나는 여부를 observation 하는 것은 리스트가 생성된 이후에나 가능합니다. 그래서 리스트에 listviewcreate event 를 사용해야 하는 겁니다.


Using the listviewcreate event


<!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>


var html = "";

html += "<ul data-role=listview data-inset=true>";

html += "<li data-icon=delete> <a href=#>Element 1 </a></li>";

html += "<li data-icon=delete> <a href=#>Element 2 </a></li>";

html += "<li data-icon=delete> <a href=#>Element 3 </a></li>";

html += "<li data-icon=delete> <a href=#>Element 4 </a></li>";

html += "<li data-icon=delete> <a href=#>Element 5 </a></li>";

html += "</ul>";

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


$("ul").bind ("listviewcreate", function ()

{

  $("li .ui-icon").bind ("click", function (event)

  {

    $(this).closest ("li").remove ();

  });

});


</script>



만약 listviewcreate event를 사용하지 않으면 이 리스트는 생성되지만 아이콘을 클릭해도 제대로 기능을 하지 않을 겁니다.


Allow deletion of a list item by clicking and holding


각 리스트 아이템마다 delete icon을 표시하는 것 보다 long click 을 감지해서 처리할 수도 있을 겁니다. 여기에 해당하는 이벤트가 taphold 입니다.


Delete an item from the list after a long click on the item


<!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>  

    <ul data-role=listview data-inset=true>

      <li> Element 1 </li>

      <li> Element 2 </li>

      <li> Element 3 </li>

      <li> Element 4 </li>

      <li> Element 5 </li>

    </ul>

  </div>

</div>


</body>

</html>


<script>


$("li").bind ("taphold", function (event)

{

  $(this).remove ();

});


</script>

tistory429_06.html




Allow deletion of a list item with a swipe


이제 오른쪽으로 swipe 했을 경우 아이템을 지우도록 해 보죠.


The event is managed by swiperight. Here is the program:


Manage swiperight events on the elements to remove


<!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>  

    <ul data-role=listview data-inset=true>

      <li> Element 1 </li>

      <li> Element 2 </li>

      <li> Element 3 </li>

      <li> Element 4 </li>

      <li> Element 5 </li>

    </ul>

  </div>

</div>


</body>

</html>


<script>


$("li").bind ("swiperight", function (event)

{

  $(this).remove ();

});


</script>



tistory429_07.html


Keep the rounded edges appearance to the list


이전 예제에서 리스트 블럭의 모서리가 rounding 처리 된 걸 보셨을 겁니다. 이 모서리들은 <ul> 에서 data-inset attribute 를 "true"로 세팅해서 처리한 겁니다.


아이템을 delete 했을 때 우리는 jQuery Mobile 에게 리스트의 첫번째 아이템과 마지막 아이템에 이 rounding 처리를 하라고 얘기해야 합니다. 첫번째나 마지막 아이템이 지워지면 그 자리를 차지하는 아이템에 rounding 처리가 되야 되겠죠. 이렇게 하기 위해 jQuery Mobile 은 첫번째 리스트 아이템에 ui-corner-top CSS class를 사용하고 마지막 리스트 아이템에 ui-corner-bottom CSS class를 사용합니다.

아이


이전 예제를 사용해서 여기에 CSS class를 추가 하겠습니다.


Keep the rounded edges appearance to the list


<!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>  

    <ul data-role=listview data-inset=true>

      <li> Element 1 </li>

      <li> Element 2 </li>

      <li> Element 3 </li>

      <li> Element 4 </li>

      <li> Element 5 </li>

    </ul>

  </div>

</div>


</body>

</html>


<script>


$("li").bind ("swiperight", function (event)

{

  $(this).remove ();

  $("ul").find ("li:first").addClass ("ui-corner-top");

  $("ul").find ("li:last").addClass ("ui-corner-bottom");

});


</script>


tistory429_08.html



이제 첫번째와 마지막 element 가 지워져도 계속 rounding 효과는 유지 됩니다.






다른 방법도 있는데요. 더 쉽습니다. jQuery Mobile 이 이 CSS 를 자동으로 관리하도록 하면 됩니다. listview ("refresh") method 를 사용하면 되요. 이를 위해 아래 두 라인을 맨 밑의 예제코드로 바꿔주면 됩니다.


Management of rounded edges on the lists (with CSS classes)


$("ul").find ("li:first").addClass ("ui-corner-top");

$("ul").find ("li:last").addClass ("ui-corner-bottom");


For this:


Management of rounded edges on the lists (with the listview ("refresh") method)


$("ul").listview ("refresh");






반응형

리스트 Customization 하기

2012. 10. 26. 21:06 | Posted by 솔웅


반응형
Customize lists


이전 글에 있는 소스를 이용하겠습니다.  5개의 element들이 있죠. 이것을 firefox 브라우저에 있는 firebug로 jQuery Mobile에 의해 생성된 HTML 을 보겠습니다.




각각의 list item은 ui-li CSS class 를 가지고 있습니다. <ul> 가 감싸고 있는 리스트는 ui-listview CSS class를 가지고 있습니다. 이 두개의 클래스를 지난번 작업했던 HTML 소스 안에 코딩해 넣어서 새로운 리스트 모양을 꾸미겠습니다.


Change the style of list items


<!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-li { 

      color : #0099FF;

      font-size : 20px;

      font-style : italic;

    }

    .ui-listview {

      padding : 10px;

      background-color : black;

    }

  </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>  

    <ul data-role=listview data-inset=true>

      <li> Element 1.1 </li>

      <li> Element 1.2 </li>

      <li> Element 1.3 </li>

      <li> Element 1.4 </li>

      <li> Element 1.5 </li>

    </ul>

  </div>

</div>


</body>

</html>


<script>


$("li").bind ("vclick", function (event)

{

  alert (this.innerHTML);

});


</script>







tistory426_01.html


리스트의 각 element의 높이가 커졌습니다. (새 글자 폰트에 맞게 조정이 된거죠.) 그리고 이 리스트는 검은 border로 감싸져 있습니다. 이것은 ui-listview CSS class 에서 정의한 것입니다.




반응형


반응형
Manage events on lists



리스트에 있어서 우리들이 활용할 수 있는 주요한 이벤트가 click event 입니다. jQuery Mobile 에서는 vclick virtual event 라고 할 수 있죠.


이 메소드는 jQuery 의 bind () method 를 사용해서 어떤 일을 할 수가 있습니다. 예를 들어 리스트 중의 한 아이템을 클릭했을 때 그 아이템 이름을 alert 으로 display 하는 것을 구현해 보겠습니다.


Taking into account the click of a list item


<!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>  

    <ul data-role=listview data-inset=true>

      <li> Element 1.1 </li>

      <li> Element 1.2 </li>

      <li> Element 1.3 </li>

      <li> Element 1.4 </li>

      <li> Element 1.5 </li>

    </ul>

  </div>

</div>


</body>

</html>


<script>


$("li").bind ("vclick", function (event)

{

  alert (this.innerHTML);

});


</script>



tistory424_01.html



반응형

리스트에서 아이템 지우기

2012. 10. 25. 03:33 | Posted by 솔웅


반응형

Delete an item from a list


리스트에서 item 을 delete 하는 것은 jQuery 의 remove () method를 사용해서 구현할 수 있습니다. 아래에 해당 아이템을 클릭하면 그 아이템이 delete 되는 소스를 만들어 보겠습니다.


Allow the removal of an element inserted


<!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>

    <ol id=list1 data-role=listview>

    </ol>

    <br />

    <a data-role=button id=add>Insert at the end of the list</a>

  </div>

</div>


</body>

</html>


<script>


$("#add").bind ("click", function (event)

{

  $("#list1").append ("<li>Inserted element</li>");

  $("#list1").listview ("refresh");

});


$("li").live ("vclick", function (event)

{

  $(this).remove ();

});


</script>




tistory423_01.html


live () method 로 <li> items 에 대해 이벤트 핸들러를 걸었습니다.  <li> item 을 클릭(vclick) 하게 되면 그 아이템을 remove() 할 겁니다.



link의 click event 와 <li> element 를 잘 보세요. 이것은 jQuery Mobile에서 추천하는 방식입니다.


반응형


반응형

인크로스사에서 11월 12일 jQuery Conference 2012 Asia 라는 행사를 진행한답니다.

공식 홈페이지도 있는데요.

http://www.jqueryasia.com/


위 링크 따라 가시면 자세한 사항을 보실 수 있을 겁니다.


참가비가 있는 행사네요. 35만원이요.


좀 부담이 되는 돈일 수 있겠네요.


회사에서 보내 주실 수 있는 분들은 좋은 기회일 겁니다.

개인적으로라도 저 금액을 지불하고 참가하고 싶으신 분들도 계실 수 있겠죠.


인크로스 사에서 홍보 부탁 메일이 와서 아래 그 내용을 포스팅할께요.

관심있는 분들의 참여 바랍니다.








반응형

리스트에 아이템 insert 하기

2012. 10. 24. 05:12 | Posted by 솔웅


반응형
Insert an item in a list



jQuery method들은 리스트에 intem 을 insert 하는 일들을 자주 합니다. 예를 들어 HTML 코드에 의해 정의된 element 들을 insert 하기 위해 append (html) method를 쓰는 경우가 있습니다. 이 메소드를 사용해서 <ul><ol> element 의 끝에 추가 하게 되는 거죠.


Here we define a <ol> list currently empty. A button Insert at the end of the list allows to insert an element <li> at the end of the <ol> list.

아래 예제에는 빈 <ol> list가 정의 돼 있습니다.  Insert at the end of the list 버튼을 누르면<ol> list 끝에 element <li>를 insert 하겠습니다.



Insert an item at the end of the list


<!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>

    <ol id=list1 data-role=listview>

    </ol>

    <br />

    <a data-role=button id=add>Insert at the end of the list</a>

  </div>

</div>


</body>

</html>


<script>


$("#add").bind ("click", function (event)

{

  $("#list1").append ("<li>Inserted element</li>");

});


</script>



이걸 실행하고 몇번 insert 하면 아래 모양처럼 될 겁니다.




<li> elements가 insert 됐죠. 그런데 이게 jQuery Mobile 스러운 리스트로 표시되지 않네요. 왜냐하면 jQuery Mobile 은 insert 할 때마다 refresh 를 하지 않기 때문이죠. <ul> or <ol> element 상에서 listview ("refresh") method 를 call 해야 됩니다.


아래 구문을 리스트 아이템 insert 한 후에 넣어 보세요. <li> element 가 insert 될 때 자바스크립트가 실행 될 겁니다.


Refresh the list after each element insertion


$("#add").bind ("click", function (event)

{

  $("#list1").append ("<li>Inserted element</li>");

  $("#list1").listview ("refresh");

});






tistory420_01.html



반응형

Ajax 로 리스트 Retrieve 하기

2012. 10. 23. 21:06 | Posted by 솔웅


반응형
Retrieve a list by Ajax


Ajax에 의해 처리된 리스트를 만들겁니다. 이 리스트는 window content에 마지막 element 로서 추가 될 겁니다.


Retrieve a list by Ajax and insert into the window


<!DOCTYPE html>

<html> 

<head> 

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

  <meta name="apple-mobile-web-app-capable" content="yes" />

  <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);

    $("#list").listview ();

  }

}); 


</script>



action.php file 에는 window 에 넣을 list를 return 하는 server program 이 있습니다.


action.php file



<?
$html = "";
$html .= "<ol id=list data-inset=true>";
$html .=   "<li data-role=list-divider>List retrieved by Ajax</li>";
$html .=   "<li data-icon=delete>";
$html .=      "<a href=#>Element 1</a>";
$html .=   "</li>";
$html .=   "<li data-icon=delete>";
$html .=      "<a href=#>Element 2</a>";
$html .=   "</li>";
$html .=   "<li data-icon=delete>";
$html .=      "<a href=#>Element 3</a>";
$html .=   "</li>";
$html .= "</ol>";
  
echo utf8_encode ($html);
?>



자바스크립트 코드 안에 있는 $("#list").listview () statement를 보세요. 여기서 서버로부터 HTML 코드를 받아 이것을 jQuery Mobile 의 list 로 display 하는 겁니다. 이게 없으면 이 리스트는 jQuery Mobile 의 list를 보여주지 않고 그냥 단순히 HTML list 를 보여주게 됩니다.


이 예제의 결과는 아래 화면과 같을 겁니다.




action419.php


tistory419_01.html



Window에 list 를 생성하기 위해 create event 를 사용하실 수도 있습니다.


Create the list using listview ()


$("#list").listview ();


위 코드 부분 아래에 아래 코드를 추가하세요.


Create the list using the create event


$("#home").trigger ("create");


만약 create event를 사용하신다면 이 리스트는 서버로부터 반드시 data-role="listview" attribute 를 retrieve 할 겁니다. 그렇지 않으면 jQuery Mobile은 이 HTML element를 jQuery Mobile 리스트로 전환해야 된다는 것을 알지 못합니다.  listview () method 를 사용하시면 이 attribute는 필요가 없게 되는 거죠. 이 메소드가 call 되면 jQuery Mobile list로 transform 해야 된 다는 것을 알려 주는 겁니다. (여기서는 #list1 element 임)




반응형


반응형





Turning a HTML element into a jQuery Mobile list



이번에 할 작업은 일반적인 HTML에서의 리스트 표시 방법인 <ul> or <ol>를 jQuery Mobile 식으로 display 하도록 바꾸는 작업입니다. <ul> or <ol> element 가 data-role="listview" attribute 를 사용해서 리스트를 표시할 수 있는 것은 이미 알고 계시죠? 우리는 이 attribute를 어떻게 <ul> or <ol> element 에 이 attribute를 적용하는지에 대해 알아 볼겁니다.


아래 예제를 보실텐데요 일단 고전적인 attribute들인 data-role, data-icon, etc 을 모두 제거한 코드를 먼저 보시겠습니다.


A HTML list with no jQuery Mobile conventions


<!DOCTYPE html>

<html> 

<head> 

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

  <meta name="apple-mobile-web-app-capable" content="yes" />

  <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>

    <ol id=list1>

      <li>Element 1.1</li>

      <li>Element 1.2</li>

      <li>Element 1.3</li>

    </ol>

  </div>

</div>


</body>

</html>


<script>


</script>



이 리스트는 전형적인 jQuery Mobile window를 포함하고 있습니다. 그런데 <ol> element 가 data-role="listview" attribute를 가지고 있지 않습니다.  이 리스트는 아주 기본적인 형식만 갖췄습니다.




tistory418_01.html


이제 자바스크립트로 jQuery Mobile convention을 따라서 display 하도록 무너가 작업을 해야될 것 같군요. 이를 위해서 listview jQuery Mobile component 에 있는 listview () method를 사용해서 HTML 리스트를 jQuery Mobile 리스트로 표시할 겁니다.

Add this statement in our JavaScript code:


Call to the listview () method on the list


<!DOCTYPE html>

<html> 

<head> 

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

  <meta name="apple-mobile-web-app-capable" content="yes" />

  <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>

    <ol id=list1>

      <li>Element 1.1</li>

      <li>Element 1.2</li>

      <li>Element 1.3</li>

    </ol>

  </div>

</div>


</body>

</html>


<script>


$("#list1").listview ();


</script>





tistory418_02.html



그런데 기대했던 모습은 아니네요. 우리는 이 window 가 생성될 때 그냥 거기서 jQuery Mobile component (list) 를 display 하라고 시켰습니다. 이러지 말고 이 window 가 그 transformation을 수행하기 위한 과정에서 create 될 때 까지 좀 더 기다려 줘야 할 것 같습니다.


그 윈도우의 pagecreate event 에서 listview () instruction을 집어 넣어 보겠습니다. 그러면 이 윈도우가 create 되는 것을 끝마칠 때까지 기다릴 겁니다.


Call to the listview () method in the treatment of the pagecreate event


<!DOCTYPE html>

<html> 

<head> 

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

  <meta name="apple-mobile-web-app-capable" content="yes" />

  <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>

    <ol id=list1>

      <li>Element 1.1</li>

      <li>Element 1.2</li>

      <li>Element 1.3</li>

    </ol>

  </div>

</div>


</body>

</html>


<script>


$("#home").bind ("pagecreate", function ()

{

  $("#list1").listview ();

});


</script>





tistory418_03.html



이제 기대했던 대로 나왔네요.....





반응형


반응형

jQuery Mobile에 의해서 display 되는 리스트 만들기는 jQuery 에 그 기본을 둡니다. jQuery Mobile 은 그것을 사용하기 위해 listview () method를 implement합니다. 이 리스트들은 listview standard component와 연계가 있습니다.



Dynamically create a list



리스트는 HTML 코드 안에서 생성할 수 있습니다. jQuery Mobile 은 이 리스트 아이템들을 좀 더 예쁘게 보이도록 새로운 CSS 클래스를 적용해서 HTML을 convert 할 수 있습니다.


그리고 새로운 리스트를 jQuery 의 메소드를 사용해서  dynamic 하게 생성하는 것도 가능합니다.


List without images


아래 예제에서는 페이지에 HTML 코드로 list 를 만들고 두번쨰로는 JavaScript code로 dynamic 하게 list를 생성했습니다. 마지막으로 이 두 리스트의 view를 일관되게 표현했습니다.


Dynamic creation of a list


<!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>

    <ol id=list1 data-role=listview data-inset=true>

      <li data-role=list-divider>Static list</li>

      <li data-icon=delete>

        <a href=#>Element 1.1</a>

      </li>

      <li data-icon=delete>

        <a href=#>Element 1.2</a>

      </li>

      <li data-icon=delete>

        <a href=#>Element 1.3</a>

      </li>

    </ol>

  </div>

</div>


</body>

</html>


<script>


var html = "";

html += "<ol id=list2 data-role=listview data-inset=true>";

html +=   "<li data-role=list-divider>Dynamic list</li>";

html +=   "<li data-icon=delete>";

html +=      "<a href=#>Element 2.1</a>";

html +=   "</li>";

html +=   "<li data-icon=delete>";

html +=      "<a href=#>Element 2.2</a>";

html +=   "</li>";

html +=   "<li data-icon=delete>";

html +=      "<a href=#>Element 2.3</a>";

html +=   "</li>";

html += "</ol>";


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


</script>



첫번째 리스트는 전통적인 방법으로 HTML을 사용해서 생성했고 (static list) 두번째는 JavaScript 로 자동으로 생성해서 append (html) instruction을 사용해서 DOM tree에 insert 했습니다(dynamic list).




tistory417_01.html


List with images


이번에는 위와 같은 예제인데요 다른 점은 리스트 아이템 안에 이미지가 들어가는 겁니다. static list 는 두개의 리스트 아이템이 있고 dynamic list에는 한개의 아이템이 있습니다.


Dynamic creation of a list with images


<!DOCTYPE html>

<html> 

<head> 

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

  <meta name="apple-mobile-web-app-capable" content="yes" />

  <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>

    <ul id=list1 data-role=listview data-inset=true>

      <li data-role=list-divider>Static list</li>

      <li>

        <img src=images/html.jpg />

        <h1> HTML & CSS</h1>

        <p> Eric Sarrion</p>

      </li>

      <li>

        <img src="images/j2ee.jpg" />

        <h3>J2EE</h3>

        <p> Eric Sarrion</p>

      </li>

    </ul>

  </div>

</div>


</body>

</html>


<script>


var html = "";

html += "<ul id=list2 data-role=listview data-inset=true>";

html +=   "<li data-role=list-divider>Dynamic list</li>";

html +=   "<li>";

html +=     "<img src=images/jquery.jpg />";

html +=     "<h3>JQuery & jQuery UI</h3>";

html +=     "<p> Eric Sarrion</p>";

html +=   "</li>";

html += "</ul>";


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


</script>





tistory417_02.zip




반응형