codeigniter分页下拉列表选择过滤器

时间:2016-01-11 12:04:57

标签: php codeigniter pagination

我创建了一个网站,我在项目中对项目进行了分组。当用户从提供的下拉菜单中选择项目并向其提交与该项目编号相关的项目时,将其检索并发送回控制器。

我的控制器

public function dwg_list()
{
    $data['proj_num'] = $this->model_proj->proj_num_all();
    $num_row = 1;
    if ($this->input->post('project_no') != '0')
    {
        $data['result'] = $this->model_issue->list_dwg($this->input->post('project_no'));

        if (count($data['result']) > 0)
        {
            $num_row = count($data['result']);
        } else $num_row = 1;
    }

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

    $config['base_url'] = base_url() . '/index.php/dwg_issue/dwg_list/page';
    $config['total_rows'] = $num_row;
    $config['per_page'] = 2; 

    $this->pagination->initialize($config); 



    $data['main_content'] = 'dwg_list';
    $this->load->view('includes/template.php', $data);

}

我的观点

<h1>This page displays all the registered drawings</h1>
<br>

<div id="body">
<div class="row">
        <div class="form-group-sm"><lable class="col-sm-2 control-label">Project number:</lable>
    <?php
        $js = 'onchange="this.form.submit()" class="form-control" id="focusInput"';
        echo form_open('dwg_issue/dwg_list');
        echo "<div class=\"col-xs-2\">" . form_dropdown('project_no',$proj_num, $this->input->post('project_no'),$js)."</div>";
        echo form_error('project_no', '<div class="col-xs-4"><div class="alert alert-danger fade in"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>','</div></div>');
    ?>
        </div>
        </div>
        <br>

<?php
echo "<noscript>".form_submit('submit','Submit')."</noscript>";
?>

        <table title="List of drawings" class="table table-hover" style="font-size: 14px">
            <caption><b>List of drawings</b></caption>
        <thead>
            <tr><th>Project number</th><th>Drawing number</th><th>Client drawing number</th>
                <th>Title</th><th>Size</th><th>Drawn by</th><th>Revision</th><th>Drawn Date</th></tr>
        </thead>    
        <tbody> &nbsp;
        <?php
            if(!empty($result))
            {
                foreach($result as $row)
                { 
                    echo "<tr>";
                    echo "<td>" . $row->project_no . "</td>";
                    echo "<td>" . $row->sws_dwg_no . "</td>";
                    echo "<td>" . $row->client_dwg_no . "</td>";
                    echo "<td>" . $row->dwg_title . "</td>";
                    echo "<td>" . $row->dwg_size . "</td>";
                    echo "<td>" . $row->dwg_by . "</td>";
                    echo "<td>" . $row->dwg_rev . "</td>";
                    echo "<td>" . date('Y/m/d', strtotime($row->dwg_date)) . "</td>"; 
                    echo "</tr>";
                }
            }

        ?>
        </tbody>
       </table>

       <?php echo $this->pagination->create_links(); ?>

</div>

我看到的问题是,当我单击下一页编号时,页面会刷新,下拉选择将返回默认值,然后将其提交给控制器。控制器无法向模型提交任何内容以从db中检索。

那我怎么能克服这个问题呢。

2 个答案:

答案 0 :(得分:0)

如果我正确理解您的问题,我认为您可以使用@Entity public class Item { @Id public myField; // other fields... } 传递您的项目编号,这样您就可以随时访问所选项目编号,而无需发布或使用post方法。< / p>

URI Segments

实施例: www.your-domain.com/controller/dwg_list/{project_no}

我希望这能给你一些想法。

这是一个链接,我认为可以提供帮助:

https://codeigniter.com/userguide2/general/controllers.html

答案 1 :(得分:-1)

也许在表单处理程序“dwg_issue / dwg_list”中将当前选择保存在会话变量中是有意义的

$this->session->set_userdata('selectedDropDownItem',$receivedDropDownItem);

每当你渲染你的视图时,你会看到是否设置了会话变量'selectedDropDownItem',然后在编写你的...时会打印出“selected”选项。

看看这个......

echo "<select>";
foreach ($selectOptions as $as){
            //check session if default option set....
            if (isset($this->session->userdata['selectedOption'])){
                $defOption = $this->session_userdata['selectedOption'];
            }else{
                  $defOption= "";
            }


            if (!strcmp($defOption,$as)){ // if the current option is equal to the session stored option...
                echo "<option value=\"".$as."\" selected=\"selected\">".$as."</option>";
            }else{
                echo "<option value=\"".$as."\">".$as."</option>";  
            }           
        }
echo "</select>";