将数据发送到“视图”而不加载它

Send data to 'view' without loading it

本文关键字:加载 视图 数据      更新时间:2023-09-26

是否可以在不加载数据的情况下发送数据查看?我在谷歌地图库上工作,用于代码点火器。我遇到在列表页面上列出酒店的情况,当用户单击地图时,它会加载显示该地址的谷歌地图。我正在使用 ajax 在控制器中调用函数。问题是输出有 2 种类型的数据。一个应插入页面的head,另一个应插入特定body div.

echo $map['html'];
echo $map['js'];

html应该插入div而"js"插入头部。两者都是函数输出并返回对 Ajax 的响应。

从中调用 ajax 的view文件已由另一个函数加载。如何处理?

查看文件(Ajax 函数(

function showMap(id)
{
var detail_id = id.replace('map_','detail_desc_');
var hotel_id = id.replace('map_','');
$("#detail_map_"+hotel_id).html('loading');
$("#"+detail_id).slideDown('fast');
    var data = hotel_id;
    $.ajax({
        url:'<?php echo base_url() . "hotels/getMap";?>',
        data:'id='+data,
        type:'get',
        cache: false,
        success: function(data) {
            alert(data);
            //$("#detail_map_"+hotel_id).html(data);
        }//end success
    });//end ajax   
}

控制器功能

public function getMap()
{
    $id = $_GET['id'];
    $map_config['directions'] = true;
    $map_config['center'] = 'G 11 Markaz, Islamabad Pakistan';
    // Initialize our map. Here you can also pass in additional parameters for customising the map (see below) 
    $this->googlemaps->initialize($map_config);

    // Set the marker parameters as an empty array. Especially important if we are using multiple markers
    $marker = array();
    // Specify an address or lat/long for where the marker should appear.
    $marker['position'] = 'G 11 Markaz, Islamabad Pakistan'; 
    $this->googlemaps->add_marker($marker);
    // placed where we want the map to appear.
    $data['map'] = $this->googlemaps->create_map();
    //print_r($data['map']);
    echo $data['map']['html'];
            echo $data['map']['js'];
            //these two echos are sent back in response to ajax function
}

现在我想把echo $data['map']['js'];放在head标签中,而另一个放在div里面body

如果问题是关于将此返回的数据放在您从中调用getMap()的视图中,则可以在控制器中执行此操作:

$json = array(
  'html' => $data['map']['html'],
  'js' => $data['map']['js']
);
echo json_encode($json);

然后在 JavaScript 回调中使用它们:

$.ajax({
    url:'<?php echo base_url() . "hotels/getMap";?>',
    data:'id='+data,
    dataType: 'json',
    type:'get',
    cache: false,
    success: function(data) {
        $("#yourdiv").html(data.js);
        $("#yourdiv").append(data.html);
    }//end success
});//end ajax  

查找我所做的更改。由于这会产生问题,我建议也data.js放在您的div中,因为每次加载地图时,如果它是head,它都会一次又一次地附加这些返回的数据。这样,它将首先转储您的div,然后将data.jsdata.html附加到它。