`

in_place_editing使用小记

阅读更多

in_place_editing是一个用于原地编辑的ajax小控件。

典型的效果:



 首先请下载相关的rails插件,大家注意:我这里的rails版本是2.1.2 ,所以原始的插件需要改进。

插件原始地址:http://svn.rubyonrails.org/rails/plugins/in_place_editing/

插件相关改进的讨论:http://railsforum.com/viewtopic.php?id=22457

这是我根据相关的讨论修改后的版本:http://qianjigui.iteye.com/upload/attachment/59164/1ddb2805-c9ce-3a9a-9d03-f950017857f4.zip

 

下面是具体的步骤:

  1. 创建测试应用
    rails test
  2. 添加需要使用的插件
    cd test/vendor/plugins/
    ls  => in_place_editing.zip
    unzip in_place_editing.zip
  3. 添加一个数据表
    ruby script/generate scaffold account
    #需要根据自己系统的特点配置 config/database.yml
    #gvim db/migrate/20081212144700_create_accounts.rb
    class CreateAccounts < ActiveRecord::Migration
      def self.up
        create_table :accounts do |t|
          t.column :name, :string
          t.column :id_card, :string
          t.column :email, :string
          t.timestamps
        end
      end
    
      def self.down
        drop_table :accounts
      end
    end
    
     
    # MySQL.  Versions 4.1 and 5.0 are recommended.
    #
    # Install the MySQL driver:
    #   gem install mysql
    # On Mac OS X:
    #   sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql
    # On Mac OS X Leopard:
    #   sudo env ARCHFLAGS="-arch i386" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
    #       This sets the ARCHFLAGS environment variable to your native architecture
    # On Windows:
    #   gem install mysql
    #       Choose the win32 build.
    #       Install MySQL and put its /bin directory on your path.
    #
    # And be sure to use new-style password hashing:
    #   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
    development:
      adapter: mysql
      encoding: utf8
      database: test_development
      username: testmysql
      password: ******
      host: localhost
      socket: /var/run/mysqld/mysqld.sock
      
    # Warning: The database defined as "test" will be erased and
    # re-generated from your development database when you run "rake".
    # Do not set this db to the same as development or production.
    test:
      adapter: mysql
      encoding: utf8
      database: test_test
      username: testmysql
      password: ******
      host: localhost
    
    production:
      adapter: mysql
      encoding: utf8
      database: test_production
      username: testmysql
      password: ******
      host: localhost
     运行rake
    rake db:migrate
  4. 下面我们在做了基础设施后,开始我们的具体使用吧:
    #插入必要的javascript到layout(app/views/layouts/accounts.html.erb )中
    <%= javascript_include_tag :defaults %>
    
    #得到完整的文件
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
      <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
      <title>Accounts: <%= controller.action_name %></title>
      <%= stylesheet_link_tag 'scaffold' %>
      <%= javascript_include_tag :defaults %>  
    </head>
    <body>
    
    <p style="color: green"><%= flash[:notice] %></p>
    
    <%= yield  %>
    
    </body>
    </html>
    
     
  5. 阅读插件的README我们了解到,需要给使用这个插件的Controller添加相应的配置方法
    README 写道
    Example:

    # Controller
    class BlogController < ApplicationController
    in_place_edit_for :post, :title
    end

    # View
    <%= in_place_editor_field :post, 'title' %>
     下面我们在app/controllers/accounts_controller.rb中添加需要的方法,在这里我们希望每一个字段都具有这样的特性。
    class AccountsController < ApplicationController
      Account.content_columns.each do |c|
        in_place_edit_for :account, c.name
      end
      
      # GET /accounts
      # GET /accounts.xml
      def index
        @accounts = Account.find(:all)
    
        respond_to do |format|
          format.html # index.html.erb
          format.xml  { render :xml => @accounts }
        end
      end
    
      # GET /accounts/1
      # GET /accounts/1.xml
      def show
        @account = Account.find(params[:id])
    
        respond_to do |format|
          format.html # show.html.erb
          format.xml  { render :xml => @account }
        end
      end
    
      # GET /accounts/new
      # GET /accounts/new.xml
      def new
        @account = Account.new
    
        respond_to do |format|
          format.html # new.html.erb
          format.xml  { render :xml => @account }
        end
      end
    
      # GET /accounts/1/edit
      def edit
        @account = Account.find(params[:id])
      end
    
      # POST /accounts
      # POST /accounts.xml
      def create
        @account = Account.new(params[:account])
    
        respond_to do |format|
          if @account.save
            flash[:notice] = 'Account was successfully created.'
            format.html { redirect_to(@account) }
            format.xml  { render :xml => @account, :status => :created, :location => @account }
          else
            format.html { render :action => "new" }
            format.xml  { render :xml => @account.errors, :status => :unprocessable_entity }
          end
        end
      end
    
      # PUT /accounts/1
      # PUT /accounts/1.xml
      def update
        @account = Account.find(params[:id])
    
        respond_to do |format|
          if @account.update_attributes(params[:account])
            flash[:notice] = 'Account was successfully updated.'
            format.html { redirect_to(@account) }
            format.xml  { head :ok }
          else
            format.html { render :action => "edit" }
            format.xml  { render :xml => @account.errors, :status => :unprocessable_entity }
          end
        end
      end
    
      # DELETE /accounts/1
      # DELETE /accounts/1.xml
      def destroy
        @account = Account.find(params[:id])
        @account.destroy
    
        respond_to do |format|
          format.html { redirect_to(accounts_url) }
          format.xml  { head :ok }
        end
      end
    end
    
     其中我们添加了
      Account.content_columns.each do |c|
        in_place_edit_for :account, c.name
      end
     这个方法的具体作用是为每一个字段添加一个设置属性的action,具体的定义可以参考插件中的方法描述。
  6. 在添加了相应的响应用的action后,我们需要让前台的页面(修改app/views/accounts/show.html.erb)工作起来。
    <% for c in Account.content_columns %>
      <p>
        <b>
          <%= c.human_name %>:
        </b>
        <%= in_place_editor_field :account, c.name, {} %>
      </p>
    <% end %>
    <%= link_to 'Edit', edit_account_path(@account) %> |
    <%= link_to 'Back', accounts_path %> 
  7. 需要提供一个有效的表单页面(修改app/views/accounts/new.html.erb)
    <h1>New account</h1>
    
    <% form_for(@account) do |f| %>
      <%= f.error_messages %>
      <% for column in Account.content_columns %>
        <p>
        <b><%= column.human_name %>:</b>
        <%= text_field :account, column.name %>
        </p>
      <% end %>
      <p>
        <%= f.submit "Create" %>
      </p>
    <% end %>
    
    <%= link_to 'Back', accounts_path %>
    
  8. 让我们跑起来看看效果吧。
    ruby script/server
     在浏览器中输入http://localhost:3000/accounts
  9. 按照步骤,我们点击  New account。输入相关数据
  10. 点击show.体验效果吧。

     

     
  • test.tar.gz (94.3 KB)
  • 描述: 原始测试代码和修改后的插件
  • 下载次数: 29
  • 描述: 典型应用样例
  • 大小: 99.4 KB
  • in.swf.zip (5.9 MB)
  • 描述: 一个小的效果演示视频
  • 下载次数: 20
  • 描述: 演示截图1
  • 大小: 69.3 KB
  • 描述: 演示截图2
  • 大小: 43.8 KB
  • test.zip (104.8 KB)
  • 描述: 演示样例及其源代码
  • 下载次数: 22
分享到:
评论
4 楼 wtb 2008-12-19  
本来就很简单,让楼主写复杂了(他把整个页面中的东西都贴出来了)
1、调入AJAX
<%= javascript_include_tag :defaults %> 
2、动态生成Action
Account.content_columns.each do |c|  
  in_place_edit_for :account, c.name  
end
3、在view中使用
<%= in_place_editor_field :post, 'title' %>
3 楼 duker 2008-12-18  
太复杂,让我们看看在wicket 中实现一个in place editing 是多么简单:

第一步: 导入 "in place edit" 组件

import org.apache.wicket.extensions.ajax.markup.html.AjaxEditableLabel;


  在wicket extension 项目中提供了一个 AjaxEditableLabel 组件


第二步: 在html 中需要in place edit 的地方放置一个标签

<span wicket:id="inlineEditor"  ></span>



第三步步: 创建组件并绑定model
  
String content ="点击开始编辑";
AjaxEditableLabel label = new AjaxEditableLabel("inlineEditor", new PropertyModel(this, "content")){

            @Override
            protected void onCancel(AjaxRequestTarget target) {
                super.onCancel(target);
            }

            @Override
            protected void onEdit(AjaxRequestTarget target) {
                super.onEdit(target);
            }

            @Override
            protected void onSubmit(AjaxRequestTarget target) {
                super.onSubmit(target);
            }

            @Override
            protected String getLabelAjaxEvent() {
                return "ondblclick";
            }

        };
        add(label);

that's all.
任何时候你访问content ,都是最后一次"ipe" 过的数据.如果想更改激活 "in place edit" 的事件,在 getLabelAjaxEvent 中返回相应的事件名称即可.并且在编辑启动,编辑取消,数据提交时都能得到事件处理的通知.

wicket 中的组件可以复合, 在任何使用Label 的地方都可以换成 AjaxEditableLabel,
form, ListView, Tree 等等.
2 楼 fkpwolf 2008-12-18  
用linux开发,赞...
那个画框的线好难看
1 楼 PBFox 2008-12-18  
Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

“If you’re receiving this error while using the in_place_editor_field method, chances are you’re iterating through a list of objects and you’re passing the variables to the method incorrectly.

To fix this problem, set the current iteration as an instance variable and you should be fine.

For example:

<% for person in @people %>
<% @person = person %>
<%= in_place_editor_field :person, :name %>

相关推荐

    PoisonImage.zip_editing_image editing

    之前我发了数篇系列博文来仔细研究Poisson Image Editing算法,每次重新审视和深入,仿佛都能有更为深刻的认识和很大的收获。这应该算是我这个系列的完结篇,会用用Matlab代码一点一点的演示,原文作者到底是如何...

    phx_in_place:凤凰城应用程序的内联编辑

    基于rails gem best_in_place的Phoenix框架的内联编辑包。 基于Rails gem ,phx_in_place可以通过Phoenix通道进行轻松的内联编辑。 该软件包包括一个视图帮助器,javascript事件侦听器和一个服务器端通道帮助器...

    Poisson-Image-Editing.rar_editing_poisson_poisson editing_poisso

    4. **求解波松方程**:使用数值方法(如有限差分法、松弛法或迭代法)求解波松方程。这一步会不断调整新像素值,直到能量最小化条件得到满足。 5. **融合与后处理**:将求解得到的新像素值应用到图像上,然后进行...

    patchmatch-2.1_cpp_patchmatch_editing_

    patchmatch算法和核心功能cpp实现,基于论文 Connelly Barnes et al. PatchMatch: A Randomized Correspondence Algorithm for Structural Image Editing.

    wave-tagger.zip_WAVE tagger_editing

    WAVE tagger - source program showing an example of editing the contents of audio files *. WAV (WAVE) with the ability to output information ... - Author Piette. Download (28 kb)

    gaban_regular_fonts_gaban_editing_

    标题 "gaban_regular_fonts_gaban_editing_" 暗示我们关注的是一个关于"Gabban"字体系列,特别适用于PSD(Photoshop Document)编辑的资源包。描述中提到的"gaban fonts fully working for psd editing"证实了这是一...

    poisson_image_editing.rar_editing

    4. 求解:使用数值方法(如有限差分法或迭代法)求解方程,得到新对象边缘的色彩过渡。 5. 插入:将求解出的新颜色值应用于新对象,并将其插入到原始图像的相应位置。 6. 合成:最终合成无缝融合的图像。 四、应用...

    FrameGrabber_eachkfy_Vc_editing_

    "FrameGrabber_eachkfy_Vc_editing_"这个标题可能指的是一个特定的帧捕获工具或者教程,它可能与eachkfy(可能是个人或团队名称)的Vc(Visual C++)编程相关,用于视频编辑目的。下面我们将深入探讨帧捕获技术、...

    Poisson_Image_Editing.zip_Poisson-Image_editing_poisson_poisson_

    4. **求解泊松方程**:使用数值方法(如有限差分法或迭代法)求解方程,得到新的色彩分布。这一步骤通常涉及复杂的计算,需要高效的算法和计算资源。 5. **融合图像**:根据求解得到的色彩分布,更新目标图像的像素...

    Handbook_of_Statistical_Data_Editing_and_Imputation

    《Handbook of Statistical Data Editing and Imputation》是一本关于统计学领域中数据编辑和缺失值填补技术的专业手册。本书作为Wiley调查方法系列的一部分,旨在向读者展示该领域中既有成熟的技术,也包括最新的...

    oclient_oclient官网_container_editing_

    【oclient_oclient官网_container_editing_】是一个与可视化编辑容器应用相关的项目,它涉及到的主要概念和技术集中在构建一个用户界面友好、支持容器编辑功能的应用程序上。以下将详细阐述这些知识点: 1. **容器...

    The_Eclipse_Graphical_Editing_Framework__GEF_

    标题:“The Eclipse Graphical Editing Framework (GEF)” 描述:该部分没有提供详细信息,只是提供了一个链接。 标签:源码 工具 根据给出的资料,我们可以了解到“Eclipse Graphical Editing Framework (GEF)...

    aviedit.rar_AVI_aviedit_editing

    在“aviedit.rar_AVI_aviedit_editing”这个压缩包中,我们聚焦的是AVI编辑,具体是通过使用AVIFile库中的编辑APIs来实现的。AVIFile是一个Windows API,它提供了一种统一的方式来访问和操作多种AVI文件,而无需关心...

    The_Eclipse_Graphical_Editing_Framework__GEF_.zip

    Eclipse Graphical Editing Framework(简称GEF)是Eclipse IDE中的一个重要组件,它为开发者提供了一套强大的工具,用于创建图形化的用户界面,特别是针对模型驱动的开发环境。GEF提供了一个灵活的框架,使得开发...

    poisson image editing_sjy_image_poisson_editing_

    **Poisson 图像编辑(Poisson Image Editing)** Poisson图像编辑是一种高级的图像处理技术,它主要用于在不破坏原始图像自然过渡的情况下进行图像合成和修复。该技术的核心在于利用泊松方程来解决图像像素级别的...

    傷害顯示_l1j_lineage_editing_

    "傷害顯示_l1j_lineage_editing_"这一主题,正是针对Lineage 1客户端进行编辑以优化显示伤害数值等界面元素的一个实践过程。 Lineage 1的客户端编辑涉及到多个方面,其中包括图像资源、音频文件、游戏逻辑脚本等。...

    EC0070022_O_editing.zip

    EC0070022_O_editing.zip

    Poisson_Image_Editing

    MATLAB代码中的"Poison_Image_Editing"可能包含了这些步骤的实现,通过阅读和理解代码,可以学习如何利用MATLAB进行复杂的图像处理任务。此外,该代码可能还提供了参数调整,以适应不同的应用场景和用户需求。 总的...

    傷害顯示_l1j_lineage_editing_源码.zip

    【标题】"傷害顯示_l1j_lineage_editing_源码.zip" 涉及的是一个关于在线游戏《Lineage》(血盟传奇)的伤害显示系统编辑的源代码项目。这个压缩包包含了用于修改游戏客户端中伤害数值显示功能的编程代码。《Lineage...

    3_333_editing_

    标题中的“3_333_editing_”可能是指一个关于编辑特定333系统或技术的教程或软件,而描述中的“218 ladder diagram editing software of wide data system”揭示了这可能与使用218号软件来编辑用于宽数据系统的梯形...

Global site tag (gtag.js) - Google Analytics