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

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함

카테고리


반응형
Manage events on checkboxes



jQuery Mobile 은 checkbox들의 change event 를 관찰하고 있습니다. checkbox는 두가지 state 를 가지고 있습니다. (checked or not) 이 이벤트는 사용자가 checkbox를 click 했을 때 나타납니다. click event 이벤트도 있습니다. 하지만 이 click event 이벤트는 아래에서 사용되지 않습니다.


Using the change event on checkboxes


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

    <span> Choose an apartment: </span>

    <div data-role=controlgroup>

      <label for=id1> 1 bedroom </label>

      <input type=checkbox id=id1 name=p1 />

      <label for=id2> 2 bedrooms </label>

      <input type=checkbox id=id2 name=p2 />

      <label for=id3> 3 bedrooms </label>

      <input type=checkbox id=id3 name=p3 />

      <label for=id4> 4 bedrooms </label>

      <input type=checkbox id=id4 name=p4 />

      <label for=id5> 5 bedrooms </label>

      <input type=checkbox id=id5 name=p5 />

    </div>

  </div>

</div>


</body>

</html>


<script>


$(":checkbox").bind ("change", function (event)

{

  alert ($(this).attr ("checked"));

});


</script>


tistory486_01.html





click event는 직접적으로 사용되지 않을까요?  jQuery Mobile 은 original HTML code를 transform 하는 것을 기억하세요. 그러니까 <input> element가 아니라  ui-checkbox CSS class가 있는 <div> element에서 click event를 receive 하게 됩니다. 그래서 <div> element에서 일어나는 클릭은 <input> element 에서 change event를 유발합니다. 그리고 이 change event를 사용하구요.


반응형


반응형
Insert and delete a checkbox in an existing list



checkbox 추가하고 제거하는데 jQuery 의 standard methods 를 사용합니다.


Insertion and removal of checkboxes


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

    <span> Choose an apartment: </span>

    <div data-role=controlgroup>

      <label for=id1> 1 bedroom </label>

      <input type=checkbox id=id1 name=p1 />

      <label for=id2> 2 bedrooms </label>

      <input type=checkbox id=id2 name=p2 />

      <label for=id3> 3 bedrooms </label>

      <input type=checkbox id=id3 name=p3 />

      <label for=id4> 4 bedrooms </label>

      <input type=checkbox id=id4 name=p4 />

      <label for=id5> 5 bedrooms </label>

      <input type=checkbox id=id5 name=p5 />

    </div>

  </div>

</div>


</body>

</html>


<script>


var html = "";

html += "<label for=id0> Studio </label>";

html += "<input type=checkbox id=id0 name=p0 />";


$("input#id1").after (html);

$("input#id1, label[for=id1]").remove ();


</script>






tistory485_01.html



위와 같이 만들었는데요. 이게 항상 제대로 작동한다는 보장은 없습니다. 여기서는 제대로 작동될 겁니다. 왜냐하면 여기서는 check box가 추가되고 제거될 떄 jQuery Mobile 에 의해서 HTML이 변환되지 않았으니까요. HTML 이 jQuery Mobile 에 의해 변환 될 때가 있을 겁니다. 이럴 때는 다른 방법을 사용해야 이 방법을 구현할 수 있습니다.


Insertion and removal of checkboxes in a window already created


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

    <span> Choose an apartment: </span>

    <div data-role=controlgroup>

      <label for=id1> 1 bedroom </label>

      <input type=checkbox id=id1 name=p1 />

      <label for=id2> 2 bedrooms </label>

      <input type=checkbox id=id2 name=p2 />

      <label for=id3> 3 bedrooms </label>

      <input type=checkbox id=id3 name=p3 />

      <label for=id4> 4 bedrooms </label>

      <input type=checkbox id=id4 name=p4 />

      <label for=id5> 5 bedrooms </label>

      <input type=checkbox id=id5 name=p5 />

    </div>

  </div>

</div>


</body>

</html>


<script>


$("#id1").bind ("checkboxradiocreate", function (event)

{

  var html = "";

  html += "<label for=id0> Studio </label>";

  html += "<input type=checkbox id=id0 name=p0 />";

  

  $(this).parent ("div.ui-checkbox").after (html);   // insertion

  $(this).parent ("div.ui-checkbox").remove ();      // suppression

  $("#id0").checkboxradio ();

  $("div:jqmData(role=controlgroup)").controlgroup ();

});


</script>


tistory485_02.html



첫번째 체크박스를 만들 때 우리는 checkboxradiocreate event 에 의해 생성되는 것을 기다려야 합니다. jQuery Mobile 에 의해 HTML 코드가 이미 변환되었고 이 체크박스는 이제 ui-checkbox CSS class 가 있는 <div> element 로 그룹화  됩니다. 체크박스를 삽입할 때는 삽입되는 엘리먼트에 checkboxradio () method 를 사용해야 합니다. 그렇게 해서 HTML 로 convert 해야 되는거죠. 또한 삽입되는 엘리먼트는 모든 체크박스들이 그룹화시키는 <div> element 와 맞는 structure 를 가져야 합니다. 그래서 <div> element 에 controlgroup () method 를 사용하는 겁니다.


새 체크박스를 생성하기위해 controlgroup () method를 사용할 수도 있습니다. 그리고 refresh 하시면 됩니다.

그렇게 하기 위해 아래 코드 대신


Using standard methods on the components (first way)


$("#id0").checkboxradio ();
$("div:jqmData(role=controlgroup)").controlgroup ();



아래 코드를 사용하시면 됩니다.


Use the create event on the window (2nd way)


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


반응형


반응형
Assign and retrieve the value of a checkbox



Checkbox already present in the HTML


checkbox를 select 하거나 deselect 하는 것을 관리하기 위해 checkbox 가 있는 <input> element 에 checked attribute 를 사용했습니다.

  • <input> element에서 사용하는 attr ("checked", "checked") method 는 checkbox를 select 하는데 사용됩니다. attr ("checked", "") method는 checkbox를 deselect 하는데 사용합니다.

  •  attr ("checked") method는 checked attribute 의 값을 retrieve 하는데 사용됩니다. return 값은 jQuery 의 버전에 따라 다른데요. jQuery 1.5에서는 box가 체크돼 있으면 값이 true 될겁니다. 체크돼 있지 않으면 false 가 되겠죠. jQuery 1.6 는 체크돼 있으면 값이 "checked"가 됩니다.  체크돼 있지 않으면 undefined가 되구요.

Check the "2 bedrooms" checkbox


$("input#id2").attr ("checked", "checked");


Uncheck the "2 bedrooms" checkbox


$("input#id2").attr ("checked", "");


Is the "2 bedrooms" checkbox checked?


alert ($("input#id2").attr ("checked")); 
   // true / "checked" (if checked), false / undefined (if unchecked)

 


아래 예제에서는 위의 예제들을 활용하는 방법을 보여줍니다.


Selecting and deselecting a checkbox


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

    <span> Choose an apartment: </span>

    <div data-role=controlgroup>

      <label for=id1> 1 bedroom </label>

      <input type=checkbox id=id1 name=p1 />

      <label for=id2> 2 bedrooms </label>

      <input type=checkbox id=id2 name=p2 />

      <label for=id3> 3 bedrooms </label>

      <input type=checkbox id=id3 name=p3 />

      <label for=id4> 4 bedrooms </label>

      <input type=checkbox id=id4 name=p4 />

      <label for=id5> 5 bedrooms </label>

      <input type=checkbox id=id5 name=p5 />

    </div>

  </div>

</div>


</body>

</html>


<script>


$("#id2").attr ("checked", "checked");

alert ($("#id2").attr ("checked"));     // Displays true or checked

alert ($("#id3").attr ("checked"));     // Displays false or undefined


</script>






tistory484_01.html



Checkbox dynamically created


attr ("checked") and attr ("checked", "checked" / "") methods 들은 체크박스에 값을 할당하거나 retrieve 할 때 사용됩니다. 체크박스를 refresh 할 때는 checkboxradio ("refresh") method 를 사용합니다.

jQuery 로 여러개의 체크박스를 생성한 경우를 가정해 봅시다.  checkboxradiocreate event 에서 2 bedrooms and 3 bedrooms 를 체크하고 싶습니다.


Check the "2 bedrooms" and "3 bedrooms" in 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>

    <span> Choose an apartment: </span>

  </div>

</div>


</body>

</html>


<script>


var html = "";

html += "<div data-role=controlgroup>";

html +=   "<label for=id1> 1 bedroom </label>";

html +=   "<input type=checkbox id=id1 name=p1 />";

html +=   "<label for=id2> 2 bedrooms </label>";

html +=   "<input type=checkbox id=id2 name=p2 />";

html +=   "<label for=id3> 3 bedrooms </label>";

html +=   "<input type=checkbox id=id3 name=p3 />";

html +=   "<label for=id4> 4 bedrooms </label>";

html +=   "<input type=checkbox id=id4 name=p4 />";

html +=   "<label for=id5> 5 bedrooms </label>";

html +=   "<input type=checkbox id=id5 name=p5 />";

html += "</div>";


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


$("input").bind ("checkboxradiocreate", function (event)

{

  if (this.id == "id2" || this.id == "id3") 

    $(this).attr ("checked", "checked").checkboxradio ("refresh");

});

</script>




tistory484_02.html




여기서는 checkboxradio ("refresh") method를 사용해야 합니다. 왜냐하면 checkbox는 생성되고 나서 affect 될 수 있기 때문이죠. (예를 들어 checkboxradiocreate event 같은). 만약 생성되기 전에 만들면 (이벤트 이전이 되겠죠) 그러면 이 메소드 call 은 소용없습니다. 그리고 Firebug에서 보시면 jQuery 에러를 만들어 냅니다. 왜냐하면 non-authorized context 안에서 이 메소드가 사용되기 때문이죠.




반응형

Ajax 로 checkbox 만들어 넣기

2012. 11. 21. 05:48 | Posted by 솔웅


반응형
Insert checkboxes by Ajax



이전에 했던 것 처럼 checkbox를 HTML 코드에 직접 생성해 넣지 않고 Ajax를 통해서 서버로부터 HTML 코드를 받아오는 방법이 있습니다.


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

    <span> Choose an apartment: </span>

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

    

    $("input").checkboxradio ();

    $("input").closest ("div:jqmData(role=controlgroup)").controlgroup ();

  }

});  


</script>



checkboxradio () controlgroup () methods 를 사용했습니다.
checkboxradio () method는 <input> and <label> HTML elements를 checkbox (ui-checkbox class)와 연관이 있는 <div>로 변환하는데 사용합니다.  감싸여진 <div> 에서 controlgroup () method를 call 하면 그 checkbox들을 그룹화 할 수 있습니다.



action.php file


<?
$html = "";
$html .= "<div data-role=controlgroup>";
$html .=   "<label for=id1> 1 bedroom </label>";
$html .=   "<input type=checkbox id=id1 name=p1 />";
$html .=   "<label for=id2> 2 bedrooms </label>";
$html .=   "<input type=checkbox id=id2 name=p2 />";
$html .=   "<label for=id3> 3 bedrooms </label>";
$html .=   "<input type=checkbox id=id3 name=p3 />";
$html .=   "<label for=id4> 4 bedrooms </label>";
$html .=   "<input type=checkbox id=id4 name=p4 />";
$html .=   "<label for=id5> 5 bedrooms </label>";
$html .=   "<input type=checkbox id=id5 name=p5 />";
$html .= "</div>";
echo utf8_encode ($html);
?>



action13.php

tistory479_01.html


checkboxradio () controlgroup () methods 를 calling 하면 create evnet 의 triggering 에 의해 replace 될 수 있다는 것을 잘 봐 두세요.  그래서 아래 코드 대신에


Using standard methods of the components (first way)


$("input").checkboxradio ();


$("input").closest ("div:jqmData(role=controlgroup)").controlgroup ();


아래 코드를 사용할 수 있습니다.


Using the create event on the window (2nd way)


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


The create event will be received by the document object in jQuery Mobile, and internal treatment of this event searches for HTML elements to transform into jQuery Mobile components. Thus triggering a single create event processes checkboxes and the group that includes them.


create event 는 jQuery Mobile안에 있는 document object 에 의해 receive 될 겁니다. 그리고 이 이벤트는 HTML element를 jQuery Mobile component로 변환하도록 합니다. 그러니까 single create event를 triggering 하면 checkboxes 와 the group 작업을 수행합니다.


반응형


반응형
Turning an HTML element into a jQuery Mobile checkbox




Firebug 에서 봐 보세요. 이전 글에서 checkbox 들이 포함되고 나서 jQuery Mobile 에 의해 생성된 HTML 코드입니다.




<div> element 에 ui-controgroup CSS class 가 있습니다. 그리고 각 checkbox들은 <div> 에  ui-checkbox class 를 가지고 있습니다. 모두 그룹화 돼 있습니다. 그 안에는 <span> elements 를 가지고 있는 <label> element 가 있구요. <input> element 는 checkbox 에 해당하는 겁니다.




반응형


반응형


Checkboxes jQuery Mobile의해 <input> element에 type="checkbox" attribute 가 삽입되어서 만들어 집니다. jQuery 의 standard 메소드들이 사용될 수 있고 jQuery Mobile 에 의해 추가된
checkboxradio () method도 사용될 수 있습니다.
checkboxes 는 checkboxradio standard component 와 연결돼 있습니다.



Dynamically create checkboxes


type attribute가 "checkbox"<input> element를 넣음으로서 checkbox는 생성되고 name attribute 는 필수요소 입니다. 이 element 는 반드시 체크박스의 text를 가지고 있는 <label> element와 연결되야 합니다.

<input> element 의 id attribute 와 연결된 for attribute 를 가지고 있어야 합니다.

같은 set 내에 checkboxes들을 그룹화 하려면  data-role="controlgroup" attribute 를 <div> element 에 넣으시면 됩니다.


Creating dynamic checkboxes


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

    <span> Choose an apartment: </span>

  </div>

</div>


</body>

</html>


<script>


var html = "";

html += "<div data-role=controlgroup>";

html +=   "<label for=id1> 1 bedroom </label>";

html +=   "<input type=checkbox id=id1 name=p1 />";

html +=   "<label for=id2> 2 bedrooms </label>";

html +=   "<input type=checkbox id=id2 name=p2 />";

html +=   "<label for=id3> 3 bedrooms </label>";

html +=   "<input type=checkbox id=id3 name=p3 />";

html +=   "<label for=id4> 4 bedrooms </label>";

html +=   "<input type=checkbox id=id4 name=p4 />";

html +=   "<label for=id5> 5 bedrooms </label>";

html +=   "<input type=checkbox id=id5 name=p5 />";

html += "</div>";


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


</script>






tistory477_01.html


반응형

selection list 관련 예제들

2012. 11. 18. 03:43 | Posted by 솔웅


반응형

Examples of manipulation of selection lists



Communicate the value of a selection list on the server by Ajax


이번에 만들 selection list 는 먼저 아파트 type 을 정하고 이것을 서버로 보내서 그에 대한 response 를 받는 로직입니다. 우리는 그 response 를 window 에 display 할 겁니다.


Transmit the type of apartment 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>

    <span> Choose an apartment type: </span>

    <select data-native-menu=false>

      <optgroup label=Basic>

        <option value=1> 1 bedroom </option>

        <option value=2> 2 bedrooms </option>

        <option value=3> 3 bedrooms </option>

      </optgroup>

      <optgroup label=Premium>

        <option value=4> 4 bedrooms </option>

        <option value=5> 5 bedrooms </option>

      </optgroup>

    </select>

  </div>

</div>


</body>

</html>


<script>


$("select").bind ("change", function (event)

{

  var type = $(this).val ();

  

  $.ajax (

  

    url : "action.php", 

    data : { type : type },

    complete : function (xhr, result)

    {

      if (result != "success") return;

      var response = xhr.responseText;

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

    }

  });   

});


</script>


서버에 아파트 type 을 보내기 위해 change event 를 사용합니다. 그러니까 selection list 에 어떤 change 가 일어날 때만 정보가 전달 될 겁니다. 서버로부터 받은 response 는 append (response) 사용해서 window에 삽입됩니다.


action.php file


<?
$type = $_REQUEST["type"];
echo utf8_encode ("<p> The type of apartment is $type </p>");
?>



action11.php


tistory476_01.html


몇번 아이템을 선택하고 나면 아래와 같은 화면이 될 겁니다.




Use a submit button to transmit information


서버의 response를 display 하기 위해서 Ajax 메카니즘을 사용하는 대신에 form 을 validate 하고 서버에서 return 된 선택된 리스트 아이템의 값을 가지고 있는 값을을 새 window 에 display 할 수도 있을 겁니다.


Pass the value of the selected item when clicking the OK 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>

    <form action=action.php>

      <span> Choose an apartment type: </span>

      <select data-native-menu=false name=type>

        <optgroup label=Basic>

          <option value=1> 1 bedroom </option>

          <option value=2> 2 bedrooms </option>

          <option value=3> 3 bedrooms </option>

        </optgroup>

        <optgroup label=Premium>

          <option value=4> 4 bedrooms </option>

          <option value=5> 5 bedrooms </option>

        </optgroup>

      </select>

      <input type=submit value=OK>

    </form>

  </div>

</div>


</body>

</html>


<script>


</script>





name attribute 를 <select> element 에 추가했습니다. 그래서 selected value 는 form validation으로 해서 전송 됩니다.


action.php file

<?
$type = $_REQUEST["type"];
?>

<!DOCTYPE html> 
<html> 
<head> 
  <meta http-equiv=Content-Type content=text/html;charset=iso-8859-1 />
</head> 

<body>
<div data-role=page id=win2 data-add-back-btn=true>
  <div data-role=header>
    <h1>Window 2</h1>
  </div>
  
  <div data-role=content>
    <p> Selected apartment : <?= $type ?> </p>
  </div>
</div>
</body>

</html>


action12.php

tistory477_01.html



Add a list item after a click of a button


이제 dynamically 삽입된 new element를 display 하겠습니다. 새 element 는 id 가 add인 버튼을 클릭하면 되도록 만들겠습니다.


Add an item to the selection list after clicking 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>

    <span> Choose an apartment type: </span>

    <select data-native-menu=false>

      <option value=1> 1 bedroom </option>

      <option value=2> 2 bedrooms </option>

      <option value=3> 3 bedrooms </option>

      <option value=4> 4 bedrooms </option>

      <option value=5> 5 bedrooms </option>

    </select>

    <a href=# data-role=button id=add>

        Add "6 bedrooms" to the list </a>

  </div>

</div>


</body>

</html>


<script>


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

{

  $("select").append ("<option value=6> 6 bedrooms </option>");

  $("select").selectmenu ("refresh");

});


</script>

tistory476_03.html

Make a treatment when clicking on any element of the selection list


change event는 selection list 의 어떤 값들이 바뀔 경우에만 적용이 됩니다. 유저가 이미 선택한 아이템을 선택한다면 이 change event 는 발생하지 않을 겁니다.


만약에 아이템을 click 했을 때를 기준으로 뭔가를 할 수 있다면 좋겠죠? 아래 예제에서는 클릭했을 때 그 리스트 아이템을 보여 줄겁니다. 이 경우에는 그 아이템이 현재 선택 된 아이템이라도 이벤트가 발생할 겁니다.


Display the content of the item clicked


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

    <span> Choose an apartment type: </span>

    <select data-native-menu=false>

      <optgroup label=Basic>

        <option value=1> 1 bedroom </option>

        <option value=2> 2 bedrooms </option>

        <option value=3> 3 bedrooms </option>

      </optgroup>

      <optgroup label=Premium>

        <option value=4> 4 bedrooms </option>

        <option value=5> 5 bedrooms </option>

      </optgroup>

    </select>

  </div>

</div>


</body>

</html>


<script>


$("select").bind ("selectmenucreate", function ()

{

  $(".ui-li:not(.ui-li-divider)").live ("vclick", function (event)

  {

    alert ($(this).find ("a").text ());

    event.stopPropagation ();

  });

});


</script>

tistory476_04.html


자바스크립트가 몇 줄 안되지만 좀 설명할 부분이 있습니다. 일단 리스트가 생성될 때까지 기다려야 합니다. (selectmenucreate event). 그렇지 않으면 HTML 코드는 jQuery Mobile 에 의해 전환되기 전이 됩니다. 그리고 jQuery Mobile 에 의해 생성된 <li> elements 들이 아직 DOM tree 에 있는 상태가 아니게 되는 거죠.


selector  "ui-li:not (.ui-li-divider)"는 리스트 아이템입니다. (separator 가 아니죠). bind () method 대신 live () method 를 사용하는 것은 jQuery Mobile 이 아이템이 select 됐을 때 온전하게 다시 display 하도록 하기 위해서 입니다. 만약 bind () click 이벤트는 오직 첫번째 selection 에서만 적용이 될 겁니다.


마지막 단계에서는 jQuery Mobile 에 의해 만들어진 <a> item 을 retrieving 함으로서 선택된 아이템의 내용을 display 하게 됩니다.
event.stopPropagation () instruction 은 click 이 여러번 인식되는 것을 방지합니다.


이제 우리가 4 bedrooms을 선택하면 alert 화면에 4 bedrooms라는 메세지가 display 됩니다.


(제 경우에 저 위의 예제는 잘 안되네요. 어떨 때 될 때도 있는데... 혹시 이 예제가 왜 잘 안되는지 아시는 분은 댓글로 좀 알려 주세요.)



선택된 아이템 내용을 retrieve 하는 것보다 그 value 를 다룰 수도 있습니다. 해당 <option> element 에 있는 value attribute 를 표시할 수도 있습니다.


Retrieve the value of the selected item (only if the list is closed)


alert ($("select").val ()); // Displays "4"



반응형

selectiion list customize 하기

2012. 11. 17. 22:15 | Posted by 솔웅


반응형
Customize selection lists


Firebug를 이용해서 jQuery Mobile에 의해 만들어진 selection list 내부의 HTML element들에 대해 assign 된 CSS 클래스들을 볼 수 있습니다. 여기서는 data-native-menu attribute가 "false"로 된 부분만 살펴보기로 하겠습니다. (native 가 아닌)

아래 샘플 코드가 있습니다. 이 코드는 엘리먼트의 그룹들을 구분해서 리스트를 display 합니다.



A selection list with groups of elements



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

    <span> Choose an apartment type: </span>

    <select data-native-menu=false>

      <optgroup label=Basic>

        <option value=1> 1 bedroom </option>

        <option value=2> 2 bedrooms </option>

        <option value=3> 3 bedrooms </option>

      </optgroup>

      <optgroup label=Premium>

        <option value=4> 4 bedrooms </option>

        <option value=5> 5 bedrooms </option>

      </optgroup>

    </select>

  </div>

</div>


</body>

</html>


tistory475_01.html


이 selection list 는 아래와 같이 display 될 겁니다.





Firebug에서 보시면 jQuery Mobile 에 의해 생성된 HTML code 를 보실 수 있습니다.





ui-selectmenu CSS class 가 있는 <div> element 안에는 ui-selectmenu-list class 가 있는 <ul> element 가 있습니다. 그 안에는 display 된 리스트 아이템들에 해당하는 <li> elements 들이 있구요. 각
<li> elements 들에는 ui-li and ui-btn classes 들이 있습니다. 그리고 그 리스트 안에 selected 아이템에는 ui-btn-active class 가 있구요.


<optgroup> element를 사용해서 jQuery MObile에 의해 삽입된 separator element 에는 ui-li, ui-li-divider, ui-btn and ui-bar-b classe 들이 있습니다.


이 CSS class들을 이용해서 selection list 를 새롭게 꾸며보겠습니다.


Styling elements of the selection 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>

  

  <style type=text/css>

     .ui-selectmenu {

       padding : 15px;

     }

     .ui-li-divider.ui-bar-b {

       color : red;

       background : black;

     }

     .ui-li:not(.ui-li-divider) {

       font-style : italic;

     }

     .ui-li.ui-btn-active {

       color : white;

       background : grey;

     }

  </style>

</head> 


<body> 


<div data-role=page id=home>

  <div data-role=header>

    <h1>Home</h1>

  </div>


  <div data-role=content>

    <span> Choose an apartment type: </span>

    <select data-native-menu=false>

      <optgroup label=Basic>

        <option value=1> 1 bedroom </option>

        <option value=2> 2 bedrooms </option>

        <option value=3> 3 bedrooms </option>

      </optgroup>

      <optgroup label=Premium>

        <option value=4> 4 bedrooms </option>

        <option value=5> 5 bedrooms </option>

      </optgroup>

    </select>

  </div>

</div>


</body>

</html>


tistory475_02.html





반응형


반응형
Manage events on selection lists




change event 는 리스트의 selected value 가 바뀌었을 때를 알려 줍니다. 이 이벤트는 jQuery의 bind () method 를 사용해서 구현하시면 됩니다.


Using the change event in the selection 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>

    <span> Choose an apartment type: </span>

    <select data-native-menu=false>

      <option value=1> 1 bedroom </option>

      <option value=2> 2 bedrooms </option>

      <option value=3> 3 bedrooms </option>

      <option value=4> 4 bedrooms </option>

      <option value=5> 5 bedrooms </option>

    </select>

  </div>

</div>


</body>

</html>


<script>


$("select").bind ("change", function (event)

{

  alert ($(this).val ());

});


</script>




tistory474_01.html


반응형


반응형
Insert and delete items in a selection list



리스트에서 아이템을 추가하거나 지우는 것은 jQuery 의 메소드를 사용해서 처리합니다. 예를 들어 append ()는 리스트의 끝에  아이템을 추가하고 remove () 는 그 메소드가 붙은 아이템을 제거합니다.


Adding and deleting items from the selection 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>

    <span> Choose an apartment type: </span>

    <select data-native-menu=false>

      <option value=1> 1 bedroom </option>

      <option value=2> 2 bedrooms </option>

      <option value=3> 3 bedrooms </option>

      <option value=4> 4 bedrooms </option>

      <option value=5> 5 bedrooms </option>

    </select>

  </div>

</div>


</body>

</html>


<script>


$("select").append ("<option value=6> 6 bedrooms </option>");

$("option[value=1]").remove ();


</script>

tistory471_01.html








리스트의 첫번째 아이템은 지워졌습니다.  그리고 6 bedrooms는 추가 됐죠.

어쨌든 이 작업은 항상 완벽하게 동작하지 않을 수도 있습니다. 왜냐하면 elements 가 추가되거나 삭제될 때 jQuery Mobile 에 리스트가 아직 생성되지 않았기 때문이죠. 그러니까 이것이 HTML code 로 전환될 때 리스트에 우리가 추가하고 삭제한 것이 적용되서 만들어지게 되는 것이죠. 만약 리스트가 create 되고 난 다음에 이 추가나 삭제를 했다면 여러분은 여기에 추가적으로 selectmenu ("refresh", true) method 를 사용해야 합니다. 여기서 true value 는 파라미터처럼 전달이 되는데요 리스트를 rebuild 하도록 하는 겁니다.


Insert or remove items after the list creation


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

    <span> Choose an apartment type: </span>

    <select data-native-menu=false>

      <option value=1> 1 bedroom </option>

      <option value=2> 2 bedrooms </option>

      <option value=3> 3 bedrooms </option>

      <option value=4> 4 bedrooms </option>

      <option value=5> 5 bedrooms </option>

    </select>

  </div>

</div>


</body>

</html>


<script>


$("select").bind ("selectmenucreate", function ()

{

  $("select").append ("<option value=6> 6 pièces </option>");

  $("option[value=1]").remove ();

  $("select").selectmenu ("refresh", true);    // true = forceRebuild

});


</script>

tistory471_02.html





반응형
이전 1 2 3 4 5 6 7 8 ··· 14 다음