Archive for the ‘ajax’ Category

<a href=“#” OnClick=DoAction(1,’Jose’); > Click </a>

Using POST

function DoAction( id, name )
{
    $.ajax({
         type: “POST”,
         url: “someurl.php”,
         data: “id=” + id + “&name=” + name,
         success: function(msg){
                     alert( “Data Saved: “ + msg );
                  }
    });
}

Using GET

function DoAction( id, name )
{
     $.ajax({
          type: “GET”,
          url: “someurl.php?id=” + id + “&name=” + name,
          success: function(msg){
                     alert( “Data Saved: “ + msg );
                   }
     });
}

 
  • Comments Off

jquery simple

#testDiv {
	background-color:#3399CC;
	font: 12px Tahoma, Arial, sans-serif;
	color:#FFFFFF;
	width: 150px;
	height: 100px;
	text-align:center;
}
.testClass {
	background-color:#FF0000;
	font: 12px Tahoma, Arial, sans-serif;
	color:#FFFFFF;
	width: 150px;
	height: 100px;
	text-align:center;
}

* #testDiv และ .testClass ใน css เอาไว้แค่กำหนดหน้าตาเฉยๆ ไม่มีผลกับ jquery

<script src=“jquery.js” type=“text/javascript”><!–mce:0–></script>

เป็นการเรียกโหลด jquery.js เข้ามา

<a onclick=“$(’#testDiv’).show();” href=“javascript:void(0);”>Show</a>
<a onclick=“$(’#testDiv’).hide();” href=“javascript:void(0);”>Hide</a>

ดสำคัญมันอยู่ตรงนี้ onclick=”$(’#testDiv’).show();” คำสั่งนี้คือการ เลือกที่จะทำงานกับ
div (ขึ้นต้นด้วย #) ที่ชื่อว่า #testDiv นั่นเอง

 
<div id=“testDiv”>ข้อความ .. .. .. ข้อความ</div>
<a onclick=“$(’.testClass’).show();” href=“javascript:void(0);”>Show</a>
<a onclick=“$(’.testClass’).hide();” href=“javascript:void(0);”>Hide</a>

ในทำนองเดียวกัน onclick=”$(’.testClass’).show();” คำสั่งนี้คือการ เลือกที่จะทำงานกับ
class (ขึ้นต้นด้วย .) ที่ชื่อว่า .testClass นั่นเอง

 
<div class=“testClass”>ข้อความ .. .. .. ข้อความ</div>
  • Comments Off

อย่างที่ทราบกันครับว่าการส่ง-รับค่าแบบ AJAX นั้นสามารถส่งได้ 2 แบบ คือแบบ POST และ GET ซึ่งก้อเหมือนเราส่งค่าผ่าน HTML Form ธรรมดาทั่วไปนั้นเอง และ jQuery ก้อมีฟั่งชั่นรองรับที่ง่ายแสนง่าย ให้พวกเราใช้งานอยู่แล้วครับเรามาเริ่มกันเลยครับ
การส่ง-รับค่าแบบ POST
แบบที่ 1 (ไม่มีการส่งค่า Parameter)

  1. $.post(“ชื่อไฟล์ที่ต้องการดึงข้อมูล”, function(data){
  2. //คำสั่ง
  3. });

ตัวอย่าง JavaScript ในไฟล์ test.html

  1. $(document).ready(function(){ //เริ่มทำงานเมื่อโหลดเพจเสร็จ
  2. $.post(“test.php”, function(data){ //data จะเก็บค่าที่ return กลับมา
  3. alert(“Data is ”+data); //แสดงข้อความใน Alert box
  4. });
  5. });

โค๊ดในไฟล์ test.php

  1. echo ‘Hi, AJAX’; //return คำว่า ”Hi, AJAX”

ผลที่ได้คือ
0011.jpg
แบบที่ 2 (มีการส่งค่า Parameter)

  1. $.post(“ชื่อไฟล์ที่ต้องการดึงข้อมูล”, [JSON], function(data){
  2. //คำสั่ง
  3. });

ตัวอย่าง JavaScript ในไฟล์ test2.html

  1. $(document).ready(function(){ //เริ่มทำงานเมื่อโหลดเพจเสร็จ
  2. $.post(“test2.php”,{fname: “ทดสอบ”, lname: “นอนสบาย”}, function(data){ //มีการการส่งค่า fname และ lname ไปด้วย
  3. alert(“Data is ”+data); //แสดงข้อความใน Alert box
  4. });
  5. });

โค๊ดในไฟล์ test2.php

  1. $fname = $_POST["fname"]; // รับค่าแบบ POST
  2. $lname = $_POST["lname"];
  3. echo “Hi, {$fname} {$lanme}”; //return ค่าที่รับมาแบบ POST

ผลที่ได้คือ
0021.jpg
การส่ง-รับค่าแบบ GET
สำหรับการส่งแบบ GET นั้นสามารถทดสอบได้ด้วยโค๊ดเดียวกัน เพียงแต่เปลี่ยนจากฟังก์ชั่น $.pos() เป็น $.get() และเปลี่ยนการรับข้อมูลในไฟล์ PHP จาก $_POST เป็น $_GET

  • Comments Off

jQuery Object Accessors

jQuery Object Accessors

สำหรับ jQuery ในส่วน Object Accessor หรือเรียกว่าเป็นเรื่องของ function ของคำสั่งที่ jQuery เตรียมไว้ให้กับเราซึ่งมีอยู่ ด้วยกันดังนี้

Core / each

ส่วนนี้คือการทำงานในลักษณะ loop ซึ่งทำให้เราสามารถที่จะทำงาน วนข้อมูลง่ายๆ จนกว่าจะครบตามจำนวน Length ของ selector ที่เลือก ซึ่งจะขออธิบายในตัวอย่างนี้นะครับ

จากตัวอย่างนี้ จะเป็นการวน loop ตามจำนวน ที่ selector คือ $(“div”) ทั้งหมด จะสังเกตุเห็นว่า จะมี alert บอกว่ากำลังทำงานกับ tag div ตัวไหนอยู่ และ parameter i ก็จะเป็นตัวบอก รอบที่อยู่ใน loop เริ่มจาก 0

<script language=”javascript”>

$(document).ready(function(){

$( document.body ).click( function () {

$(”div”).each( function(i) {

alert( (i+1) + “. Working in Tag =” + this.innerHTML);

if (this.style.color !=”blue”) {

this.style.color=”blue”;

}else{

this.style.color=””;

}

} );

} );

});

</script>

<style type=”text/css”>

div { color:red; text-align:center; cursor:pointer;

font-weight:bolder; width:300px; }

</style>

มี Tag div อยู่ 3 ตัวซึ่งเราจะมองว่า Tag div แต่ละตัวคือ element แต่ละ element

<div>Click here</div>

<div>to iterate through</div>

<div>these divs.</div>

Core/Length

การใช้ method length เพื่อนับจำนวน array ของ Selector (method length เป็นของ javascript อยู่แล้ว) ซึ่งเราสามารถนำมาประยุกต์ใช้กับงานได้ ยกตัวอย่างเช่น ตัวอย่างการเพิ่มแถวของ Table และ ลบแถวของ Table อย่างง่ายๆ ซึ่งเราจะใช้ manipulation append เพื่อ create element แถวลงไปใน table โดยต่อท้ายแถวสุดท้ายลงไป

<script language=”javascript”>

$(document).ready(function(){

// ปุ่ม Insert

$(”#btnIn”).click(function () {

var txtnum=$(”.trcolor”).length+1;

$(”#tblData”).append($(”<tr class=\”trcolor\”> ” +

“           <td>”+txtnum+”</td>” +

“           <td><input type=\”text\”></td>”+

“           <td><input type=\”text\”></td>”+

“           <td><input type=\”text\”></td>”+

” </tr>”));

getMsg();

});

// ปุ่ม Delete

$(”#btnDel”).click(function (){

$(”.trcolor:last”).remove();

getMsg();

});

// function บอกจำนวนแถว โดยใช้ method length

function getMsg(){

var n=$(”.trcolor”).length;

$(”#lblMsg”).text(”There are ” + n + ” Rows.”);

}

});

</script>

<style type=”text/css”>

.trcolor { background:#F2F2F2; }

</style>

<button id=”btnIn”>RUN</button> | <button id=”btnDel”>Del</button>

<div id=”lblMsg”></div>

<table id=”tblData” width=”600″ border=”0″ cellpadding=”1″ cellspacing=”1″ bgcolor=”#CCCCCC”>

<tr>

<td width=”12%”><strong>Number</strong></td>

<td width=”24%”><strong> Name</strong></td>

<td width=”22%”><strong>Surname</strong></td>

<td width=”42%”><strong>Tel</strong></td>

</tr>

</table>

Core/eq(position)

Argument position คือการใส่ Number Index ลงไป

การใช้ eq สำหรับระบุ ไปยัง Index ของ element แบบเฉพาะเจาะจงลงไปในตัวใดตัวหนึ่ง จะอธิบายด้วยตัวอย่างนี้นะครับ

<script language=”javascript”>

$(document).ready(function(){

/* initial function */

$(”#btnRun”).click(function (){

$(”div”).eq(2).css(”background”,”#CCFFFF”);

});

});

</script>

จากตัวอย่างนี้จะเป็นการ เลือก selector ที่ element Div และกำหนด eq ที่ 2 คือตำแหน่ง index ที่ 2 จากข้อมูลชุดด้านล่างนี้ เพื่อทำการ ใส่สี่ background

<button id=”btnRun”>RUN</button>

<div><a href=”#”>Data 01</a> < — index 0 , first element </div>

<div><a href=”#”>Data 02</a> < — index 1</div>

<div><a href=”#”>Data 03</a> < — index 2</div>

<div><a href=”#”>Data 04</a> < — index 3          , last element</div>

Core/get

การใช้ get เป็นการรับ element จาก selector ซึ่งค่าที่ได้จะเป็นลักษณะ array

<script language=”javascript”>

$(document).ready(function(){

$(”#btnRun”).click(function (){

var objDiv= $(”div”).get(); // ใช้ get ใส่ตัวแปร objDiv

var txt=[];            // ประกาศตัวแปร Array

for (var i =0;i<objDiv.length;i++){

txt.push(objDiv[i].innerHTML);

// push คือ method ของ Javascript เพื่อ ใส่ข้อมูลใน ตัวแปร Array

}

$(”span”).text(txt.join(” , “));

// join คือ method ของ Javascript เพื่อ เชื่อมสมาชิกแต่ละตัว ใน Array ซึ่ง Argument ของ join คือตัวคั่นระหว่าง สมาชิกแต่ละตัว

});

});

</script>

<button id=”btnRun”>RUN</button><br>

Data Divs Join : <span style=”color:#FF0000″></span>

<hr>

<div>One</div>

<div>Two</div>

<div>Three</div>

<div>Four</div>

Core/get (index)

เป็นการรับ element จาก selector แต่ระบุ index เข้าไปเพื่อชี้เฉพาะเจาะจง ลงไป ว่าจะเลือกตัวไหน ยกตัวอย่าง จาก ตัวอย่างด้านบน

$(”div”).get(2).innerHTML หากกำหนด get(2) จะได้ค่า กลับมาเป็น Three เนื่องจากจะไปจับ elements Index ที่ 2 นับจาก 0 ซึ่งก็คือ แทก <div> ที่ข้อความว่า Three

Core/index (subject)

Subject คือ element ที่จะระบุใน Index

เรื่อง index จะเป็นเรื่องการหาค่า index ใน element ว่าอยู่ตำแหน่งที่เท่าไหร่ ซึ่ง subject จะเป็นตัวระบุ current ของ index ที่ถูกกระทำ หาก ยังไม่ได้ถูกกระทำ จะ คืนค่า -1 กลับมา

จากตัวอย่างนี้ เมื่อ click ไปที่ Tag div แต่ละตัว จะแสดง alert current Index ของตัวนั้นออกมา

<script language=”javascript”>

$(document).ready(function(){

$(”div”).click(function (){

var txt=”Index is : ” + $(”div”).index(this);

alert(txt);

});         });

</script>

<div>One</div>

<div>Two</div>

<div>Three</div>

<div>Four</div>

  • Comments Off

See demo
drag and drop the left table row and see result in right table

Why have another plugin?

Dragging and dropping rows within a table can?t be handled by general purpose drag and drop utilities for a number of reasons, not least because you need to move the whole row, not just the cell that receives the mouse events. Re-parenting the row also requires specific code. Sadly also, effects like fadeIn and fadeOut don?t work well with table rows on all browsers, so we have to go for simpler effects.
What does it do?

This TableDnD plugin allows the user to reorder rows within a table, for example if they represent an ordered list (tasks by priority for example). Individual rows can be marked as non-draggable and/or non-droppable (so other rows can?t be dropped onto them). Rows can have as many cells as necessary and the cells can contain form elements.

How to use


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>mcDropdown jQuery Plug-in</title>
<script type="text/javascript" src="jquery-1.2.6.js"></script>
<script type="text/javascript" src="jquery.tablednd.js"></script>
<link type="text/css" href="style.css" rel="stylesheet" media="all" />
<script type="text/javascript">
$(document).ready(function() {
$('#table-3').tableDnD({
        onDrop: function(table, row) {
            //alert($.tableDnD.serialize());
			$('#AjaxResult').load("ajax.php?" + $.tableDnD.serialize());
        }
    });
 });
</script>
</head>

<body>
<div class="tableDemo">
<div id="AjaxResult">
<img src="images/1.gif"> 1 One<br>
<img src="images/2.gif"> 2 Two<br>
<img src="images/3.gif"> 3 Three (Can&amp;amp;amp;amp;#8217;t drop on this row)<br>
<img src="images/4.gif"> 4 Four<br>
<img src="images/5.gif"> 5 Five (Can&amp;amp;amp;amp;#8217;t drag this row)<br>
<img src="images/6.gif"> 6 Six<br>
</div>
<table id="table-3" cellspacing="0" cellpadding="2" height="200">
<tr id="1">
<td bgcolor="#FFFF99">1</td>
<td bgcolor="#FFFF99">One</td>
</tr>
<tr id="2">
<td bgcolor="#FF99CC">2</td>
<td bgcolor="#FF99CC">Two</td>
</tr>
<tr id="3" class="nodrop">
<td bgcolor="#0066CC">3</td>
<td bgcolor="#0066CC">Three (Can&amp;amp;amp;amp;#8217;t drop on this row)</td>
</tr>
<tr id="4">
<td bgcolor="#33CC66">4</td>
<td bgcolor="#33CC66">Four</td>
</tr>
<tr id="5" class="nodrag">
<td bgcolor="#990099">5</td>
<td bgcolor="#990099">Five (Can&amp;amp;amp;amp;#8217;t drag this row)</td>
</tr>
<tr id="6">
<td bgcolor="#FF0000">6</td>
<td bgcolor="#FF0000">Six</td>
</tr>
</table>
</div>
</body>
</html>

php code ajax.php


<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
$result = $_REQUEST["table-3"];
$val = array("","One","Two","Three (Can&amp;amp;amp;amp;#8217;t drop on this row)","Four","Five (Can&amp;amp;amp;amp;#8217;t drag this row)","Six");
$pval = array("","1.gif","2.gif","3.gif","4.gif","5.gif","6.gif");
foreach($result as $value) {
	echo "<img src=\"images/$pval[$value]\"> $value $val[$value]<br/>";
}
?>

More info
http://www.isocra.com/2008/02/table-drag-and-drop-jquery-plugin/

For our help desk, customer service and call center software applications, we needed to develop a unique UI control that would allow users to select from a complex hierarchical tree of options. We wanted this control to be intuitive and it had to allow for both quick mouse and keyboard entry. After many rounds of discussion internally and with customers, we came up with the following list of requirements:

* Menus must resize and position themselves to stay in the viewport. Options should not show up off screen where users would need to scroll to select them.
* Quick keyboard entry. Many users prefer keyboard entry for speed of data entry, so efficient keyboard entry is a must.
* Must be able to handle very large complex (deep) data trees, but work equally as well with very small sets of data.
* Easy implementation.

Demo

While the list of requirements was not long, we knew there would be substantial technical obstacles to overcome. The biggest obstacle to solve would be how to ensure that the menu and all of its submenus remain on the screen at all times.

To overcome this obstacle, we spent a lot of time developing code that would automatically split long lists into multiple columns and then position itself so that it always fits within the viewport. The overall effect is somewhat similar to the Microsoft? Windows? “Start > All Programs” menu. However, the algorithm we use should keep the menus laid out in a more compact layout?which we believe offers quicker selection of the menu items. The screenshot below shows what the menu looks like when used with a very large menu.

How to use


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>mcDropdown jQuery Plug-in</title>
<script type="text/javascript" src="./lib/jquery-1.2.6.min.js"></script>
<script type="text/javascript" src="./lib/jquery.mcdropdown.js"></script>
<script type="text/javascript" src="./lib/jquery.bgiframe.js"></script>
<link type="text/css" href="./css/jquery.mcdropdown.css" rel="stylesheet" media="all" />
<script type="text/javascript">
<!--//
// on DOM ready
$(document).ready(function (){
//$("#current_rev").html("v"+$.mcDropdown.version);
  $("#category").mcDropdown("#categorymenu");
});
//-->
</script>
</head>

<body>
<form name="f1" method="get" action="">
	Please select a category:<br />
	<input type="text" name="category" id="category" value="">
<INPUT TYPE="submit" value="ok">
    <ul id="categorymenu" class="mcdropdown_menu">
        <li rel="1">
            Arts &amp;amp;amp;amp;amp; Humanities
            <ul>
                <li rel="2">
                    Photography
                    <ul>
                        <li rel="3">
                            3D
                            </li>
                        <li rel="4">
                            Digital
                            </li>
                    </ul>
                </li>
                <li rel="5">
                    History
                    </li>
                <li rel="6">
                    Literature
                    </li>
            </ul>
        </li>
        <li rel="7">
            Business &amp;amp;amp;amp;amp; Economy
          </li>
        <li rel="8">
            Computers &amp;amp;amp;amp;amp; Internet
            </li>
        <li rel="9">
            Education
            </li>
        <li rel="11">
            Entertainment
            <ul>
                <li rel="12">
                    Movies
                    </li>
                <li rel="13">
                    TV Shows
                    </li>
                <li rel="14">
                    Music
                    </li>
                <li rel="15">
                    Humor
                    </li>
            </ul>
        </li>
        <li rel="10">
            Health
            </li>
    </ul>
    </form>
</body>
</html>

More info
http://www.givainc.com/labs/mcdropdown_jquery_plugin.htm

Time

Mp3

Msn status

  • manon2029@hotmail.com is

Chat with Meeh

Donate

    If you find an article useful, then please make a donation.

หมวดหมู่

UserOnline

Counter

  • Visitors today: 23
  • Visitors yesterday: 48
  • Visitors per day: 2,160
  • Max. visitors per day: 255
  • Total page views: 155,892
  • Page views of this page: 2,160
  • Currently online: 1
  • Max. online: 23
  • Total visitors: 33,987
  • counterStatistics