我正在尝试导出包含一些数据的 csv 文件。在从视图表(jqGrid)中选择一些行并将其导出为 csv 格式后,我正在进行 ajax 调用。问题是在成功调用 ajax 后,过滤后的数据显示为警告消息而不是下载选项?有任何想法吗??
查看代码:
function x() {
var obj = jQuery("input:checked").map(function() { return jQuery(this).parents('tr').attr('id'); });
var arr = jQuery.makeArray(obj);
var data = arr.join(',');
$.ajax({
url : '<%= url_for :action => "export_to_csv" %>',
type : 'POST',
data : {data:data},
dataType : 'json',
success : function(response) {
alert("Details saved successfully!!!");
},
error : function (request, status, error) {
alert(request.responseText);
}
});
}
控制器代码:
def export_to_csv
require 'fastercsv'
data = params['data'].split(',')
@gos = GoSe.find(:all, :conditions => [ "id IN (?)", data])
csv = FasterCSV.generate do |line|
cols = ["ID","Details","PartnerCode","Sece","Date","Partner","Person","TaxName","TaxID"]
line << cols
@gos.each do |entry|
line << [entry.id, entry.description, entry.code, entry.ellipsis, entry.oDate, entry.name, entry.people_name, entry.tax_name, entry.tax_id ]
end
end
send_data(csv,
:type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment; filename=go_data_#{Time.now.strftime('%d-%m-%y--%H-%M')}.csv")
end