我试图在选择州和城市时显示图表。
根据第一个下拉菜单中的州选择来填充城市下拉列表。
问题是,当我单击某个城市时,如果更改选择,则不会显示任何图表,也不会更新图表,而且似乎无法找出问题所在。
我正在使用ChartKick来显示一个饼图,其中包含控制器中查询的给定值。
我的代码如下:
controllers / city_stats_controller.rb:
def city_stats
@state = params[:state]
@cities = City.where(:state => @state).select(:id, :display_name).uniq.order('display_name ASC')
# @city_id = City.select('id').where(:state => params[:city_state]).where(:name => params[:city_name])
@city_id = City.select('id').find_by_id(params[:city])
dynamic_query = ActiveRecord::Base.send(:sanitize_sql_array, ['SELECT COALESCE(name) as name, count(*) FROM (SELECT name, regs.segment_id, count(*) FROM regs
INNER JOIN segments ON regs.segment_id = segments.id
WHERE regs.city_id = ?
GROUP BY segment_id, name
ORDER BY count(*) DESC) as tabe
GROUP BY name;', @city_id])
@spatial_ratio = ActiveRecord::Base.connection.select_all(dynamic_query)
puts @city_id.inspect #I can see the value printed in the terminal
puts @spatial_ratio.inspect #Same here, even after changing the selection
respond_to do |format|
format.json { render :json => @cities }
format.js {
render json: {
html: render_to_string(
partial: 'city_stats',
locals: {
city_id: @city_id,
spatial_ratio: @spatial_ratio
})
}
}
format.html
end
end
views / city_stats / city_stats.html.erb:
<div class="row">
<label>State <span>:</span></label>
<%= collection_select :city, :state, City.select(:state).uniq.order('state ASC'), :state, :state, {:prompt => 'Select a State'}, {id: 'city_state', multiple: false} %>
<label>City <span>:</span></label>
<%= collection_select :city, :name, [], :name, :name, {:prompt => 'Select a City'}, {id: 'city_name', multiple: false} %>
</div>
<div id="cities">
<!-- Here I render the cities list upon selecting a state-->
<%= render :partial => 'city_stats', :object => @cities %>
</div>
<div id="stats">
</div>
<script type="text/javascript">
$(document).ready(function() {
$("#city_state").on('change', function(){
$.ajax({
url: "/admin/city_stats",
type: "GET",
data: {state: $(this).val()},
success: function(data) {
$("#city_name").children().remove();
// Create options and append to the list
var listitems = [];
$.each(data,function(key, value) {
listitems += '<option value="' + value.id + '">' + value.display_name + '</option>';
});
$("#city_name").append(listitems);
//console.log(listitems);
$("#city_name").on('click', function(){
$.ajax({
url: "/admin/city_stats",
type: "GET",
data: {city: $(this).val()},
success: function(data) {
var content = $("#city_name").val()
console.log(content);
$('#stats').replaceWith("<%= j render(partial: 'city_stats') %>");
return content;
}
})
})
}
})
});
});
并在局部:views / city_stats / _city_stats.html.erb中:
<%= pie_chart @spatial_ratio.rows %>
注意:如果我手动将控制器中动态查询中的city_id替换为随机ID,则刷新页面时会显示对应城市的图表,但我不知道为什么它不能动态工作
1.更新您的city_stats方法,如下所示
def city_stats
...
...
respond_to do |format|
format.json { render :json => @cities }
format.js
end
end
2.使用以下代码创建city_stats.js.erb文件
$("#stats").html("<%= escape_javascript(pie_chart @spatial_ratio.rows) %>");
3.像这样更改您的city_click事件
$("#city_name").on('click', function(){
$.ajax({
url: "/admin/city_stats",
type: "GET",
data: {city: $(this).val()},
success: function(data) {
}
})
});
这应该可以解决您的问题。如果有任何问题/疑问
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句