2016-08-02 15 views
-1

一些能幫助我解決這個問題 我有我的網站地圖,我想刷新地圖只在點擊類別我this is my product如何僅刷新點擊類別的地圖?

  <?php 
      /* $message = "in home"; 
      echo "<script type='text/javascript'>alert('$message');</script>";*/ 

      foreach($categories as $category){ 
      echo '<li><a href="javascript:void(0); "onclick="javascript:changeData'.base_url('main/home/' .$category->adv_category).'"><img src="'.base_url('uploads/'.$category->image).'" alt="'.$category->cat_name.'" width="30" height="30"/></br><span>'.$category->cat_name.'</span></a></li>'; 
      } 
     ?> 
     </ul> 
    </div> 
</div> 
<script> 

    function changeData(category){ 

     var urlCI = '.base_url('main/home/' .$category->adv_category).'; 

     $.ajax({ 
     type: "POST", 
     url: urlCI, 
     data:{'categoryReq':category}, 
     success: function(response) { 
      if(response!=''){ 
      $('map_marker').html(response); 
      }else{ 
      return false; 
      } 
     } 
     }); 

    } 
</script> 

我的控制器代碼,如果你需要其他的代碼請詢問我會給。我給這只是爲了參考,謝謝

public function home($cat=NULL) 
{ 
    //$cat=10; 

    //echo debug_backtrace()[1]['function']; 

    if($_POST) 
    { 
     print_result($_POST); 
    } 
    $sql="SELECT * FROM (`ad_category`) WHERE `status`=1 AND `del_status`=0"; 
    $this->data['categories']=$this->db->query($sql)->result(); 

    if($cat) 
    { 
     //$message = $cat; 
     //echo "<script type='text/javascript'>alert('$message');</script>"; 


     //$this->data['middle_view']='home_again'; 
     $this->data['ads']=$this->advt_m->get_advts($cat); 
     $this->input->post('map_marker',$this->data); 
     $this->data['subview']='home'; 
     $this->load->view('__layout_main',$this->data); 
    } 
    else 
    { 
     /*$message = "Here 2"; 
     echo "<script type='text/javascript'>alert('$message');</script>"; 
     */ 
     $this->data['subview']='home'; 
     //$this->data['middle_view']='home'; 
     $this->data['ads']=NULL; 
     $this->load->view('__layout_main',$this->data); 
    } 

回答

0

拆分映射div和它的JavaScript到另一個視圖。

爲地圖創建具有所有必要功能的函數。通過Ajax調用添加並加載它。

阿賈克斯成功:

success:function(response){ 
    $('div').html(response); 
    //response will be the function which holds the $this->load->view('map'); 
} 

map.php

<div id="map"> 
YOUR MAP GOES HERE 
</div> 
<script> 
MAP SCRIPT 
</script> 

main.php(你的前端)

<div id="map_here"> 

</div> 

<script> 
$.ajax({ 
    type:"POST", 
    //Send some values 
    data:{ 
     somevalue:'somedata', 
     category:"mine" 
     }, 
url:"<?php echo base_url();?>mycontroller/mapfunc", 
success:function(response){ 
$('#map_here').html(response); 
} 
}) 
</script> 

myconroller:

function mapfunc(){ 
//Using this value you can change the script 
$data['category'] = $_POST('category'); 
$data['location'] = $_POST('somevalue'); 
//Process the works 
$this->load->view('map',$data); 
//On Ajax call this page can be loaded 
} 

每個ajax調用它將重新加載div。 任何疑問只是評論它...

+0

能否請您闡述一些Ajax代碼,因爲我是新來的AJAX代碼需要一些例子...三江源非常 –

+0

我更新了答案 –

0
var urlCI = '.base_url('main/home/' .$category->adv_category).'; 

大概應該是

var urlCI = '<?php echo base_url("main/home/$category->adv_category"); ?>'; 
+0

不是在我的情況...仍然是相同的結果:( –

+0

什麼是在瀏覽器控制檯輸出? – Tpojka

+0

javascript:void(0)阻止的方式 和刪除javascript:void(0)browsr輸出是頁面未找到,但給出的網址是正確的。 –