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

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함

카테고리

버튼 Customize 하기

2012. 10. 31. 04:30 | Posted by 솔웅


반응형
Customize buttons



General appearance of the button



이전 글에서 만들었던 소스를 firefox 의 firebug 를 통해서 jQuery 에 의해 생성된 HTML 소스를 보겠습니다.



<a> link 는 이제 ui-btn CSS class 가 됐죠? 그리고 안에 ui-btn-inner CSS class가 있는 <span> element 도 포함하고 있습니다. 그 안에는 ui-btn-text class 를 가지고 있는 <span> element가 있습니다.


이제 버튼의 style을 원하는 대로 바꿀 수 있겠죠? ui-btn-text CSS class 를 따로 override 해서 정리하면 버튼 스타일을 customize 할 수 있습니다.


Changing the style of the 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>

  

  <style type=text/css>

    .ui-btn-inner {

      padding : 25px;

      background-color : black;

    }

    .ui-btn-text {

      font-size : 25px;

      color : white;

    }

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

    <a id=btn data-role=button href=#> Click here </a>

  </div>

</div>


</body>

</html>


<script>


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

{

  alert ("click");

});


</script>



tistory434_01.html


이렇게 하면 색다른 모양의 버튼이 보일 겁니다.





Aspect of the clicked button


버튼이 눌렸을 때 jQuery Mobile은 ui-btn-down-c CSS class를 assign 합니다. 클래스 이름 끝의 "c"는 default theme button 을 말하는 겁니다. data-theme attribute 에 의해 modify 될 수 있습니다.  이 클래스도 따로 CSS 클래스로 implement 할 수 있습니다.


Adding the CSS class ui-btn-down-c in the styles of buttons


<style type=text/css>

 .ui-btn-inner {

    padding : 25px;

    background-color : black;

  }

  .ui-btn-text {

    font-size : 25px;

    color : white;

  }

  .ui-btn-down-c {

    font-style : italic;

  }

</style>

tistory434_02.html


위 코드를 적용하면 버튼이 클릭될 때 글자가 이탤릭체로 될 겁니다.


특정 CSS property를 사용할 때 주의하실 부분이 있습니다. 예를 들어 ui-btn-down-c class 정의 부분에 color property를 다시 사용하신다면 child element (the <span> with the ui-btn-text CSS class) 안에 재 정의 된 것으로 적용 되지 않을 겁니다. 제대로 처리 되게 하려면 아래와 같이 하셔야 합니다.


Changing the text color when the button is clicked


<style type=text/css>

  .ui-btn-inner {

    padding : 25px;

    background-color : black;

  }

  .ui-btn-text {

    font-size : 25px;

    color : white;

  }

  .ui-btn-down-c {

    font-style : italic;

  }

  .ui-btn-down-c .ui-btn-text {

    color : red;

  }

</style>


tistory434_03.html


이렇게 하면 버튼을 누를 때 글자가 이탤릭 체로 되면서 글자 색도 빨간색으로 바뀌도록 할 수 있습니다.






반응형


반응형
Manage events on buttons


리스트와 마찬가지로 버튼에서 발생할 수 있는 main event 는 click event 입니다. 이 이벤트는 jQuery 의 bind () method 의해 클릭 됐을 때 어떤 동작을 하도록 처리 됩니다.


Process the click on the 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>  

    <a id=btn data-role=button href=#> Click here </a>

  </div>

</div>


</body>

</html>


<script>


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

{

  alert ("click");

});


</script>



tistory433_01.html



jQuery Mobile은 여기서 vclick event  가 아니라 click event 를 사용할 것을 권장합니다.


반응형

Ajax 로 버튼 insert 하기

2012. 10. 30. 03:41 | Posted by 솔웅


반응형
Insert buttons by Ajax

이제 Ajax를 통해서 HTML element를 버튼으로 만들어서 화면에 그 버튼을 표시하려고 합니다. 첫번째 버튼은 링크 처럼 <a> element 이고 두번째 버튼은 type="submit" attribute 를 가지고 있는 <input> button 이 될 것입니다.


Insert buttons in a window via 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);

    $("#btn").button ();

    $("input[type=submit]").button ();

  }

});    


</script>



서버에 있는 action.php file 이 이 두개의 버튼을 만들 HTML 코드를 return 할 겁니다.


action.php file


<?
$html = "";
$html .= "<a id=btn href=#>Menu 1</a>";
$html .= "<input type=submit value=Submit />";
  
echo utf8_encode ($html);

?>


action3.php


tistory431_01.html



서버로부터 소스를 받을 시간동안 약간 기다리면 두개의 버튼이 화면에 뜨시는 것을 보실 겁니다.

자바스크립트 부분을 잘 보세요. button () method를 사용했습니다. 이 메소드는 서버로부터 받은 두개의 HTML element들을 jQuery Mobile 에 의해 standard button들로 화면에 뿌려주도록 합니다. 만약 소스가 잘 못됐던가 해서 이 과정이 실패하면 화면에는 링크와 일반적인 HTML 버튼이 뿌려질겁니다.




다른 jQuery Mobile component들과 같이 component를 생성하는 standard method 는 (button () method 같은) 그 버튼을 포함한 element에서trigger ("create") method를 call 함으로서 구현할 수도 있습니다. 아래에 예제를 보시겠습니다.


Use the create event to create the 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>


$.ajax (

  url : "action.php", 

  complete : function (xhr, result)

  {

    if (result != "success") return;

    var response = xhr.responseText;

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

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

  }

});


</script>



하나의 create event로 두개의 버튼을 생성할 수 있습니다. 여기서 첫번째 버튼에다가 이것을 button component로 바꿀 거라는 것을 알리기 위해 data-role="button" attribute를 추가 해야 합니다 두번째 버튼에는 <input> element 이기 때문에 필요없습니다.


action.php file


<?
$html = "";
$html .= "<a id=btn data-role=button href=#>Menu 1</a>";
$html .= "<input type=submit value=Submit />";
  
echo utf8_encode ($html);
?>

action4.php


tistory432_02.html




반응형


반응형

Turning a HTML element into a jQuery Mobile button


For an element to be transformed into a button, use the button () method on the HTML element.

Using the button () 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 += "<a id=btn href=#>";

html +=     "Click the button";

html += "</a>";

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


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

  $("#btn").button ();

});


</script>


Note that the window is expected to be created to transform the link into button (pagecreate event). However, in the case of buttons, it is not necessary, unlike the lists that we have previously studied. But for the sake of thoroughness, we try to write similar code in both cases ...





tistory430_02.html



반응형


반응형

버튼을 다루는 것도 리스트를 다루는 것하고 비슷합니다. 여기서는 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에서 추천하는 방식입니다.


반응형

리스트에 아이템 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



반응형