【问题标题】:Update jQuery Datatables cell with geocoded address使用地理编码地址更新 jQuery 数据表单元格
【发布时间】:2012-07-10 16:39:18
【问题描述】:

我有一个显示我的数据的 jquery 数据表(见下文)。您会注意到最后一列是地址,而行数据只是表示正在加载。

我想使用数据中的经度和纬度从 google maps api 获取地址。

我知道获取地址可能需要一些时间,如果返回 3000 个结果将需要很长时间,这就是为什么我没有通过数据中的预地理编码地址。

显示表格数据后,我可以开始对每一行的纬度/经度进行地理编码,并在每一行完成后更新地址列吗?

我也在使用谷歌地图的 GMAP3 jQuery 插件,所以我可以得到这样的地址:

        // get address
        $("#map").gmap3({
          action: 'getAddress',
          latLng: [lati, longi],
          callback: function (results) {
            content = results && results[1] ? results && results[1].formatted_address : 'No Address';
          } // end callback

        });

更新

几乎完成了 :) 这些类被添加到纬度、经度和地址,我正在将地理编码的地址接收到控制台中。我需要做的就是将地址放入表中的正确行。我该怎么做?

我的代码 var historyArray = window.opener.historyArray;

 $(document).ready(function() {
            //$('#dynamic').html( '<table cellpadding="0" cellspacing="0" border="0" class="display" id="report"></table>' );
            $('#report').dataTable( {
                "aaData": historyArray,
                "aoColumns": [
                    { "mDataProp": "User" },
                    { "mDataProp": "Timestamp" },
                    { "mDataProp": "Latitude" },
                    { "mDataProp": "Longitude" },
                    { "mDataProp": "Address" }
                ],
                "iDisplayLength": 25,
                "bJQueryUI": true,
                "sPaginationType": "full_numbers",
                "sDom": '<"H"Tfr>t<"F"ip>',
                "oTableTools": {
                            "sSwfPath": "swf/copy_csv_xls_pdf.swf",
                            "aButtons": ["copy", "csv", "xls", "pdf"]
                                      },
                "fnInitComplete": function () {
                    getAdresses();
                }
            } );    

$("#report td:nth-child(3)").addClass("lat");
$("#report td:nth-child(4)").addClass("lng");
$("#report td:nth-child(5)").addClass("addi");
} );

 function getAdresses() {
   $.each(historyArray, function (index,data) {
    // get address
    var map = window.opener.document.getElementById('dispatcher');
    $(map).gmap3({
      action: 'getAddress',
      latLng: [data.Latitude, data.Longitude],
      callback: function (results) {
        content = results && results[1] ? results && results[1].formatted_address : 'No Address';
        console.log(content);
      } // end callback

    });
   });
 }

更新 2

嗨,更新的代码。 因为我添加了 $(this).html(content);在 gmap3 的回调中,它没有更新“addi”单元格,我该如何解决?

 function getAdresses() {
   $(".addi").each(function () {
     var lat = $(this).siblings(".lat").html().toString();
     var lng = $(this).siblings(".lng").html().toString();
      // get address
      $(map).gmap3({
        action: 'getAddress',
        latLng: [lat, lng],
        callback: function (results) {
          content = results && results[1] ? results && results[1].formatted_address : 'No Address';
          $(this).html(content);
        } // end callback

      });
   });
 }

最终更新 添加到解决方案中。 我发现当我将数据导出到 CSV、Excel 等时,地址数据没有更新。

我把代码修改成了这个。

       myTable = $('#report').dataTable({
     "aaData": historyArray,
     "aoColumns": [{
       "mDataProp": "User"
     }, {
       "mDataProp": "Timestamp"
     }, {
       "mDataProp": "Latitude"
     }, {
       "mDataProp": "Longitude"
     }, {
       "mDataProp": "Address"
     }],
     "bPaginate": false,
     "bJQueryUI": true,
     "sPaginationType": "full_numbers",
     "sDom": '<"H"Tfr>t<"F"ip>',
     "oTableTools": {
       "sSwfPath": "swf/copy_csv_xls_pdf.swf",
       "aButtons": ["copy", "csv", "xls", "pdf"]
     },
     "fnInitComplete": function () {
       addClasses();
     }
   });
   window.setTimeout(function () {
     getAdresses();
   }, 1000);
 });

 function addClasses() {
   $("#report td:nth-child(3)").addClass("lat");
   $("#report td:nth-child(4)").addClass("lng");
   $("#report td:nth-child(5)").addClass("addi");
 }

 function getAdresses() {
   $(".addi").each(function () {
     var lat = $(this).siblings(".lat").html().toString();
     var lng = $(this).siblings(".lng").html().toString();
     var addi = $(this);
     $(addi).html("Reverse geocoding..");
     var aPos = myTable.fnGetPosition(this);

     // get address
     $('#hidden').gmap3({
       action: 'getAddress',
       latLng: [lat, lng],
       callback: function (results) {
         content = results && results[1] ? results && results[1].formatted_address : 'No Address';
         myTable.fnUpdate(content, aPos[0], 4);
       } // end callback

     });
   });
 }

现在数据真正更新了。

【问题讨论】:

    标签: jquery datatables


    【解决方案1】:

    如果我理解正确:

    您可以像这样通过 jQuery 将类添加到最后一列:

    $("#table td:nth-child(5)").addClass("someClass");

    然后你可以创建一个在表初始化后运行的函数:

    //Part of the datatable initialization
    "fnInitComplete": function () {
        myAwesomeFunction();
    }
    

    在该函数中,您可以根据类执行 .each() 并获取兄弟姐妹(经纬度)将它们传递给 api 并使用结果执行 .html() 以替换加载文本。 我希望这是有道理的。如果不让我知道:)

    //编辑//

    要抓住兄弟姐妹: 先给他们上课

    $("#table td:nth-child(3)").addClass("lat");

    $("#table td:nth-child(4)").addClass("long");

    请记住,您必须在初始化数据表后应用所有这些样式。

    那么你可以:

    var lat = $(this).siblings(".lat").html().toString();
    var long = $(this).siblings(".long").html().toString();
    

    //////////// ///编辑2 /// /////////////

    先移动这段代码:

    $("#report td:nth-child(3)").addClass("lat");
    $("#report td:nth-child(4)").addClass("lng");
    $("#report td:nth-child(5)").addClass("addi");
    

    像这样的函数:

    $("#report").on("draw", function() {
       addClasses();
    });
    
    function addClasses() {
        $("#report td:nth-child(3)").addClass("lat");
        $("#report td:nth-child(4)").addClass("lng");
        $("#report td:nth-child(5)").addClass("addi");
    }
    

    并将addClasses() 函数添加到fnInitcomplete 之前 getAddresses()

    这样,如果您更改页面或排序或对表格进行其他更改,则将应用/重新应用这些类。

    我不确定地理数据的 API 是如何工作的,但为了抓住长拉特我会做这样的事情:

        $(".addi").each(function () {
            var lat = $(this).siblings(".lat").html().toString();
            var lng = $(this).siblings(".lng").html().toString();
            // Now pass these parameters to the geocode function and store the result in some var
            //In this case lets call it result
            //Now just replace the loading with the result:
            $(this).html(result);
        });
    

    将上面的代码放在getAddresses() 函数中,它应该会给你结果。

    让我知道这是否足够以及是否可以正常工作;)

    【讨论】:

    • 嗨,是的,我理解并会试一试,我可能需要更多关于如何抓住兄弟姐妹的帮助。
    • @Iro 好的,我已经做到了,谢谢。请参阅我的更新 2 我还有一个问题要克服 :) 感谢您的帮助
    • 好的,我已经通过 var addi = $(this);然后 $(addi).html(content);,不确定这是否是最好的方式?
    • 可能不是,但我不明白为什么原来的不起作用。我必须检查一下。
    猜你喜欢
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 2016-09-25
    • 2013-09-28
    • 2021-07-19
    • 2017-10-06
    • 1970-01-01
    • 2010-09-18
    相关资源
    最近更新 更多