2014-11-02 78 views
0

在StackOverflow中可能有很多類似的帖子,但我的情況有點不同。所以,我決定使用這個標題發佈這個問題。CodeIgniter分頁活動鏈接不工作

我想描述的小零件我的問題:

部分 - 1: 我有一個正常的視圖頁面,在那裏我可以選擇特定的日期。這部分工作。

enter image description here

部分 - 2: 視圖頁面將顯示選定日期的一些數據(默認情況下,它是當前日期)。 例如,2014年11月2日,有一些數據。他們將顯示在負載爲這樣:

enter image description here

但是,這些數據從另一個視圖頁面,這是我使用Ajax調用加載這裏來。我這樣做是因爲我想將顯示的數據限制在選定的日期。這部分也在工作。

Part -3: 這是主要的問題描述。這是不工作的部分。

爲了進一步限制要顯示的數據量,我正在使用分頁。分頁工作正常,除了活動鏈接部分。無論我點擊哪個鏈接,默認選中的鏈接都只是第一個鏈接。

enter image description here

在上述畫面,我已經選擇鏈接2,但鏈路1被示出爲活動的,而不是鏈接2.

所以我的視圖 -

 <fieldset> 
       <legend id="meal_view_page_legend">Meal</legend> 
       <div id="content" class="box"> 
        <a href="<?php echo base_url(); ?>index.php/admin_logins/meal2"><button id="button_add">Add Meal</button></a> 

        <br> 
         <br> 

          <div> 
           <?php 
           if (isset($message)) { 
            if ($message == 'Meal information added successfully' || $message == 'Meal information edited successfully' || $message == 'Meal information deleted successfully') { 
             ?> 
             <p class="msg done"> 
              <?php echo $message; ?> 
             </p> 
             <?php 
            } else if ($message == 'Employee ID doesn\'t exist') { 
             ?> 
             <p class="msg error"> 
              <?php echo $message; ?> 
             </p> 
             <?php 
            } 
           } 
           ?> 
          </div> 

          <label style="font-weight: bold">Select Date: </label> 

          <select onchange="ajaxGetInfoByDate()" id="day" name="day"> 
           <!--option selected><--?php echo $day; ?></option--> 
           <option <?php if(date("d")=='01'){ ?> selected <?php } ?>>01</option> 
           <option <?php if(date("d")=='02'){ ?> selected <?php } ?> >02</option> 
           <option <?php if(date("d")=='03'){ ?> selected <?php } ?>>03</option> 
           <option <?php if(date("d")=='04'){ ?> selected <?php } ?>>04</option> 
           <option <?php if(date("d")=='05'){ ?> selected <?php } ?>>05</option> 
           <option <?php if(date("d")=='06'){ ?> selected <?php } ?>>06</option> 
           <option <?php if(date("d")=='07'){ ?> selected <?php } ?>>07</option> 
           <option <?php if(date("d")=='08'){ ?> selected <?php } ?>>08</option> 
           <option <?php if(date("d")=='09'){ ?> selected <?php } ?>>09</option> 
           <option <?php if(date("d")=='10'){ ?> selected <?php } ?>>10</option> 
           <option <?php if(date("d")=='11'){ ?> selected <?php } ?>>11</option> 
           <option <?php if(date("d")=='12'){ ?> selected <?php } ?>>12</option> 
           <option <?php if(date("d")=='13'){ ?> selected <?php } ?>>13</option> 
           <option <?php if(date("d")=='14'){ ?> selected <?php } ?>>14</option> 
           <option <?php if(date("d")=='15'){ ?> selected <?php } ?>>15</option> 
           <option <?php if(date("d")=='16'){ ?> selected <?php } ?>>16</option> 
           <option <?php if(date("d")=='17'){ ?> selected <?php } ?> >17</option> 
           <option <?php if(date("d")=='18'){ ?> selected <?php } ?>>18</option> 
           <option <?php if(date("d")=='19'){ ?> selected <?php } ?>>19</option> 
           <option <?php if(date("d")=='20'){ ?> selected <?php } ?>>20</option> 
           <option <?php if(date("d")=='21'){ ?> selected <?php } ?>>21</option> 
           <option <?php if(date("d")=='22'){ ?> selected <?php } ?>>22</option> 
           <option <?php if(date("d")=='23'){ ?> selected <?php } ?>>23</option> 
           <option <?php if(date("d")=='24'){ ?> selected <?php } ?>>24</option> 
           <option <?php if(date("d")=='25'){ ?> selected <?php } ?>>25</option> 
           <option <?php if(date("d")=='26'){ ?> selected <?php } ?>>26</option> 
           <option <?php if(date("d")=='27'){ ?> selected <?php } ?>>27</option> 
           <option <?php if(date("d")=='28'){ ?> selected <?php } ?>>28</option> 
           <option <?php if(date("d")=='29'){ ?> selected <?php } ?>>29</option> 
           <option <?php if(date("d")=='30'){ ?> selected <?php } ?>>30</option> 
           <option <?php if(date("d")=='31'){ ?> selected <?php } ?>>31</option> 
          </select> 

          <select onchange="ajaxGetInfoByDate()" id="month" name="month" > 
           <!--option selected><--?php echo $month; ?></option--> 
           <option <?php if(date("m")=='01'){ ?> selected <?php } ?>>01</option> 
           <option <?php if(date("m")=='02'){ ?> selected <?php } ?>>02</option> 
           <option <?php if(date("m")=='03'){ ?> selected <?php } ?>>03</option> 
           <option <?php if(date("m")=='04'){ ?> selected <?php } ?>>04</option> 
           <option <?php if(date("m")=='05'){ ?> selected <?php } ?>>05</option> 
           <option <?php if(date("m")=='06'){ ?> selected <?php } ?>>06</option> 
           <option <?php if(date("m")=='07'){ ?> selected <?php } ?>>07</option> 
           <option <?php if(date("m")=='08'){ ?> selected <?php } ?>>08</option> 
           <option <?php if(date("m")=='09'){ ?> selected <?php } ?>>09</option> 
           <option <?php if(date("m")=='10'){ ?> selected <?php } ?>>10</option> 
           <option <?php if(date("m")=='11'){ ?> selected <?php } ?>>11</option> 
           <option <?php if(date("m")=='12'){ ?> selected <?php } ?>>12</option>  
          </select> 

          <select onchange="ajaxGetInfoByDate()" id="year" name="year"> 
           <!--option selected><--?php echo $year; ?></option--> 
           <option <?php if(date("y")=='14'){ ?> selected <?php } ?>>2014</option> 
           <option <?php if(date("y")=='15'){ ?> selected <?php } ?>>2015</option> 
           <option <?php if(date("y")=='16'){ ?> selected <?php } ?>>2016</option> 
           <option <?php if(date("y")=='17'){ ?> selected <?php } ?>>2017</option> 
          </select> 

          <div id="indexView"></div> 
          </fieldset> 

而另一查看頁面,我已經加載上面的查看頁面內使用ajax負載:

 <fieldset>  

     <table id = "meal_list_table"> 
      <tr> 
       <th scope="col" >Employee Id</th> 
       <th scope="col" >Guest?</th> 
       <th scope="col" >No. of Guest</th> 
       <th scope="col" >Remarks</th> 
       <th scope="col" colspan="2" >Action</th> 
      </tr> 

      <?php foreach ($info as $list) { ?> 
       <tr> 
        <td><?php echo $list['emp_id']; ?></td> 
        <td><?php echo $list['is_guest']; ?></td> 
        <td><?php echo $list['num_of_guest']; ?></td> 
        <td><?php echo $list['remarks']; ?></td> 
        <td><a href="<?php echo base_url(); ?>index.php/admin_logins/mealIdGet/<?php echo $list['id']; ?>"><button id="button_edit">Edit</button></a></td> 
        <td> 
         <a href="<?php echo base_url(); ?>index.php/admin_logins/meal4/<?php echo $list['id']; ?>" 
          onclick="return confirm('Do you want to delete this Meal Information?');"> 
          <button id="button_delete">Delete</button> 
         </a> 
        </td> 
       </tr> 
      <?php } ?> 
     </table> 
     <p><?php echo $links; ?></p> 
</fieldset> 

AJAX調用(在第一視圖頁):

 function ajaxGetInfoByDate() { 
      //alert("ok"); 
      var offset = document.getElementById("offset").value; 
      var day = document.getElementById("day").value; 
      var month = document.getElementById("month").value; 
      var year = document.getElementById("year").value; 
      var date = year + '-' + month + '-' + day; 
      //alert(offset); 

      // alert(date); 

      if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari 
       xmlhttp = new XMLHttpRequest(); 
      } 
      else {// code for IE6, IE5 
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
      xmlhttp.onreadystatechange = function() { 
       if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
        document.getElementById("indexView").innerHTML = xmlhttp.responseText; 
       } 
      } 
      xmlhttp.open("GET", "<?php echo base_url(); ?>index.php/admin_logins/ajaxGetInfo?date=" + date + "&&offset=" + offset, true); 
      xmlhttp.send(); 
     } 

控制器方法,以查看所述第一視圖頁:

function meal() { 
    $data['message'] = $this->session->flashdata('message'); 
    $this->load->view('/admin_logins/meal_list', $data); 
} 

和控制器的方法通過AJAX調用加載第二視圖頁:

function ajaxGetInfo() { 

    $customDate = $this->input->get('date'); 
    $offset = $this->input->get('offset'); 
    $query_string = "SELECT m.id, e.emp_id, m.is_guest, m.num_of_guest, m.remarks 
    FROM meal m 
    LEFT JOIN employee e 
    ON e.id = m.emp_id 
    WHERE m.entry_date ='$customDate' "; 
    $query = $this->db->query($query_string); 

    //pagination codes 
    $config = array(); 
    $config['base_url'] = site_url('admin_logins/meal'); 
    $config['per_page'] = 10; 
    $config['uri_segment'] = 3; 
    $config['total_rows'] = $query->num_rows(); 
    $choice = $config['total_rows']/$config['per_page']; 
    $config['num_links'] = round($choice); 
    $this->pagination->initialize($config); 
    $page = ($offset) ? $offset : 0 ; 
    $conf = $config['per_page'];  

    $query_string2 = "SELECT m.id, e.emp_id, m.is_guest, m.num_of_guest, m.remarks 
    FROM meal m 
    LEFT JOIN employee e 
    ON e.id = m.emp_id 
    WHERE m.entry_date ='$customDate' 
    LIMIT $page, $conf "; 

    $query2 = $this->db->query($query_string2); 
    $dataByDate['info'] = $query2->result_array(); 
    //echo "<pre>"; 
    //print_r($dataByDate['info']); 
    //die(); 
    $dataByDate['links'] = $this->pagination->create_links(); 
    $this->load->view('admin_logins/ajaxIndex', $dataByDate); 
} 

我的代碼中哪裏有錯?

回答

0

您是否檢查$ offset返回的值以及它是否是正確的值?

+0

我已經檢查過$ $ offset值。這是正確的。 – 2014-11-03 10:45:13

+0

將$ config ['base_url'] = site_url更改爲base_url – mdgeus 2014-11-06 13:53:04