【问题标题】:Adding multiple statements to function将多个语句添加到函数中
【发布时间】:2015-03-27 08:56:02
【问题描述】:

我得到这样的数据:

$.ajax({
            url: 'php/get_all_attri.php',
            type: 'GET',
            dataType: 'JSON',
            data: {
                col_name: firstSel
            },
            success: function(data) {
                var total_statement = {};
               $.each(data, function(i, data) {
                console.log(data.attri_1);
            });

                }
            });

在控制台中,这会返回我的查询的可能值,例如 18 19 20 21 等。

它们也可以是 TRUE FALSE 之类的字符串。

我需要这些值来创建用于使用 cartoDB/leaflet 构建地图的语句。函数如下:

function goMap3() {

        var layerSource = {
            user_name: 'me',
            type: 'cartodb',
            sublayers: [{
                sql: "SELECT cartodb_id, the_geom_webmercator FROM mytable where attribute='" + attr_value + "'",
                cartocss: "#location_fusion_table {marker-fill:#1d5492;marker-width:5;marker-line-width: 0.3;marker-line-color:#fff;marker-allow-overlap: true;"}
            }, ]
        };
        cartodb.createLayer(map_object, layerSource, options)
            .addTo(map_object);
    }

我需要做的是让整个部分写入尽可能多的次数,因为我的 AJAX 返回了不同的值。

          {
                sql: "SELECT cartodb_id, the_geom_webmercator FROM mytable where attribute='" + attr_value + "'",
                cartocss: "#location_fusion_table {marker-fill:#1d5492;marker-width:5;marker-line-width: 0.3;marker-line-color:#fff;marker-allow-overlap: true;"}
            },

在这种情况下,attr_value 每次都需要用来自 AJAX 的值替换。这将使我的整个函数看起来像这样:

function goMap3() {

        var layerSource = {
            user_name: 'me',
            type: 'cartodb',
            sublayers: [{
                sql: "SELECT cartodb_id, the_geom_webmercator FROM mytable where attribute='17'",
                cartocss: "#location_fusion_table {marker-fill:#1d5492;marker-width:5;marker-line-width: 0.3;marker-line-color:#fff;marker-allow-overlap: true;"}
            }, 
  {
                sql: "SELECT cartodb_id, the_geom_webmercator FROM mytable where attribute='18'",
                cartocss: "#location_fusion_table {marker-fill:#1d5492;marker-width:5;marker-line-width: 0.3;marker-line-color:#fff;marker-allow-overlap: true;"}
            },
  {
                sql: "SELECT cartodb_id, the_geom_webmercator FROM mytable where attribute='19'",
                cartocss: "#location_fusion_table {marker-fill:#1d5492;marker-width:5;marker-line-width: 0.3;marker-line-color:#fff;marker-allow-overlap: true;"}
            },]
        };
        cartodb.createLayer(map_object, layerSource, options)
            .addTo(map_object);
    }

总而言之:我试图弄清楚我最终是如何得到我的问题中的最后一个函数的。我需要制作一些“子层”以匹配从我的 AJAX 返回的值的数量以及在子层语句中填充正确区域的值。

【问题讨论】:

  • 我不太清楚你在问什么..
  • 我试图弄清楚我最终是如何得到我的问题中的最后一个函数的。我需要制作一些“子层”以匹配从我的 AJAX 返回的值的数量以及填充在子层语句中正确区域的值。

标签: javascript jquery ajax cartodb


【解决方案1】:

这些不是多个语句,它们只是数组的元素。您可以在$.each() 循环中简单地添加到数组中:

function goMap3(data) {

    var sublayers = [];
    $.each(data, function(i, attr_value) {
        sublayers.push({
            sql: "SELECT cartodb_id, the_geom_webmercator FROM mytable where attribute='" + attr_value + "'",
            cartocss: "#location_fusion_table {marker-fill:#1d5492;marker-width:5;marker-line-width: 0.3;marker-line-color:#fff;marker-allow-overlap: true;"
        });
    });

    var layerSource = {
        user_name: 'me',
        type: 'cartodb',
        sublayers: sublayers
    };
    cartodb.createLayer(map_object, layerSource, options)
        .addTo(map_object);
}

然后你可以从AJAX成功函数调用geoMap3(data)

【讨论】:

  • 花了我一些时间修修补补,但我明白了。谢谢!感谢额外的解释。我刚刚意识到一件事,我需要为每个元素的marker-fill 使用不同的颜色。关于如何开始这样做有什么想法吗?
  • 将颜色计算成一个变量,并使用字符串连接将其放入cartocss属性中。
【解决方案2】:

你会想要循环数组中的每个条目。只需将你的数据发送到这个函数,它就会创建对象

   function goMap3(data) {

    var layerSource = {
        user_name: 'me',
        type: 'cartodb',
        sublayers: []
    };

    //Adds a new sublayer to the layerSource object every iteration. 
    $.each(data, function (i, attr_value) {
        layerSource.sublayers.push({
            sql: "SELECT cartodb_id, the_geom_webmercator FROM mytable where attribute='" + attr_value + "'",
            cartocss: "#location_fusion_table {marker-fill:#1d5492;marker-width:5;marker-line-width: 0.3;marker- line-color:#fff;marker-allow-overlap: true;"
        });
    });

    cartodb.createLayer(map_object, layerSource, options)
        .addTo(map_object);
}

【讨论】:

  • 对...如果我确切地知道从我的 AJAX 返回了多少值,我就知道了,但我不知道。有时可能是 5,有时可能是 2 或 10。因此,我需要构建尽可能多的这些子层语句,因为我有返回值。
  • 哦,那现在就为你的解决方案修复吧
  • 修改后的答案应该适用于您的解决方案。您可以将整个内部函数放在“成功:函数(数据){}”中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多