2011-05-21 102 views
1

我剛開始玩jqgrid,遇到一個奇怪的問題?jqgrid自定義編輯功能undefined

香港專業教育學院從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中的按鈕,它應該我thoughr叫我的功能

但我得到一個js錯誤,geocodeMe是undefined 其在我的代碼中,所以不知道爲什麼它不能找到它我猜它的範圍問題,任何想法如何解決它?

的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()); 
      }; 
     }); 
    }; 

回答

1

發現在這個線程Answer here

我craeted名爲地理編碼我的按鈕一類的答案,

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' class='geocodeMe' rel='"+cl+"' />"; 
       jQuery("#rowed2").jqGrid('setRowData',ids[i],{Actions:be+se+ce+gc}); 
      } 

然後創建一個點擊功能該類讀取持有正確的id的rel屬性:

$('body').delegate('.geocodeMe', 'click', function() { 
     var myID = $(this).attr('rel'); 
     var myData = $("#rowed2").getRowData(myID); 
     rest of function code............. 

    }); 
+0

非常感謝你的回答,這真的很有幫助。 @Dizzy布萊恩高 – 2012-05-18 08:38:08

1

如果使用onclick=\"geocodeMe("+cl+"),功能geocodeMe必須被定義爲全球,所以在頂層像jQuery

我建議你看看其他可能性來實現相同的行爲:herehere

此外,您應該清楚,在for循環中枚舉所有項目jqGrid('getDataIDs')可能會非常緩慢(如果網格中有大量項目(大約100個或更多))。更有效的方法是使用描述here的jQuery選擇。更有效的方式是在任何rows元素內使用table元素和cells屬性的rows陣列。請參閱here多一個答案,其中還包括相應的演示。