【问题标题】:how to change view's vairable in js of rails如何在rails的js中更改视图变量
【发布时间】:2016-10-22 14:01:28
【问题描述】:

如何在 javascript 中更改视图的变量值。我的意思是当我从下拉表中选择四月时,将只显示四月的数据,当前表数据将消失并刷新。我该怎么做?帮我
我的 JS

:javascript
    var updateMonth = function(){
        var month_id = $("#select_other_month").val();
        console.log(month_id)
    }

这是我的选择标签(下拉菜单)

%select{:name => "options", :id=>"select_other_month",:onchange=>"updateMonth()"}
            %option.placeholder{:disabled => "", :selected => "", :value => ""} see other months
            %option{:value => "0"} this month
            -a=@this_month - 1.month
            %option{:value => "5"}=a.strftime('%Y-%m')
            -b=@this_month - 2.month
            %option{:value => "4"}=b.strftime('%Y-%m')
            -c=@this_month - 3.month
            %option{:value => "3"}=c.strftime('%Y-%m')
            -d=@this_month - 4.month
            %option{:value => "2"}=d.strftime('%Y-%m')
            -e=@this_month - 5.month
            %option{:value => "1"}=e.strftime('%Y-%m')

我的桌子是这样的

.table{:id=>"time_table"}
        %table{:border=>"1"}
            %thead
                %tr
                    %th No
                    %th Name
                    -(@xmonth.at_beginning_of_month..@xmonth.at_end_of_month).each do |day|
                        -if (day.saturday? || day.sunday?)==false
                            %th=day.strftime("%d")
                    %th Total days
                    %th Total time

我想从 JS 更改我的 @xmonth 变量
注意:@this_month = Date.today

【问题讨论】:

  • 您想从服务器获取新数据并进行填充?
  • 是的,我是。我现在该怎么办?

标签: javascript jquery ruby-on-rails haml


【解决方案1】:
var updateMonth = function(){
  var month_id = $("#select_other_month").val();
  $.ajax({
    url: '/your_controller/its_method', //make sure to add this path to routes file
    data: {
      month_id: month_id
    },
    success: function(response){
      $("#target_area").html(response);
    },
    error: function(error_res){
      console.log(error_res);
    }
  });
}

在您的视图文件中添加一个 id,以便我们可以将其内容替换为我们得到的新响应

%thead{:id => "target_area"}
  %tr
    %th No
    %th Name
    ...

在你的控制器中

class YourController
   def your_method
     month_id = params[:month_id]
     #update your @xmonth by month_id
     #your new information should be inside @xmonth object
     respond_to do |format|
       format.html {render partial: 'your_controller/your_method/your_partial.haml.erb' }
     end
   end
end

使用需要替换的内容创建您的部分文件_your_partial.haml.erb

%tr
  %th No
  %th Name
  -(@xmonth.at_beginning_of_month..@xmonth.at_end_of_month).each do |day|
    -if (day.saturday? || day.sunday?)==false
  %th=day.strftime("%d")
  %th Total days
  %th Total time

当您收到成功响应时,部分内容将在您的视图中替换为 #target_area 的内容。请参阅 updateMonth 函数。

希望对你有帮助

【讨论】:

  • 感谢您回答我的问题。但我对你的建议有意见。错误信息是关于我的问题的。
  • @NeoTeo 在渲染内部format.html 的部分时不要使用_,因为它会自动为您添加。
  • 太棒了!对不起,如果我在任何地方写错了haml。不使用haml。
  • 没事没问题。谢谢你:)
猜你喜欢
  • 1970-01-01
  • 2018-01-21
  • 2016-03-06
  • 2021-06-29
  • 2022-01-27
  • 1970-01-01
  • 2019-05-11
  • 1970-01-01
  • 2014-09-13
相关资源
最近更新 更多