jqgrid自定义编辑功能未定义
我刚刚开始玩 jqgrid 并遇到一个奇怪的问题?
我从 jqgrid 演示网站复制了自定义编辑示例,并添加了一个额外的按钮
我的代码:
gridComplete: function(){
var ids = jQuery("#rowed2").jqGrid('getDataIDs');
for(var i=0;i < ids.length;i++){ var cl = ids[i];
be = "<input style='height:22px;width:20px;' type='button' value='E' onclick=\"jQuery('#rowed2').editRow('"+cl+"');\" />";
se = "<input style='height:22px;width:20px;' type='button' value='S' onclick=\"jQuery('#rowed2').saveRow('"+cl+"');\" />";
ce = "<input style='height:22px;width:20px;' type='button' value='C' onclick=\"jQuery('#rowed2').restoreRow('"+cl+"');\" />";
gc = "<input style='height:22px;width:20px;' type='button' value='G' onclick=\"geocodeMe("+cl+");\" />";
jQuery("#rowed2").jqGrid('setRowData',ids[i],{Actions:be+se+ce+gc});
}
}
当我单击标记为 G 的按钮时,我应该调用我的函数,
但我收到一个 js 错误,指出 geocodeMe 未定义 它在我的代码中,所以不知道为什么它找不到它,我猜测它是一个范围问题,有什么想法如何修复它吗???
地理编码功能:
function geocodeMe(myID) {
// set default region
region = 'uk';
// address not empty
removemarkers();
$('#geo_detail').html('<label>Geocoding address...</label>');
// lookup the address
var myData = $("#rowed2").getRowData(myID);
address = myData.geoaddr_mdt;
geocoder.geocode( {'address':address,'region':region}, function(results, status) {
// if the address was found
if(status == google.maps.GeocoderStatus.OK) {
$str = '<label>Geocode Successful<br> Lattitude: '+results[0].geometry.location.lat()+' Longitude: '+results[0].geometry.location.lng()+'<br> The address is displayed below and will be stored in the database.<br> If the address is incorrect you may edit it before saving the location to the database.<br>If the marker is in the wrong location you may drag it to where you believe it should be.</label>';
$('#geo_detail').html($str);
// insert lat/long into form
$('#lat').val(results[0].geometry.location.lat());
$('#lng').val(results[0].geometry.location.lng());
// create new lat/long object
latlng = new google.maps.LatLng(results[0].geometry.location.lat(),results[0].geometry.location.lng());
$('#disp_addr').val(address);
$('#form_buttons').show();
$('#detail_address').show();
//reverselookup(results[0].geometry.location.lat(), results[0].geometry.location.lng());
// set zoom option
map.setZoom(15);
// center the map on the new location
map.setCenter(results[0].geometry.location);
createMarker(map, latlng, true, false);
if(savetype ='update'){
savedata('update');
};
if(savedata='save'){
savedata('save');
};
} else {
// display error
$('#geo_detail').append('<label>Geocoder failed to retrieve address: '+status+'</label>');
$('#disp_addr').val($('#geo_addr').val());
};
});
};
Ive just started playing with jqgrid and am experiencing a strange problem?
ive copied the custom edit example from the jqgrid demo's site and have added an extra button
my code:
gridComplete: function(){
var ids = jQuery("#rowed2").jqGrid('getDataIDs');
for(var i=0;i < ids.length;i++){ var cl = ids[i];
be = "<input style='height:22px;width:20px;' type='button' value='E' onclick=\"jQuery('#rowed2').editRow('"+cl+"');\" />";
se = "<input style='height:22px;width:20px;' type='button' value='S' onclick=\"jQuery('#rowed2').saveRow('"+cl+"');\" />";
ce = "<input style='height:22px;width:20px;' type='button' value='C' onclick=\"jQuery('#rowed2').restoreRow('"+cl+"');\" />";
gc = "<input style='height:22px;width:20px;' type='button' value='G' onclick=\"geocodeMe("+cl+");\" />";
jQuery("#rowed2").jqGrid('setRowData',ids[i],{Actions:be+se+ce+gc});
}
}
when i click the button labeled G it should i thoughr call my function
but i get a js error that geocodeMe is undefined
its in my code so dont know why it cant find it am guessing its a scope problem, any ideas how to fix it???
the geocodeMe function:
function geocodeMe(myID) {
// set default region
region = 'uk';
// address not empty
removemarkers();
$('#geo_detail').html('<label>Geocoding address...</label>');
// lookup the address
var myData = $("#rowed2").getRowData(myID);
address = myData.geoaddr_mdt;
geocoder.geocode( {'address':address,'region':region}, function(results, status) {
// if the address was found
if(status == google.maps.GeocoderStatus.OK) {
$str = '<label>Geocode Successful<br> Lattitude: '+results[0].geometry.location.lat()+' Longitude: '+results[0].geometry.location.lng()+'<br> The address is displayed below and will be stored in the database.<br> If the address is incorrect you may edit it before saving the location to the database.<br>If the marker is in the wrong location you may drag it to where you believe it should be.</label>';
$('#geo_detail').html($str);
// insert lat/long into form
$('#lat').val(results[0].geometry.location.lat());
$('#lng').val(results[0].geometry.location.lng());
// create new lat/long object
latlng = new google.maps.LatLng(results[0].geometry.location.lat(),results[0].geometry.location.lng());
$('#disp_addr').val(address);
$('#form_buttons').show();
$('#detail_address').show();
//reverselookup(results[0].geometry.location.lat(), results[0].geometry.location.lng());
// set zoom option
map.setZoom(15);
// center the map on the new location
map.setCenter(results[0].geometry.location);
createMarker(map, latlng, true, false);
if(savetype ='update'){
savedata('update');
};
if(savedata='save'){
savedata('save');
};
} else {
// display error
$('#geo_detail').append('<label>Geocoder failed to retrieve address: '+status+'</label>');
$('#disp_addr').val($('#geo_addr').val());
};
});
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在此线程中找到了答案 答案在这里
我为按钮创建了一个名为 geocode me 的类,
然后为该类创建了一个单击函数,该函数读取持有正确 ID 的 rel 属性:
found the answer in this thread Answer here
i craeted a class for the button called geocode me,
then created a click function for that class which reads in the rel attribute holding the correct id:
如果您使用
onclick=\"geocodeMe("+cl+")
,则函数geocodeMe
必须定义为 global,因此在顶层就像jQuery
一样,我建议您寻找实现相同行为的其他可能性:此处和此处此外
,您应该清楚,如果网格中有大量项目(大约 100 个或 100 个),则 for 循环中枚举
jqGrid('getDataIDs')
的所有项目可能会非常慢。更有效的方法是使用描述的 jQuery 选择 这里。更有效的方法是使用table
元素和cells 的
属性。请参阅此处 还有一个答案,其中还包括相应的演示。rows
数组任何rows
元素内的If you use
onclick=\"geocodeMe("+cl+")
, the functiongeocodeMe
must be defined as global, so on the top level likejQuery
.I recommend you to look other possibilities to implement the same behaviors: here and here.
Moreover you should be clear that enumeration through all items of
jqGrid('getDataIDs')
in for loop could be very slow in case of large number of items in the grid (about 100 or more). Much more effective way is to use jQuery selection described here. Even more effective way is the usage ofrows
array of thetable
element andcells
property inside of anyrows
element. See here one more answer which also includes corresponding demo.