gpt4 book ai didi

javascript - MethodName 不是 Javascript 中的函数

转载 作者:行者123 更新时间:2023-11-28 06:07:08 24 4
gpt4 key购买 nike

loadComplete 方法的以下代码中,它给了我这个错误:

this.getColumnIndexByName is not a function

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;
},
};

最佳答案

您需要在正确的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');

关于javascript - MethodName 不是 Javascript 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36738886/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com