Showing posts with label ajax. Show all posts
Showing posts with label ajax. Show all posts

Thursday, August 12, 2010

PHP : Ajax : Refresh partially without refresh entire page

1. Show alert after 10 seconds with ajax setInterval function

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script language="JavaScript">
setInterval( "alert('Hello World')", 10000 );
</script>


2. Update your contents after 5 seconds with ajax setInterval function

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script language="JavaScript">
setInterval( "hello();", 5000 ); ///////// 5 seconds

$(function() {
hello = function(){
$('#dataDisplay').prepend("Hi This is auto refresh example<br><br>").fadeIn("slow");
}
});
</script>


3. Refresh div when insert database row


Ajax JQuery / PHP – Refresh Table Demo
Download Ajax JQuery / PHP – Refresh Table

Refer link:
http://www.w3cgallery.com/
http://www.brightcherry.co.uk

Tuesday, February 23, 2010

Ruby on Rails : Ajax : Running Digital Clock

Step 1: view.rhtml
<%= javascript_include_tag "prototype" %>
<h1>Test periodically_call_remote</h1>
<div id='show_date_time_id' style="background-color: #eee"></div>
<%= periodically_call_remote(:update => 'show_date_time_id',:url =>
{ :action => :show_date_time },
:frequency => 1 )%>

Step 2: ajax_controller.rb
def show_date_time
render :text=>"Now : #{DateTime.now.to_s}"
end


If you find this useful, would you like to buy me a drink? No matter more or less, it will be an encouragement for me to go further. Thanks in advance!! =)

Thursday, November 12, 2009

Ruby on Rails : Thinking Sphinx : Search Engine (gem)

Sphinx is a very fast search engine that indexes data and provides flexible ways of searching it. Thinking Sphinx allows you to link up your models into Sphinx simply and painlessly -– because let’s face it, searching across multiple fields using SQL is a pain in the neck.

Step by step install Sphinx.

Basic files need to edit and according files to apply the function :
1. /config/environment.rb
2. /config/sphinx.yml
3. /app/controllers/articles_controller.rb
4. /app/models/article.rb
5. /app/views/articles/index.rhtml
6. /app/views/articles/_searchResult.rhtml

1. Add few line to /config/environment.rb
Rails::Initializer.run do |config|
config.gem(
'thinking-sphinx',
:lib => 'thinking_sphinx/0.9.8',
:version => '1.3.3'
)
end

2. /config/sphinx.yml
development:
enable_star: 1
min_infix_len: 1
test:
enable_star: 1
min_infix_len: 1
production:
enable_star: 1
min_infix_len: 1

3. /app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
def SphinxSearch
@articles = Article.search '*book*', :with => {
:created_at => 8.week.ago..Time.now,
}

render :partial => 'searchResult', :collection => @articles
end
end

4. /app/models/article.rb
class Article < ActiveRecord::Base
define_index do
# search from articles db table
indexes title, :sortable => true, :facet => true
indexes author, :sortable => true, :facet => true
indexes type, :sortable => true, :facet => true

# search from publishers db table
indexes publishers(:name), :as => :publishers_name, :sortable => true

# search from books db table
indexes books(:description), :as => :books_description, :sortable => true

end
end

** When you make changes to your Sphinx index structure, you will need to stop and start Sphinx for these changes to take effect, as well as re-index the data. This is all wrapped up into a single task:
rake thinking_sphinx:rebuild
rake ts:rebuild

5. /app/views/articles/index.rhtml
<% form_remote_tag(:url => {:action => 'SphinxSearch'}, 
:update => "resultList") do %>
<%= submit_tag 'Search' %>
<% end %>

<div id="resultList">
<!-- Search Result will be displayed here -->
</div>

6. /app/views/articles/_searchResult.rhtml
<% if articles.length != 0 %>
<% for article in articles %>
Id : <%= article.id %><br>
Author : <%= article.author %><br>
Type : <%= article.type %><hr>
<% end %>
<% else %>
No record found!
<% end %>


Sphinx
http://www.sphinxsearch.com/

Ultrasphinx

http://blog.evanweaver.com/files/doc/fauna/ultrasphinx/files/README.html

Thinking Sphinx

http://freelancing-god.github.com/ts/en/
http://groups.google.com/group/thinking-sphinx

Ruby on Rails : Show items per page in Ajax

In views.rhtml
<script language = "javascript">
function check(pagenum, totalpage){
for(var i=0;i<=totalpage;i++){
if(i==pagenum)
document.getElementById('page'+pagenum).style.border='1px solid #000000';
else
document.getElementById('page'+i).style.border='0px solid #000000';
}
}
</script>

<%= javascript_include_tag "prototype" %>
<%
@articles = Article.find(:all, :limit => 10)
@allarticles = Article.find(:all)
@total = @allarticles.length/10

for x in 0..@total
@num = x+1
%>
<span id="page<%=x%>" onclick="check(<%=x%>,<%=@total%>)">
<%= link_to_remote @num, :url => { :action => "articles_per_page", :id => x },
:update => "article_list" %>
</span>
<% end %>

<div id = 'article_list'>
<%= render :partial => 'article', :collection => @articles %>
</div>

In partial page, _article.rhtml
Title: <%= article.title %><br>
Author: <%= article.author %>
<hr>

In controller.rb
def articles_per_page
@show = ((params[:id].to_i)*10)
@articles = Article.find(:all, :offset => @show, :limit => 10)
render :partial => 'article', :collection => @articles
end
** :offset, starting id to show per page
** :limit, limit data showing

Class Pagination : http://rails.rubyonrails.org/classes/ActionController/Pagination/Paginator/Page.html#M000158
Prototype Helper : http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M001645
Other source : http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/remote_form_for

Wednesday, November 11, 2009

Ruby on Rails : Simple search engine using Ajax

After type words in the input field and wait awhile, the result will display at the partial page, no need to hit enter.

In views.rhtml
<%= javascript_include_tag "prototype" %>
Search : <%= text_field "article", "title" %>
<%= observe_field :article_title,
:frequency => 1.0,
:update => 'article_list',
:url => { :action => 'search' },
:with => "'search=' + encodeURIComponent(value)"
%>
<div id = 'article_list'>
<%= render :partial => 'article', :collection => @articles %>
</div>

In partial page, _article.rhtml
Title: <%= article.title %><br>
Author: <%= article.author %>
<hr>

In controller.rb
def search
@articles = Article.find(:all,
:conditions => ["lower(title) like ?", "%" + params[:search].downcase + "%"])

if params[:search].to_s.size < 1
@articles = Article.find(:all)
render :partial => 'article', :collection => @articles
else
if @articles.size > 0
render :partial => 'article', :collection => @articles
else
render :text => "Result not found!", :layout => false
end
end
end

** To wider search :
@articles = Article.find(:all, :conditions => ["lower(title) like ? and lower(type) like ?
and number like ?", "%" + params[:search][:title].downcase + "%",
"%" + params[:search][:type].downcase + "%", "%" + params[:search][:number] + "%"])

Prototype Helper : http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M001645