Have you ever developed a web Project that needed an auto refreshing data all the time?
If not you will probably have to soon.
This is my new post on Woork Up! Check it out:
Showing posts with label jquery. Show all posts
Showing posts with label jquery. Show all posts
Sunday, September 26, 2010
Tuesday, July 20, 2010
Using jQuery to search in HTML
What are we going to do?
How?
The example HTML structure
<div id="search">
<input type="text" name="q" />
</div>
<ul id="content">
<li>Donec posuere lobortis urna, sit amet adipiscing purus blandit vel.</li>
<li>Morbi in sem quis dui placerat ornare. Pellentesque odio nisi.</li>
<li>Praesent dapibus, neque id cursus faucibus. </li>
<li>Phasellus ultrices nulla quis nibh. Quisque a lectus.</li>
<li>Duis tempus turpis in neque sagittis at adipiscing. Nulla congue turpis ac turpis aliquet placerat.</li>
<li>Pellentesque fermentum dolor.</li>
</ul>
JS
$(function(){
/**
* Fix to disable case-sensitive
**/
$.expr[':'].icontains = function(obj, index, meta, stack){
return (obj.textContent || obj.innerText || jQuery(obj).text() || '').toLowerCase().indexOf(meta[3].toLowerCase()) >= 0;
};
$("div#search input[type=text]").keydown(function(e){
var $input = $(this);
if($input.val() != ''){
$('ul#content li').hide();
$('ul#content li:icontains('+$input.val()+')').show();
}
});
});
Optional CSS
body{
margin:0;
}
div#search{
background:#ddd;
border-bottom:1px solid #bbb;
padding:3px 10px;
}
div#search input[type=text]{
border:1px solid #aaa;
width:100%;
font-size:16px;
}
ul#content{
margin:0;
padding:0;
list-style:none;
}
ul#content li{
background:#E0EDFF;
border-bottom:1px solid #BACCE6;
padding:3px 5px;
}
Example of use
A good place to use this technique is on a chat list. You'll search all your contacts without refreshing or sending many ajax requests. Just asking your HTML!Usando jQuery para procurar no HTML
O que vamos fazer?
Como?
A estrutura HTML do exemplo
<div id="search">
<input type="text" name="q" />
</div>
<ul id="content">
<li>Donec posuere lobortis urna, sit amet adipiscing purus blandit vel.</li>
<li>Morbi in sem quis dui placerat ornare. Pellentesque odio nisi.</li>
<li>Praesent dapibus, neque id cursus faucibus. </li>
<li>Phasellus ultrices nulla quis nibh. Quisque a lectus.</li>
<li>Duis tempus turpis in neque sagittis at adipiscing. Nulla congue turpis ac turpis aliquet placerat.</li>
<li>Pellentesque fermentum dolor.</li>
</ul>
JS
$(function(){
/**
* Fix para desabilitar o case-sensitive
**/
$.expr[':'].icontains = function(obj, index, meta, stack){
return (obj.textContent || obj.innerText || jQuery(obj).text() || '').toLowerCase().indexOf(meta[3].toLowerCase()) >= 0;
};
$("div#search input[type=text]").keydown(function(e){
var $input = $(this);
if($input.val() != ''){
$('ul#content li').hide();
$('ul#content li:icontains('+$input.val()+')').show();
}
});
});
CSS Opcional
body{
margin:0;
}
div#search{
background:#ddd;
border-bottom:1px solid #bbb;
padding:3px 10px;
}
div#search input[type=text]{
border:1px solid #aaa;
width:100%;
font-size:16px;
}
ul#content{
margin:0;
padding:0;
list-style:none;
}
ul#content li{
background:#E0EDFF;
border-bottom:1px solid #BACCE6;
padding:3px 5px;
}
Exemplo de uso
Um bom lugar parar usar esta técnica é numa lista de contatos de um chat, por exemplo. Você irá procurar automaticamente sem precisar atualizar ou enviar muitas requisições ajax. Apenas pergunte ao seu HTML! (:Using Keyboard Shortcuts On You Web Application With jQuery
Here's the basic code:
$(function(){
$('body').keydown(function(e){
e.preventDefault();
if(e.keyCode==13){ // enter
// action
}
});
});
Note: You may use it for a specifc element instead of 'body', for example an input.At the end of the post you'll find how to do this with "CTRL+Key" actions.
But what are the keycode's? And where do I find them?
Right Here:| Key Pressed | Key Code | backspace | 8 |
|---|---|
| tab | 9 |
| enter | 13 |
| shift | 16 |
| ctrl | 17 |
| alt | 18 |
| pause/break | 19 |
| caps lock | 20 |
| escape | 27 |
| page up | 33 |
| page down | 34 |
| end | 35 |
| home | 36 |
| left arrow | 37 |
| up arrow | 38 |
| right arrow | 39 |
| down arrow | 40 |
| insert | 45 |
| delete | 46 |
| 0 | 48 |
| 1 | 49 |
| 2 | 50 |
| 3 | 51 |
| 4 | 52 |
| 5 | 53 |
| 6 | 54 |
| 7 | 55 |
| 8 | 56 |
| 9 | 57 |
| a | 65 |
| b | 66 |
| c | 67 |
| d | 68 |
| e | 69 |
| f | 70 |
| g | 71 |
| h | 72 |
| i | 73 |
| j | 74 |
| k | 75 |
| l | 76 |
| m | 77 |
| n | 78 |
| o | 79 |
| p | 80 |
| q | 81 |
| r | 82 |
| s | 83 |
| t | 84 |
| u | 85 |
| v | 86 |
| w | 87 |
| x | 88 |
| y | 89 |
| z | 90 |
| left window key | 91 |
| right window key | 92 |
| select key | 93 |
| numpad 0 | 96 |
| numpad 1 | 97 |
| numpad 2 | 98 |
| numpad 3 | 99 |
| numpad 4 | 100 |
| numpad 5 | 101 |
| numpad 6 | 102 |
| numpad 7 | 103 |
| numpad 8 | 104 |
| numpad 9 | 105 |
| multiply | 106 |
| add | 107 |
| subtract | 109 |
| decimal point | 110 |
| divide | 111 |
| f1 | 112 |
| f2 | 113 |
| f3 | 114 |
| f4 | 115 |
| f5 | 116 |
| f6 | 117 |
| f7 | 118 |
| f8 | 119 |
| f9 | 120 |
| f10 | 121 |
| f11 | 122 |
| f12 | 123 |
| num lock | 144 |
| scroll lock | 145 |
| semi-colon | 186 |
| equal sign | 187 |
| comma | 188 |
| dash | 189 |
| period | 190 |
| forward slash | 191 |
| grave accent | 192 |
| open bracket | 219 |
| back slash | 220 |
| close braket | 221 |
| single quote | 222 |
Using "CTRL + Key" Actions
var isCtrl = false;
$(document).keyup(function (e) {
if(e.which == 17)
isCtrl=false;
}).keydown(function (e) {
if(e.which == 17) isCtrl=true;
if(e.which == 83 && isCtrl == true) {
//run code for CTRL+S return false;
}
});
Atalhos de Teclado com jQuery
O código Básico:
$(function(){
$('body').keydown(function(e){
e.preventDefault();
if(e.keyCode==13){ // enter
// ação
}
});
});
OBS: Você pode usar para um elemento específico ao invés do 'body', um input, por exemplo.No final do post você vai ver como fazer o mesmo para atalhos "CTRL+TECLA".
Mas o que é um keyCode? E onde os encontro?
Aqui:| Tecla (key) | Código (Key Code) | backspace | 8 |
|---|---|
| tab | 9 |
| enter | 13 |
| shift | 16 |
| ctrl | 17 |
| alt | 18 |
| pause/break | 19 |
| caps lock | 20 |
| escape | 27 |
| page up | 33 |
| page down | 34 |
| end | 35 |
| home | 36 |
| left arrow | 37 |
| up arrow | 38 |
| right arrow | 39 |
| down arrow | 40 |
| insert | 45 |
| delete | 46 |
| 0 | 48 |
| 1 | 49 |
| 2 | 50 |
| 3 | 51 |
| 4 | 52 |
| 5 | 53 |
| 6 | 54 |
| 7 | 55 |
| 8 | 56 |
| 9 | 57 |
| a | 65 |
| b | 66 |
| c | 67 |
| d | 68 |
| e | 69 |
| f | 70 |
| g | 71 |
| h | 72 |
| i | 73 |
| j | 74 |
| k | 75 |
| l | 76 |
| m | 77 |
| n | 78 |
| o | 79 |
| p | 80 |
| q | 81 |
| r | 82 |
| s | 83 |
| t | 84 |
| u | 85 |
| v | 86 |
| w | 87 |
| x | 88 |
| y | 89 |
| z | 90 |
| left window key | 91 |
| right window key | 92 |
| select key | 93 |
| numpad 0 | 96 |
| numpad 1 | 97 |
| numpad 2 | 98 |
| numpad 3 | 99 |
| numpad 4 | 100 |
| numpad 5 | 101 |
| numpad 6 | 102 |
| numpad 7 | 103 |
| numpad 8 | 104 |
| numpad 9 | 105 |
| multiply | 106 |
| add | 107 |
| subtract | 109 |
| decimal point | 110 |
| divide | 111 |
| f1 | 112 |
| f2 | 113 |
| f3 | 114 |
| f4 | 115 |
| f5 | 116 |
| f6 | 117 |
| f7 | 118 |
| f8 | 119 |
| f9 | 120 |
| f10 | 121 |
| f11 | 122 |
| f12 | 123 |
| num lock | 144 |
| scroll lock | 145 |
| ; | 186 |
| = | 187 |
| , | 188 |
| - | 189 |
| . | 190 |
| / | 191 |
| grave accent | 192 |
| open bracket | 219 |
| back slash | 220 |
| close braket | 221 |
| ' | 222 |
Atalhos "CTRL + Key"
var isCtrl = false;
$(document).keyup(function (e) {
if(e.which == 17)
isCtrl=false;
}).keydown(function (e) {
if(e.which == 17) isCtrl=true;
if(e.which == 83 && isCtrl == true) {
// executa código para CTRL+S return false;
}
});
Sunday, July 18, 2010
Auto Content Refreshing with jQuery
jQuery is one of the most powerful tools in these days for a web developer
It's so good and so simple! Perfect for us!
So let's show you how to use jQuery to create a simple and efficient auto content refresher.
Html:
Javascript:
Now let's explain it.
$.get() is the jQuery function that send the GET request. $.get has 3 param's, firs is the URL, second params and third is callback.
Something like this:
The url must redirects to your .php (.html, or any other programming language) that will handle your request, return the result.
The setTimeout function creates a kind of loop, recalling that function every 15000 ms.
To cancel the auto refresh simply call the function cancelRefresh();
Note: You may use $.get, $.post or even $.load.
It's so good and so simple! Perfect for us!
So let's show you how to use jQuery to create a simple and efficient auto content refresher.
Html:
<html>
<head>
<title>Auto Refreshing Data</title>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<div id="container"></div>
</body>
</html>
Javascript:
<script type="text/javascript">
$(function(){
var refreshTime = 0;
function refresh(){
$.get('refresh.php',{},function(callback){
$('#container').html(callback);
refreshTime = setTimeout(refresh,15000); // we set the loop
});
}
function cancelRefresh(){
clearTimeout(refreshTime);
}
refresh(); // call it for the first time
});
</script>
Now let's explain it.
$.get() is the jQuery function that send the GET request. $.get has 3 param's, firs is the URL, second params and third is callback.
Something like this:
$.get(url, params,function(callback){...});The url must redirects to your .php (.html, or any other programming language) that will handle your request, return the result.
The setTimeout function creates a kind of loop, recalling that function every 15000 ms.
To cancel the auto refresh simply call the function cancelRefresh();
Note: You may use $.get, $.post or even $.load.
Atualização Automática de Conteúdo com jQuery
jQuery é uma das ferramentas mais poderosas hoje em dia para um desenvolvedor web.
É tão bom e ao mesmo tempo tão simples! Perfeito para nós :D
Então vamos mostrar para você como usar jQuery para criar um simple e eficiente autalizador automático de conteúdo.
Html:
Javascript:
Agora vamos entender.
$.get() é uma função do jQuery que envia a requisição GET. $.get tem 3 parâmetros, o primeiro é a URL, o segundo são parâmetros a serem passados (se necessário) e o terceiro é a resposta.
Algo assim:
A url deve redirecionar para seu arquivo .php (.html ou qualquer outra linguagem) que vai administrar a requisição, retornando a resposta.
A função setTimeout cria uma espécie de loop, chamando novamente a nossa função a cada 15000 ms.
Para cancelar a atualização automática basta simplesmente chamar a função cancelaAtualiza();
OBS: Você pode usar $.get, $.post ou até mesmo $.load.
É tão bom e ao mesmo tempo tão simples! Perfeito para nós :D
Então vamos mostrar para você como usar jQuery para criar um simple e eficiente autalizador automático de conteúdo.
Html:
<html>
<head>
<title>Conteúdo Automático</title>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<div id="receptor"></div>
</body>
</html>
Javascript:
<script type="text/javascript">
$(function(){
var tempoAtualizacao = 0;
function atualiza(){
$.get('refresh.php',{},function(resposta){
$('#receptor').html(resposta);
tempoAtualizacao = setTimeout(atualiza,15000); // we set the loop
});
}
function cancelaAtualiza(){
clearTimeout(tempoAtualizacao);
}
atualiza(); // chamamos pela primeira vez a atualização
});
</script>
Agora vamos entender.
$.get() é uma função do jQuery que envia a requisição GET. $.get tem 3 parâmetros, o primeiro é a URL, o segundo são parâmetros a serem passados (se necessário) e o terceiro é a resposta.
Algo assim:
$.get(url, parametros,function(resposta){...});A url deve redirecionar para seu arquivo .php (.html ou qualquer outra linguagem) que vai administrar a requisição, retornando a resposta.
A função setTimeout cria uma espécie de loop, chamando novamente a nossa função a cada 15000 ms.
Para cancelar a atualização automática basta simplesmente chamar a função cancelaAtualiza();
OBS: Você pode usar $.get, $.post ou até mesmo $.load.
Subscribe to:
Comments (Atom)

