我正在尝试直接从数据源将数据导出到excel文件,我可以将其导出到excel,但问题是数据没有按照我希望的方式排序,也不按照我想要的方式进行分组
无论我做什么,输出都没有按照我想要的方式排序,它与从数据源检索数据的方式相同。
示例输入数据:
|席琳| 29 |女|
|吉安| 25 |男|
|胡| 40 |男|
| Satia | 25 |女|
|居林| 50 |男|
|利兹| 15 |女|
excel中的示例输出:
|利兹| 15 |女|
| Satia | 25 |女|
|席琳| 29 |女|
|吉安| 25 |男|
|胡| 40 |男|
|居林| 50 |男|
以下是我使用的一段代码:
$(".export-excel").click(function () {
var from = new Date($("input#startDate").val()), to = new Date($("input#endDate").val());
var ds = new kendo.data.DataSource({
type: "odata",
transport: {
read: {
url: webApiUrl +
"/data/getdata(startDateString='" +
(from.getFullYear() + "-" + (from.getMonth() + 1) + "-" + from.getDate()) +
"', endDateString='" + (to.getFullYear() + "-" + (to.getMonth() + 1) + "-" + to.getDate()) + "')",
dataType: "json"
},
parameterMap: function (options, data) {
var paramMap = kendo.data.transports.odata.parameterMap(options);
var d = kendo.data.transports.odata.parameterMap(data);
delete paramMap.$inlinecount;
delete paramMap.$format;
paramMap.$count = true;
return paramMap;
}
},
schema: {
data: function (data) {
return data.value;
},
total: function (data) {
return data['odata.count'];
},
errors: function (data) {
},
model: {
fields: {
Name: { type: "string" },
Age: { type: "string" },
Gender: {type:"string"}
}
},
group: {
field: "Gender"
}
},
sort: {
field: "Gender",
dir: "asc"
},
sortable: true,
pageable: true,
groupable: true,
filterable: true,
});
ds.fetch(function () {
var data = this.data();
for (var i = 0; i < data.length; i++) {
rows.push({
cells: [
{ value: data[i].Name },
{ value: data[i].Age},
{ value: data[i].Gender}
]
})
}
var workbook = new kendo.ooxml.Workbook({
sheets: [
{
columns: [
// Column settings (width)
{ autoWidth: true },
{ autoWidth: true },
{ autoWidth: true }
],
title: "Peaple_Report"
}
]
});
//save the file as Excel file with extension xlsx
kendo.saveAs({ dataURI: workbook.toDataURL(), fileName: "Test.xlsx" });
});
});
先谢谢你。
答案 0 :(得分:0)
.data()方法在未排序或过滤时保存原始数据...
当您应用任何过滤器或排序或分组或分页时...您应该从.view()方法中读取数据...这将为您提供您正在寻找的已排序/已过滤数据...
这里是.view()方法的文档 - http://docs.telerik.com/kendo-ui/api/javascript/data/datasource#methods-view