Webクリエイティブ集団、アイレット株式会社の代表取締役が綴る
Web制作・開発の気になる最新技術レビュー。  RSS Feeds
スカウター : [iret-ceo] アイレット株式会社CEOブログ
ラベル JQuery の投稿を表示しています。 すべての投稿を表示
ラベル JQuery の投稿を表示しています。 すべての投稿を表示

2008年12月5日金曜日

オートコンプリート JQuery

JQueryでオートコンプリートやりたいと思い、調べました。
jquery.autocomplete.js
といういいものがありました。
これを使えば簡単にオートコンプリートが実装できます。

サンプルとして

<script type='text/javascript' src='js/jquery.autocomplete.js'></script>


<script type="text/javascript">
$(document).ready(function(){
 // イニシャル時にセットアップ
 setAutoCompliete("test");
});

// セットされたときに呼び出されます。
function findValue(li) {
if( li == null ) return alert("No match!");

// if coming from an AJAX call, let's use the CityId as the value
if( !!li.extra ) var sValue = li.extra[0];

// otherwise, let's just display the value in the text box
else var sValue = li.selectValue;

alert("The value you selected was: " + sValue);
}

function selectItem(li) {
findValue(li);
}
// オートコンプリートで表示をどのようにするか?
function formatItem(row) {
return row[0] + " (name: " + row[1] + ")";
}

// 初期設定
function setAutoCompliete(key){
// PHPを呼び出し
var url_text = "/test.php";
$('#' + key).autocomplete(url_text, {
onItemSelect : selectItem,
onFindValue : findValue,
formatItem : formatItem,
autoFill:true,
cacheLength:10,
delay:10,
matchSubset:1,
matchContains:1,
width:300
});
}
</script>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~省略
<input type="text" name="test" id="test" value="" />

PHP側の出力
1234|test
5678|test2
9012|test3

のような感じでやります。

これは簡単で便利です



2008年11月29日土曜日

JQuery focus時に枠の色を変える

HTMLでfocus時にGmailみたいに枠の色を変えたいとかあると思います。

非選択時


選択状態


そこで一括で変えるサンプルをのせておきます。

// focus.js

$(function(){
// input type=text password textareaのみ変える処理
 $("input[@type='text'],input[@type='password'],textarea")
  .focus(function(){
   $(this).addClass("focus");
  })
  
  .blur(function(){
   $(this).removeClass("focus");
  });
});


// focus.html

<script src="jquery.js" type="text/javascript"></script>
<script src="focus.js" type="text/javascript"></script>
<style type="text/css">
.focus {
color: #333333;
border: 2px solid #fc2e8b;
background-color: #ffffff;
}
</script>





以上これだけでフォームにあるものすべて枠に色が着きます。

2008年9月8日月曜日

JavaScript

完全JavaScript時代到来か
一応AjaxとかではやってはいたがFlashにはかなわないとかいっていたが
もうその次元ではなくなってきました。
SilverLightも視野はJavaScript
時代が変わる瞬間かな。。

http://japan.cnet.com/news/ent/story/0,2000056022,20379997,00.htm?ref=rss

2008年1月17日木曜日

JQuery パフォーマンス

弊社が推奨するJQueryのパフォーマンスが
かなりすぐれていることがわかった。
軽量だし、お勧めですね

Dojo 1.0.1、jQuery 1.2.1、Prototype 1.6.0、YUI 2.4.0、Protoculous 1.0.2
で調査した結果
http://journal.pc.mycom.co.jp/news/2008/01/17/030/index.html

2008年1月16日水曜日

JQuery バージョンアップ 速度向上

JQuery1.2.2が登場
大幅に速度が向上したという。(300%)
それ以外にもマウスホイルイベントなども取得できるようになり、
いろいろ試したいと思う。

JQueryを私はよく使うのでこの辺のデモ、サンプルができたらBlogに公開して
行こうと思います。

詳細は下記
http://journal.pc.mycom.co.jp/news/2008/01/15/044/index.html

2007年11月21日水曜日

Teeda Ajax & JQuery & JTemplates

以前の記事でTeeda Ajax&JQueryでテーブルの追加、削除を実装した
記事を載せた。しかしまだ効率が悪いことで、JTemplatesとなるもをCTOAから
ききJTemplatesはvelocityなどとにいているから使いやすいとのことでつかってみた

Teeda Ajaxでは通信のみ
JQueryで管理
JTemplatesで表示系を実施

下記サンプル例
初期ローディングを行い、表を表示
-----------------------------Sample
html-----------------------------------------

// 下記をインクルード

<script language="javascript" src="../include/lib/ajax.js"
type="text/javascript"></script>
<script language="javascript" src="../include/lib/jquery.js"
type="text/javascript"></script>
<script language="javascript"
src="../include/lib/jquery-jtemplates.js"
type="text/javascript"></script>

// 実装部分 Ajax連携
<script type="text/javascript">
// イニシャル処理
$(document).ready(function(){
// ローディング
$('#testList').empty().append( '<img src="../img/loading.gif" />' );
// Teedaでデータの読み込み 引数はなし
Kumu.Ajax.executeTeedaAjax(test_ajaxTest, [], Kumu.Ajax.RESPONSE_TYPE_JSON);
});
function test_ajaxTest(res){
// ローディングをクリア
$('#testList').empty();
// テンプレート読み込み
$("#testList").setTemplateURL("./testList.tpl");
// JSON形式のデータを取得しテンプレートに引き渡す
$('#testList').processTemplate(res);
}
</script>
-----------------------------Sample
html-----------------------------------------
-----------------------------Sample
test.tpl-------------------------------------
{#template MAIN}
<table>
{#foreach $T as row}
<tr><td>{$T.row.a}</td></tr>
<tr><td><input type="text" value="{$T.row.b}"/></td></tr>
{#/for}
{#/template MAIN}
-----------------------------Sample
test.tpl-------------------------------------

Teeda Ajaxは以前の記事参照
http://iret-ceo.blogspot.com/2007/10/teeda-ajax-jquery.html
JSONの形式
[
{a: 1, b: 'test1'},
{a: 2, b: 'test2'},
{a: 3, b: 'test3'}
]

テンプレートにすることによってかなり簡潔に書けるようになった。
これは便利今後ともつかわねば

SUZ-LAB
http://suz-lab.blogspot.com/2007/11/jtemplatesjquery.html
CTOA参考
http://app.blog.livedoor.jp/hiroki0907/tb.cgi/50965571

2007年11月20日火曜日

Teeda Ajax 文字化け

Teeda Ajax 文字化けの対応方法
(JQueryも使用しています。)

下記のようにJavaScript側でencodeURIを実行
------------javaScript--------------------------
function updateComment(){
var comment = encodeURI($("#comment").val());
Kumu.Ajax.executeTeedaAjax(ajaxTestMoji, [comment], Kumu.Ajax.RESPONSE_TYPE_JSON);
}
------------javaScript--------------------------

Teedaでの受け取りとしてURLDecoder.decode
------------teeda ajax---------------------------
public String ajaxTestMoji(String comment){
URLDecoder.decode(comment,"UTF-8");
// DBとかにInsertなどなど
}

------------teeda ajax---------------------------

まだいい方法があるかもしれないが、まずはメモメモ

2007年11月6日火曜日

JQuery 便利機能2 テーブルの表示非表示

JQueryのプラグインでとても便利な機能を見つけました。
仕事上でどうしてもテーブルの列を表示非表示させたいことから
なんかいいのないか?と探していたら見つけました。

まずはプラグインをダウンロード

プラグインは下記から
http://p.sohei.org/jquery-plugins/columnmanager/

対象コンテンツにincludeします。
<script type="text/javaScript" src="../js/jquery.js"></script>
<script type="text/javaScript" src="../js/jquery.columnmanager.js"></script>

テーブルにidを定義します。
<table id="tableall">

表示非表示を制御するcheckbox
<input type="checkbox" id="name" value="1" checked>名前
<input type="checkbox" id="price" value="1">お金

イニシャル処理に下記を定義
$(document).ready(function(){
// チェックボックスをクリックイベントの登録
   $('#name').click(function(){ $('#tableall').toggleColumns(1);});
   $('#price').click(function(){ $('#tableall').toggleColumns(2);});

   // ディフォルトチェックボックス状態の確認(初期時に表示しておきたいものを定義)
if(!$('#name:checked').val())$('#tableall').toggleColumns(1);
if(!$('#price:checked').val())$('#tableall').toggleColumns(2);
});

以上でおわり 簡単ですね。
JQueryすばらしい!!


デモは下記から
http://p.sohei.org/stuff/jquery/columnmanager/demo/demo.html

JQuery 便利機能 テーブルをシマシマに

JQueryをつかうとテーブルが簡単にシマシマに変わることができる。
今までテンプレートなどで i % 2 == 0 とかで切り替えていたのが
下記のようにstripeを追加するだけでかわる
<table class="stripe">
もちろんjqueryのincludeを忘れずに
<script type="text/javaScript" src="../js/jquery.js"></script>

応用として
マウスカーソルを当てたら自動的に色が変わるようにも簡単にできる
下記のようにイベントをloadで追加
$(document).ready(function(){
$(".stripe tr").mouseover(function(){
$(this).addClass("over");
}).mouseout(function(){
$(this).removeClass("over");
});
});
スタイルシートを定義
<style>
tr.even td {
background-color: #f3f3f3;
}
tr.over td {
background: #666666;
color: #FFFFFF;
}
</style>

これで完成!

2007年10月31日水曜日

Teeda Ajax & JQuery

またTeeda AjaxとJQueryを使ったサンプルです。
テーブルの行の追加を何とか実装したくてJQueryを使うことにしました。
ちょっとソースは汚いですが、まだまだ効率化できるはず。
テーブルのデザイン等はスタイルシートをあてて表示
リストがしましまになるようにループ時に別のスタイルシートをあてるようにしました。


例として ポイント付与するプログラムです。
下記がHTMLの例です。
--------------------------------------------------------------------------------------------------------------------
<html xml:lang="ja" lang="ja"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:te="http://www.seasar.org/teeda/extension"
  xmlns:x="http://myfaces.apache.org/tomahawk">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <script type="text/javaScript" src="../js/ajax.js"></script>
 <script type="text/javaScript" src="../js/jquery.js"></script>
 <script type="text/javascript">
 <!--
  function usersearch_entryPage_ajaxAddPoint(res){
   var totalpoint = 0;
   // ローディングをクリア
    $('#result').empty();
    tbody = document.getElementById("Items");
    $(tbody).empty();
   // 検索結果をループで回す
   for( i=0; i < res.length; i++ ) {
     var entry = res[i];
     var tr = document.createElement( 'tr' );
     if(i % 2 == 0) $(tr).addClass("list1");
     else $(tr).addClass("list2");
     tbody.appendChild( tr );

     // 日時(date)のセル
     var tddate = document.createElement( 'td' );
     $(tddate).text( entry.pointinststamp );
     $(tddate).addClass("S3");
     tr.appendChild( tddate );

     // 種別 userPointType
     var tdtype = document.createElement( 'td' );
     var type = "";
     if(entry.userpointtype == 1)type = "登録ポイント";
     if(entry.userpointtype == 2)type = "予約ポイント";
     if(entry.userpointtype == 3)type = "クチコミポイント";
     if(entry.userpointtype == 4)type = "特別ポイント";
     if(entry.userpointtype == 5)type = "使用済みポイント";
     $(tdtype).text( type );
     $(tdtype).addClass("S3");
     tr.appendChild( tdtype );

     // ポイントのセル
     var tdpoint = document.createElement( 'td' );
     $(tdpoint).text( entry.userpoint );
     $(tdpoint).addClass("S3");
     tr.appendChild( tdpoint );
     totalpoint = totalpoint + entry.userpoint;

     // タイトル(title)+リンク(link)のセル
     var tdlink = document.createElement( 'td' );
     $(tdlink).addClass("S3");
     var center = document.createElement( 'center' );
     tdlink.appendChild( center );
     var a = document.createElement( 'a' );
     $(a).text( "削除" );
     a.href = "javascript:deletePoint(" + entry.userpointid + ")" ;
     center.appendChild( a );
     tr.appendChild( tdlink );
   }
   // 合計を表示する
   document.getElementById("totalpoint").innerHTML = totalpoint;
   document.getElementById("point").value = 0;
  }
  function addPoint(){
    tbody = document.getElementById("Items");
    $(tbody).empty();
   // ローディングを表示
    $('#result').empty().append( '<img src="../img/loading.gif" />' );
  var form = Kumu.FormHelper.create('entryForm');
   point = document.getElementById("point").value;
   userId = form["entryForm:userId"];
   if(point == undefined || point == "")point = 0;
    Kumu.Ajax.executeTeedaAjax(usersearch_entryPage_ajaxAddPoint,
[userId,point], Kumu.Ajax.RESPONSE_TYPE_JSON);
  }

  function usersearch_entryPage_ajaxDeletePoint(){
   addPoint();
  }
  function deletePoint(userPointId){
    $('#result').empty().append( '<img src="../img/loading.gif" />' );
  var form = Kumu.FormHelper.create('entryForm');
   userId = form["entryForm:userId"];
    Kumu.Ajax.executeTeedaAjax(usersearch_entryPage_ajaxDeletePoint,
[userPointId,userId], Kumu.Ajax.RESPONSE_TYPE_JSON);
  }
  -->
  -->
 </script>
  <style>
   .onTeedaError {
    background-color: #FFCCCC;
   }
   .must{
    color: #F00;
    font-size: 10px;
    font-weight: bold;
   }
   .list2 td {
    background:#EEEEEE;
   }
   .list1 td {
    background:#FFFFFF;
   }
  </style>

</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"
onLoad="javascript:addPoint()">
 <form id="entryForm">
   <table width="800" border="0" cellspacing="0" cellpadding="0">
    <tr valign="top">
     <td>
      <table width="750" border="0" cellspacing="1" cellpadding="3" >
       <tr style="background:#a7dafd;">
        <th width="150" height="25" class="S3" >ポイント合計</th>
        <td class="S3" style="background:#EEEEEE;">
         <span id="totalpoint">3,000</span> ポイント
        </td>
        <th width="150" height="25" class="S3" >特別ポイント付与</th>
        <td class="S3" style="background:#EEEEEE;">
         <input type="text" id="point" size="5"
value="0"/>&nbsp;&nbsp;&nbsp;<input type="button"
onClick="javascript:addPoint()" value="ポイント付与" /><br />
        </td>
       </tr>
      </table>
      <center><p id="result"></p></center>
      <p id="url"></p>
      <table width="750" border="0" cellspacing="1" cellpadding="3">
       <thead>
        <tr style="background:#a7dafd;">
         <th width="150" class="S3">
          日付<br />
         </th>
         <th width="250" class="S3">
          種別<br />
         </th>
         <th width="200" class="S3">
          ポイント<br />
         </th>
         <th width="150" class="S3">
          削除<br />
         </th>
        </tr>
       </thead>
       <tbody id="Items">
       </tbody>
      </table>
     </td>
    </tr>
   </table>
 </form>
</body>
</html>
--------------------------------------------------------------------------------------------------------------------
Teeda Ajax
// 抜粋
public String ajaxAddPoint(int userId,int point){
  this.userId = userId;
  if(point > 0){
    // データのインサート
    usersearchLogic.insertUserPoint(userId,point,"shouhei");
   }
   // 一覧取得
   usersearchLogic.findUserPoint(this);
   // JSON形式でリストを返す
   return JSONSerializer.serialize(this.userPointList);
}