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

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함

카테고리


반응형
Turning a HTML element into a jQuery Mobile input field


Input on one line


single line input field 를 생성할 때 jQuery Mobile 은 너비와 높이 그리고 shaded border 등을 정할 수 있는 CSS classes 들을 해당 <input> element 에 add 합니다.  이전 글에서 사용했던 소스를 가지고 한번 확인해 보죠. 이전 소스를 fireworks 로 열어서 firebug로 jQuery Mobile 에 의해 생성된 HTML code를 확인해 보세요.




<input> element에 ui-input-text CSS class 가  보이시죠? 다른 클래스들은 border 의 테두리 곡선을 관리하는 클래스들입니다.


Input on multiple lines


jQuery Mobile 에 의해 생성된 HTML 을 잘 보세요. 아래는 multiline input field 소스코드를 firebugs 에서 본 모습입니다.




여기서도 ui-input-text class 를 보실 수 있습니다. 다만 이번에는 <textarea> element에 적용됐을 뿐이죠.


Search field


같은 방법으로 search field 소스코드를 보겠습니다.




jQuery Mobile 에 의해서 바뀌어진 HTML 은 좀 더 복잡하게 변했죠? <div> element 에 ui-input-search class 가 있고 거기에 <input> element가 포함돼 있구요.  여기에 또 <a> link도 있습니다. field 의 오른쪽에 있는 erase icon 과 관계 있는 버튼을 만들기 위해서죠.

또한 <input> element 에 type="true" attribute (instead of type="search") 가 있는 점을 잘 봐두세요.  그리고 새 data-type attribute 가 "search"로 세팅돼 있습니다.




반응형


반응형

input fields는 single line 과 multiline 두가지 타입이 있습니다. 첫번째것은  <input> elements 에 type="text" attribute 가 들어가고 두번째 것은 <textarea> elements가 들어갑니다. jQuery Mobile 에는 이 외에 search field가 있습니다. 어떤 값을 입력하거나 어떤 문자를 찾을 때 사용하죠. 이 세가지의 input field 들은 jQuery 의 standard methods 에 의해 관리 됩니다. 그리고 jQuery Mobile에는 textinput () method가 추가로 있습니다. input field 는 textinput standard component 와 연관이 있습니다.


Dynamically create an input field


Input on one line


JavaScript 로 single line 의 input field를 다이나믹하게 생성하겠습니다. 간단하게 type="text" attribute를 가지고 있는 <input> element를 생성하면 됩니다.


Create an input field dynamically


<!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 += "Name : <input type=text value='Sarrion' />";

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


</script>


tistory451_01.html




Input on multiple lines


multiline text field 를 표시할 수 있는 <textarea> element 를 생성해 보죠.


Create a multiline text field dynamically


<!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 += "Description : <textarea> Beautiful sunny studio. Reasonable price.</textarea>";

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


</script>


tistory451_02.html





Search field


search field 는 <input> element 안에 type="search" attribute를 넣으시면 됩니다.


Dynamic creation of a search field


<!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 += "Name : <input type=search value='Sarrion' />";

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


</script>


tistory451_03.html





반응형

테이블 manipulation 예제

2012. 11. 6. 22:18 | Posted by 솔웅


반응형
Example of table manipulation


A main menu as images in a table


이제 테이블에 여러 이미지를 넣고 그 이미지를 클릭하면 Text 가 나오고 그 Text를 클릭하면 해당 홈페이지로 가게 할 겁니다.

(아래 첨부파일은 본문의 예제소스와 약간 다릅니다. 첨부파일도 꼭 확인해 주세요.)


Main menu as images


<!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-grid-b img {

      width : 80px;

      margin : 5px;

    }

  </style>

</head> 


<body> 


<div data-role=page id=home>

  <div data-role=header>

    <h1>Home</h1>

  </div>


  <div data-role=content>

    <h3> A main menu as images </h3>

    <div class=ui-grid-b>

      <div class=ui-block-a>

        <a href=#jquery><img src=images/jquery.jpg /></a>

      </div>

      <div class=ui-block-b>

        <a href=#html><img src=images/html.jpg /></a>

      </div>

      <div class=ui-block-c>

        <a href=#j2ee><img src=images/j2ee.jpg /></a>

      </div>

      <div class=ui-block-a>

        <a href=#rails><img src=images/rails.jpg /></a>

      </div>

      <div class=ui-block-b>

        <a href=#mobile_web><img src=images/mobile_web.jpg /></a>

      </div>

      <div class=ui-block-c>

        <a href=#phonegap><img src=images/phonegap.jpg /></a>

      </div>

    </div>

  </div>

</div>


<div data-role=page id=jquery data-add-back-btn=true>

  <div data-role=header>

    <h1>jQuery</h1>

  </div>


  <div data-role=content>

    <h3> jQuery content</h3>

  </div>

</div>


<div data-role=page id=html data-add-back-btn=true>

  <div data-role=header>

    <h1>HTML & CSS</h1>

  </div>


  <div data-role=content>

    <h3> HTML & CSS content</h3>

  </div>

</div>


<div data-role=page id=j2ee data-add-back-btn=true>

  <div data-role=header>

    <h1>J2EE</h1>

  </div>


  <div data-role=content>

    <h3> J2EE content </h3>

  </div>

</div>


<div data-role=page id=rails data-add-back-btn=true>

  <div data-role=header>

    <h1>Ruby on Rails</h1>

  </div>


  <div data-role=content>

    <h3> Ruby on Rails content </h3>

  </div>

</div>


<div data-role=page id=mobile_web data-add-back-btn=true>

  <div data-role=header>

    <h1>Mobile web</h1>

  </div>


  <div data-role=content>

    <h3> Mobile web content </h3>

  </div>

</div>


<div data-role=page id=phonegap data-add-back-btn=true>

  <div data-role=header>

    <h1>PhoneGap</h1>

  </div>


  <div data-role=content>

    <h3> PhoneGap content </h3>

  </div>

</div>


</body>

</html>


tistory448_01.html



이미지는 수평으로 나란히 정렬 되도록 만들었습니다. 그리고 각각의 이미지에는 <a> link가 있어서 해당 이미지에 맞는 text를 다른 window 에서 display 되도록 만들었습니다. (그리고 저의 예제 파일을 열어보시면 그 텍스트에 링크를 걸어서 해당 홈페이지에 가도록 만들었습니다.)


아래는 위 예제소스의 실행화면입니다.
(제 첨부파일에는 구글, 야후, 다음, 한겨레, 경향신만, Ohmynews 등을 예제로 들었습니다. 일부러 네이버는 뺐는데요. 그건 순전히 저의 정치성향때문입니다. ^^; 네이버가 이명박과 박근혜 쪽에 기운것 같아서.... ;;)




J2EE를 클릭하면 아래 화면이 나옵니다.




반응형

테이블에서 이벤트 다루기

2012. 11. 6. 21:47 | Posted by 솔웅


반응형
Manage events on tables


테이블의 각 cell 에서 처리될 수 있는 주요 이벤트는 click event 입니다. jQuery Mobile에서는 vclick virtual event 로도 사용할 수 있죠. tapholdswipe events 들도 사용될 수 있습니다.


예를 들어 cell 테이블에 long click  (taphold) 이 일어났을 때 어떤 동작을 하도록 만들 수 있습니다. 아래 예제는 long click 이 일어났을 때 그 cell 의 content를 delete 하는 예제입니다.


Deleting array elements by clicking and holding


<!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 class=ui-grid-b>

      <div class=ui-block-a>Element 1.1</div>

      <div class=ui-block-b>Element 1.2</div>

      <div class=ui-block-c>Element 1.3</div>

      <div class=ui-block-a>Element 2.1</div>

      <div class=ui-block-b>Element 2.2</div>

      <div class=ui-block-c>Element 2.3</div>

      <div class=ui-block-a>Element 3.1</div>

      <div class=ui-block-b>Element 3.2</div>

      <div class=ui-block-c>Element 3.3</div>

    </div>

  </div>

</div>


</body>

</html>


<script>


$(".ui-block-a, .ui-block-b, .ui-block-c").bind ("taphold", function (event)

{

  $(this).html ("&nbsp;");

});


</script>


tistory446_01.html


테이블의 각 cell 에는 ui-block-a, ui-block-b, or ui-block-c CSS classes 들이 있습니다. 이 클래스들에 taphold event를 implement 했죠. 그러면 long click 이 감지되면 <div> 의 content 는 "&nbsp;"로 바뀝니다. 실제로 delete 되는건 아니죠 .하지만 사실상 delete 되는 것입니다. 해당 셀을 그냥 빈 공간으로 두는 것이 아니라 space 한칸을 두도록 만든 겁니다.





반응형


반응형
Dynamically insert a new row


jQuery Mobile 에서는 새로운 라인의 시작을 가리키기 위해 <div> element 에 ui-block-a CSS class 를 사용합니다. 이전에도 다뤘었구요.


Dynamic insertion of a new line in a table


<!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 id=table class=ui-grid-b>

      <div class=ui-block-a>Element 1.1</div>

      <div class=ui-block-b>Element 1.2</div>

      <div class=ui-block-c id=insert>Element 1.3</div>

      <div class=ui-block-a>Element 2.1</div>

      <div class=ui-block-b>Element 2.2</div>

      <div class=ui-block-c>Element 2.3</div>

    </div>

  </div>

</div>


</body>

</html>


<script>


var html = "<div class=ui-block-a> Element 1bis.1 </div>";

html += "<div class=ui-block-b> Element 1bis.2 </div>";

$("#insert").after (html);


</script>

tistory445_01.html






반응형


반응형
Dynamically insert a new column


처음에 테이블에는 두개의 row 와 세개의 column이 있습니다. 여기에 한개의 컬럼을 더 추가하기를 원합니다.


Inserting dynamic column in a table


<!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 id=table class=ui-grid-b>

      <div class=ui-block-a>Element 1.1</div>

      <div class=ui-block-b>Element 1.2</div>

      <div class=ui-block-c id=insert>Element 1.3</div>

      <div class=ui-block-a>Element 2.1</div>

      <div class=ui-block-b>Element 2.2</div>

      <div class=ui-block-c>Element 2.3</div>

    </div>

  </div>

</div>


</body>

</html>


<script>


$("#table").removeClass ("ui-grid-b").addClass ("ui-grid-c");

var html = "<div class=ui-block-d> Element 1.4 </div>";

$("#insert").after (html);


</script>



tistory444_01.html


페이지가 시작할 때 CSS 클래스를 ui-grid-b에서 ui-grid-c로 바꾸도록 해 새로운 컬럼을 가지고 있는 테이블이 되도록 했습니다. 그 다음에 after () method 를 사용해서 insert identifier 에 의해 위치를 정해준 다음 해당 element를 insert 하게 됩니다.


원래 테이블은 두개의 줄과 세개의 컬럼이 있었습니다.



그런데 script 가 한번 돌고 나면 첫번째 줄의 끝에 컬럼이 하나 더 추가됩니다.





반응형

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




반응형


반응형
Turning a HTML element into a jQuery Mobile table


A simple table


jQuery Mobile table  ui-grid-a, ..., ui-grid-d, and u-block-a, ..., ui-block-e 같이 specialized 된 CSS classes 들을 가지고 있는 <div> elements를 사용합니다. 이런 CSS 클래스들은 jQuery Mobile convention에 맞게 테이블을 스타일화 하는 역할을 합니다. 이전 글의 테이블을 firefox 의 firebug로 보면 아래와 같은 HTML code를 보실 수 있습니다.




아직 jQuery Mobile 에 의해서 modify 되지는 않은 상황입니다.



A table of buttons


테이블에 버튼을 추가해 보겠습니다. data-role="controlgroup"data-type="horizontal" attributes 들을 병행해서 사용하시면 됩니다.


Two buttons side by side in the two cells of a table


<!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 += "<div class=ui-grid-a data-role=controlgroup data-type=horizontal >";

html +=   "<div class=ui-block-a style=text-align:right>";

html +=     "<a href=# data-role=button style=width:140px> OK </a>";

html +=   "</div>";

html +=   "<div class=ui-block-b>";

html +=     "<a href=# data-role=button style=width:140px> Delete </a>";

html +=   "</div>";

html += "</div>";

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


</script>



tistory441_01.html



jQuery Mobile 에 의해 생성된 HTML 코드 입니다. firefox의 firebug로 본 화면입니다.



data-role="controlgroup" 이 있는 <div> 를  보시면 jQuery Mobile 에 의해 ui-controlgroupui-controgroup-horizontal CSS 클래스 두개가 추가 된 것을 보실 수 있을 겁니다. 그리고 ui-block-a and ui-block-b CSS classes 가 있는 두개의 <div> elements 들이 있구요. 여기에 버튼들이 삽입됩니다.

<a> link에 data-role="button" attribute 가 있기 때문에 button 으로 변환 됩니다.

결론적으로 테이블을 만들기 위해 jQuery Mobile 에 의해 만들어진 HTML 은 그리 많이 변환되지 않았습니다. 버튼 같은 특정 element 들만이 modify 됩니다. table structure 그 자체는 변환되지 않습니다.



A table of buttons (continued)


HTML에 data-role="controlgroup"data-role="button" attributes를 사용하지 않고 같은 결과를 얻어볼까요. 아래 샘플을 실행해 보세요.


Two buttons side by side in the two cells of a table


<!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 += "<div class=ui-grid-a data-type=horizontal >";

html +=   "<div class=ui-block-a style=text-align:right>";

html +=     "<a href=# style=width:140px> OK </a>";

html +=   "</div>";

html +=   "<div class=ui-block-b>";

html +=     "<a href=# style=width:140px> Delete </a>";

html +=   "</div>";

html += "</div>";

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


$("a").button ();

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


</script>



tistory441_02.html


해당 element들이 버튼으로 변환되고 같이 그룹화 되도록 하기 위해 button ()controlgroup () 메소드들을 사용했습니다.  


jQuery Mobile 에 의해 생성된 화면을 보면 뭔가 2% 부족한 것을 느끼실 겁니다.






두 버튼의 width 가 같지 않죠. HTML 에서는 140px로 지정돼 있는데 말이죠. 그 이유는 button () method 를 call 하면서 그 아이템에 assign 된 CSS 가 disable 됐기 때문입니다. 해결방법은 jQuery Mobile 이 버튼을 세팅하기 위해 사용한 CSS 클래스에 직접 스타일링 하는 방법이 있습니다. (ui-btn class)


Define this CSS class in our HTML page, in the head section of the page:


Definition of the ui-btn class to style the buttons in the page


<style type=text/css>

.ui-btn {

    color : red;

    width : 140px;

  }

</style>

tistory441_03.html


그러면 아래처럼 보일 겁니다.





이제 버튼은 같은 width 를 갖게 되고 또 글자색도 빨간색으로 display 됩니다.


반응형


반응형

테이블 안에서 데이터를 관리하는 것은 주로 jQuery 의 메소드를 사용해서 구현합니다.  jQuery Mobile에 의해 controlgroup () method 가 추가되서 테이블의 다양한 component들에 visual group 을 제공하게 됩니다.


Dynamically create a table

이제 window에 dynamic 하게 table을 생성하려고 합니다. 이 테이블은 두개의 row와 세개의 column을 가지게 될 겁니다. 이것을 구현하기 위해 ui-grid-b class 가 있는 <div> element 를 생성합니다. 그리고 그 <div> 안에 ui-block-a, ui-block-b and ui-block-c CSS classes를 가지고 있는  <div> elements 들을 넣습니다.


Dynamic creation of table (2 rows and 3 columns)


<!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 += "<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>";

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


</script>


tistory440_01.html





반응형

버튼 관련 예제 파일 들

2012. 10. 31. 05:46 | Posted by 솔웅


반응형
Examples of manipulation of buttons



Managing a two-state button (pressed / not pressed)


이전 예제에서 버튼이 클릭됐을 때 어떤 동작을 하도록 했었습니다. 이 버튼의 상태 (pressed / not pressed)를 관리하기 위해 <a> element에 ui-btn-active CSS class 를 사용할 수 있습니다.


Managing a two-state 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;

    }

    .ui-btn-active {

      font-style : italic;

    }

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

{

  $(this).toggleClass ("ui-btn-active");

});


</script>

tistory435_01.html



이 소스는 버튼이 클릭되면
ui-btn-active class 를 활성화/비활성화 하는 작업을 jQuery 의 toggleClass () method 를 사용해서 구현했습니다.


Dynamically change the text and the button icon


단순히 글자 모양을 바꾼느 것 보다 클릭함에 따라서 글자 자체와 버튼의 아이콘도 바꾸는 것을 한번 해 보죠.


버튼 ON, OFF 두가지 상태가 있고 버튼이 ON 인 상태에서는 아이콘이 오른쪽에 나타나고 OFF 인 상태에서는 아이콘이 없어지도록 하겠습니다.


Volume management ON or OFF


<!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=# data-icon=check 

       data-iconpos=right selected=true> Volume ON </a>

  </div>

</div>


</body>

</html>


<script>


$("#btn").attr ("select", "true");


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

{

  var selected = $(this).attr ("select");

  if (selected == "true")

  {

    $(this).attr ("select", "false");

    $(this).find (".ui-icon").hide ();

    $(this).find (".ui-btn-text").text ("Volume OFF");

  }

  else

  {

    $(this).attr ("select", "true");

    $(this).find (".ui-icon").show ();

    $(this).find (".ui-btn-text").text ("Volume ON");

  }

});


</script>

tistory435_02.html




버튼의 상태를 select attribute에 보관해 두고 있죠? (디폴트는 true) 입니다. 그래서 이 값이 true 이면 버튼은 ON 이고 false 이면 버튼은 OFF 가 됩니다. 아이콘은 ui-icon CSS class 에서 처리되구요 텍스트는 ui-btn-text class를 통해서 처리 됩니다.


처음 페이지를 열면 아래와 같을 겁니다.




버튼을 클릭하면 글자가 OFF 로 바뀝니다.





Display a dynamically Delete button on a list item


이제 구현할 기능은 버튼을 동적으로 생성하는 겁니다. 화면에 리스트가 있고 그 리스트 중의 한 아이템을 오른쪽으로 drag 하면 delete 버튼이 생깁니다. 그리고 그 delete 버튼을 누르면 해당 아이템이 지워지는 겁니다.

아이폰의 메일에 이와 비슷한 기능이 있습니다.


Delete button added dynamically


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

    .remove {

      position : absolute;

      right : 10px;

      top : 5px;

      font-size : 13px;

    }

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

{

  if ($(this).find (".remove").length) return;

  $(this).append ("<input type=button value=Delete class=remove />");

  $("input.remove").unbind ().bind ("click", function (event)

  {  

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

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

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

  });

});


</script>

tistory435_03.html




Delete button은 해당 리스트 아이템에 버튼이 없을 경우에만 생성됩니다. 그리고 이 버튼을 누르면 해당 아이템이 delete 됩니다.




여기서 Delete button이 생겨난 이후에 꼭 버튼만 클릭하는게 아니라 해당 아이템을 클릭하면 그 아이템이 지워지게 하면 재밌을 까요? 이를 위해서는 다음 예제를 참고하세요.


Hide the Delete button by clicking outside the button


버튼 밖을 클릭해서 해당 아이템이 지워지도록 하려면 아래 코드를 자바스크립트 안에 추가하세요.


Remove the Delete button when clicking outside the button


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

{

    $(this).find ("input.remove").remove ();

});



이 코드는 약간의 문제가 있습니다.


해결 방법은 리스트 아이템에 time-out 을 넣는 겁니다. 이렇게 되면 버튼을 클릭해도 제대로 작동할 겁니다.


Inclusion of a delay in the removal 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>

    .remove {

      position : absolute;

      right : 10px;

      top : 5px;

      font-size : 13px;

    }

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

{

  if ($(this).find (".remove").length) return;

  $(this).append ("<input type=button value=Delete class=remove />");

  $("input.remove").unbind ().bind ("click", function (event)

  {  

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

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

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

  });

});


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

{

  var $this = $(this);

  setTimeout (function ()

  {

    $this.find ("input.remove").remove ();

  }, 500);

});


</script>



tistory435_04.html


이 소스에서는 버튼을 클릭하는 시간을 0.5초 줬습니다. 이 시간 동안에는 리스트 아이템은 remove 되지 않을 겁니다. 그 다음에 버튼이 delete 되겠죠.







반응형