如何在 rails 的 js 中更改视图变量
how to change view's vairable in js of rails
如何在 javascript 中更改视图的变量值。我的意思是,当我 select April from dropdown table 时,将仅显示 4 月的数据,而当前的 table 数据将消失并刷新。我该怎么做?帮我
我的 JS
:javascript
var updateMonth = function(){
var month_id = $("#select_other_month").val();
console.log(month_id)
}
这是我的 select 标签(下拉菜单)
%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 看起来像这样
.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
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 函数。
希望对您有所帮助
如何在 javascript 中更改视图的变量值。我的意思是,当我 select April from dropdown table 时,将仅显示 4 月的数据,而当前的 table 数据将消失并刷新。我该怎么做?帮我
我的 JS
:javascript
var updateMonth = function(){
var month_id = $("#select_other_month").val();
console.log(month_id)
}
这是我的 select 标签(下拉菜单)
%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 看起来像这样
.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
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 函数。
希望对您有所帮助