【问题标题】:Codeigniter pagination with jQuery使用 jQuery 进行 Codeigniter 分页
【发布时间】:2016-07-08 11:53:37
【问题描述】:

我已经被这个问题困住了好几天了,我快要崩溃了……我不知道自己做错了什么,也不知道应该去哪里寻求帮助。

我有一个搜索页面,允许用户按多个类别过滤结果。

这是视图:

<form method="post" id="search_form" name="search_form">
 <input type='hidden' id='seeking_gender_id' value='<?php echo $profile->gender_id ?>'/>

 <div id='sidebar_wrapper'>
  <div id='sidebar'>
   <div class="slider_header">
    Country
   </div>
   <div class="slider_content">
    <select id="country_dropdown" name="country_dropdown">
    <?php
     if($countries->num_rows() > 0)
     {
      foreach($countries->result() as $row)
      {
       $selected = ($row->country_id == $profile->country_id ? "selected='yes'" : "");
       echo "<option value='" . $row->country_id . "' "  . $selected . ">" . $row->country ."</option>";
      }
     }
    ?>
    </select>
   </div>
   <div class="slider_header">
    Region
   </div>
   <div class="slider_content">
    <select id='region_dropdown' name='region_dropdown'>
     <option value="0">All regions</option>
     <?php
      if($regions->num_rows() > 0)
      {
       foreach($regions->result() as $row)
       {
        $selected = ($row->region_id == $profile->region_id ? "selected='yes'" : "");
        echo "<option value='" . $row->region_id . "' "  . $selected . ">" . $row->region ."</option>";
       }
      }
     ?>
    </select>
   </div>
   <div class="slider_header">
    Gender
   </div>
   <div class="slider_content">
    <?php
     $male_checked = ($profile->seeking_gender_id == 1 ? "checked='yes'" : "");
     $female_checked = ($profile->seeking_gender_id == 2 ? "checked='yes'" : "");
    ?>
    <input id='male_checkbox' type="checkbox" name="genders" <?php echo $male_checked ?> value="1" />
    Male
    <br/>
    <input id='female_checkbox' type="checkbox" name="genders" <?php echo $female_checked ?>  value="2" />
    Female
   </div>
   <div class="slider_header">
    Age
   </div>
   <div class="slider_content">
    Between 
    <input id="min_age" type="text" maxlength="2" style="width: 35px" value='<?php echo $profile->min_age ?>'/>
    and
    <input id="max_age" type="text" maxlength="2" style="width: 35px" value='<?php echo $profile->max_age ?>'/>
   </div>
   <input id='profile_search_button' type='button' value='Search'></input>
  </div>
  <div id='sidebar_content'>
   <div id='content_header'>
    Search
    <div id='profiles_found' style='float: right'>0</div>
   </div>
   <div id='search_results'></div>
  </div>
  <div id='sidebar_footer'></div>
 </div>
</form>

所以当点击“profile_search_button”时,会运行以下 jQuery 代码:

    $("#profile_search_button").click(function()
 {
  $('#search_results').hide();

  $.post("http://localhost/index.php/search/search_database",
  function(data)
  { $('#search_results').html(data);
   $('#search_results').slideDown('slow');
  });
 });

该函数调用以下 PHP 函数:

public function search_database()
{
// HOW AM I MEANT TO GET THE VALUES OF THE FORM FILTERS? eg: country_id, region_id etc

$this->load->library('Jquery_pagination');

$config['base_url'] = site_url('search/blahh/');
$config['total_rows'] = 100;
$config['per_page'] = '10';
$config['div'] = '#search_results';

$this->jquery_pagination->initialize($config);
echo $this->jquery_pagination->create_links();
}

上面的代码有效...我只是不知道如何从“search_database”PHP 函数中读取过滤器的值。

我一直在研究这个例子,但我似乎只能找到这些:

http://tohin.wordpress.com/2008/10/07/codeigniter-ajax-pagination-exampleguideline/

有人可以帮忙吗?

【问题讨论】:

  • 呃...代码在预览窗口中看起来不错。这不是我的一天:(。

标签: jquery codeigniter pagination


【解决方案1】:

使用 $.post 的“数据”部分。

http://api.jquery.com/jQuery.post/

$.ajax({
  type: 'POST',
  url: url,
  data: { $('#form_field').val() },
  etc.

【讨论】:

  • 似乎我发布了旧版本的代码。我将以下 JSON 值传递给 PHP 函数 { "serialised_form" : $('#search_form').serialize() } 我可以使用 $this->input->post('serialised_form') 从 PHP 访问这些数据方法。但是当我单击分页链接时,不会发送表单值...
【解决方案2】:

搞定了,伙计们。只是一些愚蠢的错误。 必须序列化表单数据而不是作为 JSON 对象发送

$("#profile_search_button").click(function()
{
    $('#search_results').hide();

    $.post("http://localhost/index.php/search/search_database",  serialize_form(),
    function(data)
    {
        $('#search_results').html(data);
        $('#search_results').slideDown('slow');
    });
 });

事实证明,“additional_param”毕竟起作用了。

【讨论】:

    猜你喜欢
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 2014-08-09
    • 2019-11-12
    • 2016-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多