在loadComplete
方法的以下代码中,它给出了我的错误:
this.getColumnIndexByName不是函数
dataGrid.prototype = {
display: function() {
var html = [];
var check = 0;
html.push("<table id='" + this.id + "" + "'class='table'>\n</table>");
$('body').append(html.join(""));
$("#" + this.id).jqGrid({
url: "index.jsp",
styleUI: 'Bootstrap',
datatype: "local",
data: this.data,
colModel: this.getColModels(this.data[0]),
viewrecords: true,
width: 1300,
height: 250,
rowNum: 50,
loadComplete: function() {
var iCol = this.getColumnIndexByName('Enable');
var rows = $("#" + this.id).jqGrid('getGridParam', 'records');
var i;
for (i = 0; i < rows; i++) {
$(rows[i].cells[iCol]).click(function(e) {
var id = $(e.target).closest('tr')[0].id,
isChecked = $(e.target).is(':checked');
alert("checked:" + isChecked);
//you can also get the values of the row data
alert('clicked on the checkbox in the row with id=' + id + '\nNow the checkbox is ' + (isChecked ? 'checked' : 'not checked'));
});
}
}
});
},
getColNames: function(data) {
var keys = [];
for (var key in data) {
if (data.hasOwnProperty(key)) {
keys.push(key);
}
}
return keys;
},
getColModels: function(data) {
var colNames = this.getColNames(data);
var colModelsArray = [];
var str2;
for (var i = 0; i < colNames.length; i++) {
var str1;
str1 = {
name: colNames[i],
index: colNames[i],
};
colModelsArray.push(str1);
}
str2 = {
name: 'Enable',
index: 'Enable',
};
colModelsArray.push(str2);
return colModelsArray;
},
getColumnIndexByName: function(columnName) {
var cm = $("#" + this.id).jqGrid('getGridParam', 'colModel'),
i, l;
for (i = 0, l = cm.length; i < l; i += 1) {
if (cm[i].name === columnName) {
return i;
}
}
return -1;
},
};
答案 0 :(得分:0)
您需要在正确的this
dataGrid.prototype = {
// ...
display : function() {
var me = this; // Keep a reference to the dataGrid `this`
// ...
$("#" + me.id).jqGrid({
// ...
loadComplete: function () {
var iCol = me.getColumnIndexByName('Enable');
答案 1 :(得分:0)
因为loadComplete
内部没有引用该对象,所以此行为称为closures
看到其他有趣的文章 Stackoverflow self = this
您必须将this
引用保存到另一个变量并使用它:
dataGrid.prototype = {
display : function() {
var self = this; // Save this reference
var html = [];
var check = 0;
html.push("<table id='"+this.id+"" + "'class='table'>\n</table>");
$('body').append(html.join(""));
$("#" + this.id).jqGrid({
url : "index.jsp",
styleUI : 'Bootstrap',
datatype : "local",
data : this.data,
colModel : this.getColModels(this.data[0]),
viewrecords : true,
width : 1300,
height : 250,
rowNum : 50,
loadComplete: function () {
//use selft reference
var iCol = self.getColumnIndexByName('Enable');
var rows = $("#" + self.id).jqGrid('getGridParam', 'records');
var i;
for (i = 0; i < rows; i ++) {
$(rows[i].cells[iCol]).click(function (e) {
var id = $(e.target).closest('tr')[0].id, isChecked = $(e.target).is(':checked');
alert("checked:" + isChecked);
//you can also get the values of the row data
alert('clicked on the checkbox in the row with id=' + id + '\nNow the checkbox is ' + (isChecked ? 'checked' : 'not checked'));
});
}
}
});
},
getColNames : function(data) {
var keys = [];
for ( var key in data) {
if (data.hasOwnProperty(key)) {
keys.push(key);
}
}
return keys;
},
getColModels : function(data) {
var colNames = this.getColNames(data);
var colModelsArray = [];
var str2;
for (var i = 0; i < colNames.length; i++) {
var str1;
str1 = {
name : colNames[i],
index : colNames[i],
};
colModelsArray.push(str1);
}
str2 = {
name : 'Enable',
index : 'Enable',
};
colModelsArray.push(str2);
return colModelsArray;
},
getColumnIndexByName : function (columnName) {
var cm = $("#" + this.id).jqGrid('getGridParam', 'colModel'), i, l;
for (i = 0, l = cm.length; i < l; i += 1) {
if (cm[i].name === columnName) {
return i;
}
}
return -1;
},
};