Javascript 使用KendoGrid抛出错误将隐藏字段导出到Excel
我有一个按钮导出Excel:Javascript 使用KendoGrid抛出错误将隐藏字段导出到Excel,javascript,c#,kendo-grid,export-to-excel,kendo-asp.net-mvc,Javascript,C#,Kendo Grid,Export To Excel,Kendo Asp.net Mvc,我有一个按钮导出Excel: <script type="text/x-kendo-template" id="template"> <div class="toolbar"> <button id="ExportExcel" class="btn btn-default " onclick="ExportExcel(); return false;"><i class="fa fa-file-excel-o"
<script type="text/x-kendo-template" id="template">
<div class="toolbar">
<button id="ExportExcel" class="btn btn-default " onclick="ExportExcel(); return false;"><i class="fa fa-file-excel-o"></i> Exportar</button>
</div>
</script>
在下面,我像这样填充网格:
function ExportExcel() {
$("#lstEmployees").data("kendoGrid").saveAsExcel(); // export grid to excel
}
function onPageOperationsSuccess(response) {
var data = response;
var dataSource = new kendo.data.DataSource({
data: data,
pageSize: 10
});
$("#lstEmployees").kendoGrid({
dataSource: {
data: response,
schema: {
model: {
fields: {
Id: { type: "int" },
Code: { type: "string" },
Name: { type: "string" },
Department: { type: "string" },
Area: { type: "string" },
Occupation: { type: "string" },
AdmissionDate: { type: "date" }
}
}
},
pageSize: 10
},
height: 630,
toolbar: kendo.template($("#template").html()),
scrollable: true,
sortable: true,
rezisable: true,
pageable: {
input: true,
numeric: false
},
columns: [
{ field: "Code", title: "No empleado", width: "130px" },
{ field: "Name", title: "Nombre", width: "150px", },
{ field: "Department", title: "Departamento", width: "120px" },
{ field: "Area", title: "Área", width: "120px" },
{ field: "Occupation", title: "Ocupación", width: "120px" },
{ field: "AdmissionDate", title: "Fecha de ingreso", width: "120px", format: '{0:dd/MM/yyyy}' },
{
field: "", title: "Acciones", width: "120px",
template: function (item) {
var id = item.Id;
var dropbox;
dropbox = "<div class='actions'><div class='btn-group'><a class='btn btn-mini dropdown-toggle' data-toggle='dropdown'><i class='fa fa-cog'></i> <span class='caret'></span></a><ul class='dropdown-menu pull-right'><li><a href='javascript:loadModalEditCreate(" + id + ");' role='button' data-toggle='modal'><i class='fa fa-pencil-square-o'></i> Editar</a></li></ul></div></div>";
return dropbox;
}
}
]
});
和字段列,如:
{ field: "Sexo", title:"Sexo", width:"150px", hidden: true},
嗯,我读到我需要调用函数来读取隐藏字段并导出到excel,所以现在,在网格填充下面,我同意以下代码:
function onPageOperationsSuccess(response) {
var data = response;
var dataSource = new kendo.data.DataSource({
data: data,
pageSize: 10
});
$("#lstEmployees").kendoGrid({
dataSource: {
data: response,
schema: {
model: {
fields: {
Id: { type: "int" },
Code: { type: "string" },
Name: { type: "string" },
Sexo: { type: "string"},
Department: { type: "string" },
Area: { type: "string" },
Occupation: { type: "string" },
AdmissionDate: { type: "date" }
}
}
},
pageSize: 10
},
height: 630,
toolbar: kendo.template($("#template").html()),
scrollable: true,
sortable: true,
rezisable: true,
pageable: {
input: true,
numeric: false
},
columns: [
{ field: "Code", title: "No empleado", width: "130px" },
{ field: "Name", title: "Nombre", width: "150px", },
{ field: "Sexo", title:"Sexo", width:"150px", hidden: true},
{ field: "Department", title: "Departamento", width: "120px" },
{ field: "Area", title: "Área", width: "120px" },
{ field: "Occupation", title: "Ocupación", width: "120px" },
{ field: "AdmissionDate", title: "Fecha de ingreso", width: "120px", format: '{0:dd/MM/yyyy}' },
{
field: "", title: "Acciones", width: "120px",
template: function (item) {
var id = item.Id;
var dropbox;
dropbox = "<div class='actions'><div class='btn-group'><a class='btn btn-mini dropdown-toggle' data-toggle='dropdown'><i class='fa fa-cog'></i> <span class='caret'></span></a><ul class='dropdown-menu pull-right'><li><a href='javascript:loadModalEditCreate(" + id + ");' role='button' data-toggle='modal'><i class='fa fa-pencil-square-o'></i> Editar</a></li></ul></div></div>";
return dropbox;
}
}
]
});
var exportFlag = false;
$("#lstEmployees").data("kendoGrid").bind("ExportExcel", function (e) {
if (!exportFlag) {
// e.sender.showColumn(0); for demo
// for your case show column that you want to see in export file
e.sender.showColumn(1);
e.preventDefault();
exportFlag = true;
setTimeout(function () {
e.sender.saveAsExcel();
});
} else {
e.sender.hideColumn(5);
e.sender.hideColumn(6);
exportFlag = false;
}
});
$("#searchbox").keyup(function () {
var val = $('#searchbox').val();
$("#lstEmployees").data("kendoGrid").dataSource.filter({
logic: "or",
filters: [
{
field: "Code",
operator: "contains",
value: val
},
{
field: "Name",
operator: "contains",
value: val
},
{
field: "Department",
operator: "contains",
value: val
},
{
field: "Area",
operator: "contains",
value: val
},
{
field: "Occupation",
operator: "contains",
value: val
}
]
});
});
}
功能在页面操作成功(响应){
var数据=响应;
var dataSource=new kendo.data.dataSource({
数据:数据,
页面大小:10
});
$(“#lstmployees”).kendoGrid({
数据源:{
数据:答复,
模式:{
型号:{
字段:{
Id:{type:“int”},
代码:{type:“string”},
名称:{type:“string”},
Sexo:{type:“string”},
部门:{type:“string”},
区域:{type:“string”},
职业:{type:“string”},
接纳日期:{type:“date”}
}
}
},
页面大小:10
},
身高:630,
工具栏:kendo.template($(“#template”).html(),
可滚动:对,
可排序:是的,
雷齐塞布尔:是的,
可分页:{
输入:正确,
数字:false
},
栏目:[
{字段:“代码”,标题:“无雇员”,宽度:“130px”},
{字段:“名称”,标题:“Nombre”,宽度:“150px”},
{字段:“Sexo”,标题:“Sexo”,宽度:“150px”,隐藏:true},
{字段:“部门”,标题:“部门”,宽度:“120px”},
{字段:“区域”,标题:“Área”,宽度:“120px”},
{字段:“职业”,标题:“Ocupación”,宽度:“120px”},
{字段:“受理日期”,标题:“Fecha de Ingreo”,宽度:“120px”,格式:{0:dd/MM/yyyy},
{
字段:,标题:“Acciones”,宽度:“120px”,
模板:功能(项目){
var id=item.id;
var下拉框;
dropbox=“”;
返回下拉框;
}
}
]
});
var exportFlag=false;
$(“#lstmemployees”).data(“kendoGrid”).bind(“ExportExcel”,函数(e){
如果(!exportFlag){
//e.sender.showColumn(0);用于演示
//对于要在导出文件中看到的案例显示列
e、 发送者。显示列(1);
e、 预防默认值();
exportFlag=true;
setTimeout(函数(){
e、 sender.saveAsExcel();
});
}否则{
e、 寄件人:hideColumn(5);
e、 寄件人:hideColumn(6);
exportFlag=false;
}
});
$(“#搜索框”).keyup(函数(){
var val=$(“#搜索框”).val();
$(“#lstmemployees”).data(“kendoGrid”).dataSource.filter({
逻辑:“或”,
过滤器:[
{
字段:“代码”,
操作员:“包含”,
值:val
},
{
字段:“名称”,
操作员:“包含”,
值:val
},
{
字段:“部门”,
操作员:“包含”,
值:val
},
{
字段:“区域”,
操作员:“包含”,
值:val
},
{
字段:“职业”,
操作员:“包含”,
值:val
}
]
});
});
}
但我在Google Chrome Inspector中遇到了一个问题:
VM951 AdminEmployee:1未捕获类型错误:ExportExcel不是
作用
有谁能帮我一下哪里出了问题?关于是否在网格范围内。是否在ExportExcel()之后定义了网格onPageOperationsSuccess()?我上载了所有onPageOperationsSuccess,现在我遇到了问题
VM951 AdminEmployee:1未捕获类型错误:ExportExcel不是一个函数
@RossBush@Jesus a。我的意思是,在源代码文件和工作版本中,是否在网格函数之前定义了导出函数(没有隐藏字段),ExportExcel是在onPageOperationsSuccess()之后定义的,我将它添加到我的问题@Rossbush中只是为了测试,更改顺序并将ExportExcel放在部分的顶部,看看它是否工作。它是否在网格的范围内。是否在ExportExcel()之后定义了网格onPageOperationsSuccess()?我上传了所有onPageOperationsSuccess,现在我收到了问题VM951 AdminEmployee:1未捕获类型错误:ExportExcel不是一个函数
@RossBush@Jesus a。我的意思是在源代码文件中是否在网格函数之前定义了导出函数?在工作版本(没有隐藏字段)中,ExportExcel是在onPageOperationsSuccess之后定义的(),我把它添加到我的问题@Rossbush中只是为了测试,改变顺序,将ExportExcel放在部分的顶部,看看它是否有效。
function onPageOperationsSuccess(response) {
var data = response;
var dataSource = new kendo.data.DataSource({
data: data,
pageSize: 10
});
$("#lstEmployees").kendoGrid({
dataSource: {
data: response,
schema: {
model: {
fields: {
Id: { type: "int" },
Code: { type: "string" },
Name: { type: "string" },
Sexo: { type: "string"},
Department: { type: "string" },
Area: { type: "string" },
Occupation: { type: "string" },
AdmissionDate: { type: "date" }
}
}
},
pageSize: 10
},
height: 630,
toolbar: kendo.template($("#template").html()),
scrollable: true,
sortable: true,
rezisable: true,
pageable: {
input: true,
numeric: false
},
columns: [
{ field: "Code", title: "No empleado", width: "130px" },
{ field: "Name", title: "Nombre", width: "150px", },
{ field: "Sexo", title:"Sexo", width:"150px", hidden: true},
{ field: "Department", title: "Departamento", width: "120px" },
{ field: "Area", title: "Área", width: "120px" },
{ field: "Occupation", title: "Ocupación", width: "120px" },
{ field: "AdmissionDate", title: "Fecha de ingreso", width: "120px", format: '{0:dd/MM/yyyy}' },
{
field: "", title: "Acciones", width: "120px",
template: function (item) {
var id = item.Id;
var dropbox;
dropbox = "<div class='actions'><div class='btn-group'><a class='btn btn-mini dropdown-toggle' data-toggle='dropdown'><i class='fa fa-cog'></i> <span class='caret'></span></a><ul class='dropdown-menu pull-right'><li><a href='javascript:loadModalEditCreate(" + id + ");' role='button' data-toggle='modal'><i class='fa fa-pencil-square-o'></i> Editar</a></li></ul></div></div>";
return dropbox;
}
}
]
});
var exportFlag = false;
$("#lstEmployees").data("kendoGrid").bind("ExportExcel", function (e) {
if (!exportFlag) {
// e.sender.showColumn(0); for demo
// for your case show column that you want to see in export file
e.sender.showColumn(1);
e.preventDefault();
exportFlag = true;
setTimeout(function () {
e.sender.saveAsExcel();
});
} else {
e.sender.hideColumn(5);
e.sender.hideColumn(6);
exportFlag = false;
}
});
$("#searchbox").keyup(function () {
var val = $('#searchbox').val();
$("#lstEmployees").data("kendoGrid").dataSource.filter({
logic: "or",
filters: [
{
field: "Code",
operator: "contains",
value: val
},
{
field: "Name",
operator: "contains",
value: val
},
{
field: "Department",
operator: "contains",
value: val
},
{
field: "Area",
operator: "contains",
value: val
},
{
field: "Occupation",
operator: "contains",
value: val
}
]
});
});
}