Ruby Ruport实践—报表参数实现(二)

本例在 Ruby Ruport实践—报表参数实现的基础上进行改造,实现报表执行定义时报表参数的增、删、改操作

一、修改views/report_executions/edit.html.erb

不仅显示在新建报表执行时保存的报表参数,还可动态显示剩余的参数(这里控制参数最多可定义5个),这样可避免遗忘定义报表参数或需要增加报表参数的现象

<h1>Editing report_execution</h1> <% form_for(@report_execution) do |f| %> <%= f.error_messages %> <p> <%= f.label :execute_code %>: <%= f.text_field :execute_code %> </p> <p> <%= f.label :execute_name %>: <%= f.text_field :execute_name %> </p> <p> <%= f.label :report_definition_id %>: <%=report_definitions_control%> </p> <p> <%= f.label :report_template_id %>: <%=report_templates_control%> </p> <h3>Report Parameters</h3><p/> <!-- 显示已有的报表参数 --> <%if @report_parameters.length>0%> <%for i in [email protected]_parameters.length-1%> <p> <%=hidden_field_tag 'parameter_id[]',@report_parameters[i].report_parameter_id%> Parameter<%=i+1%>: <%=text_field_tag 'parameter_name[]',@report_parameters[i].parameter_name%> Value<%=i+1%>: <%=text_field_tag 'parameter_value[]',@report_parameters[i].parameter_value%> </p> <%end%> <%end%> <!--显示其他的参数框,若需要添加参数可在此操作--> <%for i in @report_parameters.length+1..5%> <p> Parameter<%=i%>: <%=text_field_tag 'parameter_other_name[]'%> Value<%=i%>: <%=text_field_tag 'parameter_other_value[]'%> </p> <%end%> <p> <%= f.submit 'Update' %> </p> <% end %> <%= link_to 'Show', @report_execution %> | <%= link_to 'Back', report_executions_path %>

二、修改ReportExecutionsController

主要修改create_report_parameter、update、destroy方法

def index @report_executions = ReportExecution.all respond_to do |format| format.html # index.html.erb format.xml { render :xml => @report_executions } end end # GET /report_executions/1 # GET /report_executions/1.xml def show @report_execution = ReportExecution.find(params[:id]) @report_definition = ReportDefinition.find(@report_execution.report_definition_id) @report_template = ReportTemplate.find(@report_execution.report_template_id) @report_parameters = ReportParameter.find_by_sql("select * from report_parameters where report_execute_id = #{params[:id]}") respond_to do |format| format.html # show.html.erb format.xml { render :xml => @report_execution } end end # GET /report_executions/new # GET /report_executions/new.xml def new @report_execution = ReportExecution.new respond_to do |format| format.html # new.html.erb format.xml { render :xml => @report_execution } end end # GET /report_executions/1/edit def edit @report_execution = ReportExecution.find(params[:id]) report_definition = ReportDefinition.find(@report_execution.report_definition_id) report_template = ReportTemplate.find(@report_execution.report_template_id) @latest_report_name = report_definition.report_name @latest_template_name = report_template.template_name @report_parameters = ReportParameter.find_by_sql("select * from report_parameters where report_execute_id = #{params[:id]}") if (@report_parameters.length>0) puts "has parameters #{@report_parameters.length}=========================" @report_parameters.each do |f| puts "#{f.parameter_name}: #{f.parameter_value}" end puts "has parameters=========================" end end def create_report_parameters(parameter_name,parameter_value) flag=true if(parameter_name) for i in 0..parameter_name.length-1 if(parameter_name[i]!="") puts "Create report parameter: #{parameter_name[i]}, #{parameter_value[i]}" report_parameter = ReportParameter.new report_parameter.write_attribute("report_execute_id",@report_execution.report_execute_id) report_parameter.write_attribute("parameter_name",parameter_name[i]) report_parameter.write_attribute("parameter_value",parameter_value[i]) if !report_parameter.save flag=false end end end end return flag end # POST /report_executions # POST /report_executions.xml def create @report_execution = ReportExecution.new(params[:report_execution]) #@report_parameter = ReportParameter.new(params[:report_parameter]) respond_to do |format| if @report_execution.save if create_report_parameters(params[:parameter_name],params[:parameter_value]) flash[:notice] = 'ReportExecution was successfully created.' end format.html { redirect_to(@report_execution) } format.xml { render :xml => @report_execution, :status => :created, :location => @report_execution } else format.html { render :action => "new" } format.xml { render :xml => @report_execution.errors, :status => :unprocessable_entity } end end end # PUT /report_executions/1 # PUT /report_executions/1.xml def update @report_execution = ReportExecution.find(params[:id]) respond_to do |format| if @report_execution.update_attributes(params[:report_execution]) flash[:notice] = 'ReportExecution was successfully updated.' if(params[:parameter_id]) for i in 0..params[:parameter_id].length-1 report_parameter = ReportParameter.find(params[:parameter_id][i]) if(params[:parameter_name][i]!="") puts "#{report_parameter.report_parameter_id} should be updated" report_parameter.update_attribute("parameter_name",params[:parameter_name][i]) report_parameter.update_attribute("parameter_value",params[:parameter_value][i]) else puts "#{report_parameter.report_parameter_id} should be deleted" report_parameter.destroy end end end create_report_parameters(params[:parameter_other_name],params[:parameter_other_value]) format.html { redirect_to(@report_execution) } format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @report_execution.errors, :status => :unprocessable_entity } end end end # DELETE /report_executions/1 # DELETE /report_executions/1.xml def destroy @report_execution = ReportExecution.find(params[:id]) @report_parameters = ReportParameter.find_by_sql("SELECT * FROM report_parameters WHERE report_execute_id = #{@report_execution.report_execute_id}") if(@report_parameters.length>0) @report_parameters.each do |report_parameter| puts "Destroy report_parameter #{report_parameter.report_parameter_id}" report_parameter.destroy end end @report_execution.destroy respond_to do |format| format.html { redirect_to(report_executions_url) } format.xml { head :ok } end end

演示效果(主要演示edit界面):

Ruby Ruport实践—报表参数实现(二)

Ruby Ruport实践—报表参数实现(二)

Ruby Ruport实践—报表参数实现(二)