如何在编辑行后保持当前页面的ajax分页?

时间:2014-10-02 16:54:55

标签: php ajax codeigniter

header.php有一个<div id="content"></div>,然后会加载页面user.php

Q1:header.php上的javascript编码是否无法与加载的内容进行交互? 因此我将js代码放在加载的页面上,但我发现有点奇怪。

Q2: 分页功能正在运行,假设它在第4页。 在我编辑其中一行之后,页面返回到第一页。我想保留在第4页。

&LT; 1 2 3 4 5 6>

我希望在点击分页后将当前链接存储为文本,但首先存储链接,然后页面将刷新并清除数据。

分页链接的href看起来像 本地主机://blog/index.php/admin/users/show/10

本地主机://blog/index.php/admin/users/show/20

本地主机://blog/index.php/admin/users/show/30

请指出解决方案或提出另一个更好的解决方案

  $("input[name=submit]").click(function() {
            $(this).parents('.alert-box').hide();
            $form = $(this).parent('form');
            $.post(
                    $form.attr('action'),
                    $form.find(':input').serializeArray(),
                    function(data) {
                        $("#content").html(data);
                    }
            );
        });

查看:header.php

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <link href="<?= $css; ?>bootstrap.css" rel="stylesheet" type="text/css">
        <link href="<?= $css; ?>basic/basic.css" rel="stylesheet" type="text/css">
        <script type="text/javascript" src="<?= $js; ?>jquery.js"></script>
        <script type="text/javascript" src="<?= $js; ?>jquery_validate.js"></script>
        <script type="text/javascript" src="<?= $js; ?>form_control.js"></script>
        <script type="text/javascript" src="<?= $js; ?>additional-methods.min.js"></script>

    </head>
    <body>
        <style>
            #content{
                background-color: #D0D0D0;
                float:left;
                width:80%;
            }
            #main-frame{
                width:100%;
            }
            #list{
                width:18%;
                float:left;
            }
            #delete-alert-box{
                background-color: #269abc;
                position: absolute;

                z-index: 99999;
                display: none;
            }
            #edit-alert-box{
                background-color: #269abc;
                position: absolute;

                z-index: 99999;
                display: none;
            }

            body{
                font-size:2em;
            }

        </style>
        <script language="javascript">
            $(document).ready(function() {
                init();
                $('.open').click(function(e) {
                    e.preventDefault();
                    $.post($(this).attr('href'), function(data) {
                        $('#content').html(data);
                    });
                });

            });

            function init() {
                $.post(
                        "<?php echo site_url("admin/users/show");?>", function(data) {
                            $("#content").html(data);
                        }
                );
            }
        </script>

        <div id="header">
            <div id="logo">
            </div>
            <?php if ($this->AuthModel->check_admin_log()) { ?>
                <a href="<?= site_url('admin/users/logout') ?>">Logout</a>
            <?php }
            ?>
        </div>

        <ul id="list">
            <li>
                <a href="<?php echo site_url('admin/users/show') ?>" class="open">Users Manage</a>
            </li>
            <li>
                <a href="<?php echo site_url('admin/groups') ?>" class="open">Group Manage</a>
            </li>
            <li>
                <a href="<?php echo site_url('admin/blog_posts') ?>" class="open">Post Mange</a>
            </li>
            <li>
                <a href="<?php echo site_url('admin/system') ?>" class="open">System Setting</a>
            </li>
            <li>
                <a href="<?php echo site_url('logout/admin') ?>" >Logout</a>
            </li>
        </ul>
        <div id="content" class="box"></div> 

查看:users.php

<table border="1">

    <tr><th>User Id</th><th>User Name</th><th>Email</th><th>Registeration Date</th><th>Group</th><th>State</th><th>Operation</th></tr>
    <?php foreach ($users as $user): ?>
        <tr>
            <td><?= $user->id; ?></td>
            <td><?= $user->username; ?></td>
            <td><?= $user->email; ?></td>
            <td><?= mdate('%Y-%m-%d', $user->registeration_time); ?></td>
            <td><?= $user->user_type; ?></td>
            <td><?= $user->account_status; ?></td>
            <td>
                <button type="button" value="<?php echo $user->id; ?>" name="delete">X</button>
                <button type="button" value="<?php echo $user->id; ?>" name="edit">edit</button>
            </td>
        </tr>
    <?php endforeach; ?>

</table>  

<?php echo $links ?>
<div id="delete-alert-box" class="alert-box">
    <div class="cancel">X</div>
    <h3>Are you sure to delete the account?</h3>
    <form action="<?php echo site_url('admin/users/delete') ?>" id="deleteForm">
        <input type="hidden" value="" name="user_id">
        <input type="button" value="Yes" name="submit"> 
        <input type="button" value="No" name="cancel">
    </form>
</div>
<div id="edit-alert-box" class="alert-box">
    <div class="cancel">X</div>
    <h3>Edit User:<span id="username"></span></h3>
    <form action="<?php echo site_url('admin/users/edit') ?>" id="editForm">
        <table>
            <tr>
                <td>Group</td>
                <td>
                    <select name="group" id="group">
                        <option value="1">Nomal User</option>
                        <option value="2">Amin</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>State</td>
                <td>
                    <select name="state" id="state">
                        <option value="1">Activated</option>
                        <option value="2">Non-Activated</option>
                        <option value="3">Blocked</option>
                    </select>
                </td>
            </tr>
        </table>
        <input type="hidden" value="" name="user_id">
        <input type="button" value="Yes" name="submit"> 
        <input type="button" value="No" name="cancel">
    </form>
</div>

<script>
    $(document).ready(function() {
        $(".cancel").click(function() {
            $(this).parent('.alert-box').hide();
        });
        $("input[name=cancel]").click(function() {
            $(this).parents('.alert-box').hide();
        });

        $("button[name=delete]").click(function() {
            var $user_id = $(this).attr('value');
            if ($user_id !=<?php echo $this->session->userdata('user_id') ?>) {
                $("#delete-alert-box").show();
                $('#delete-alert-box').find('input[type=hidden]').attr('value', $user_id);
            }
        });

        $("button[name=edit]").click(function() {
            var $user_id = $(this).attr('value');
            if ($user_id !=<?php echo $this->session->userdata('user_id') ?>) {
                $("#edit-alert-box").show();
                var $tr = $(this).parents('tr');
                var $tds = $tr.find('td');
                $('#edit-alert-box').find('input[type=hidden]').attr('value', $user_id);

                $('#group').find('option').each(function(index) {
                    $(this).removeAttr('selected');
                });
                $('#group').find("option[value=" + get_group_code($($tds[4]).html()) + "]").attr('selected', 'selected');

                $('#state').find("option[value=" + get_account_code($($tds[5]).html()) + "]").attr('selected', 'selected');
            }
        });

        $("input[name=submit]").click(function() {
            $(this).parents('.alert-box').hide();
            $form = $(this).parent('form');
            $.post(
                    $form.attr('action'),
                    $form.find(':input').serializeArray(),
                    function(data) {
                        $("#content").html(data);
                    }
            );
        });

        $('.paging a').click(function(e) {
            e.preventDefault();
            $.post($(this).attr("href"), function(data) {
                $("#content").html(data);
            });
        });
    });
    function get_group_code(name) {
        switch (name) {
            case "Normal User":
                return 1;
            case "Admin":
                return 2;
        }
    }
    function get_account_code(name) {
        switch (name) {
            case "Activated":
                return 1;
            case "Non-Activated":
                return 2;
            case "Blocked":
                return 3;
        }
    }
</script>

控制器:admin / users.php

function pagination() {
    $this->load->library('pagination');
    $config['base_url'] = site_url('admin/users/show');
    $config['total_rows'] = $this->UsersModel->get_num_rows();
    $config['per_page'] = '10';
    $config['uri_segment'] = 4;
    $config['full_tag_open'] = '<p class="paging">';
    $config['full_tag_close'] = '</p>';
    $this->pagination->initialize($config);
    return $this->pagination->create_links();
}

public function show() {
    $data['users'] = $this->UsersModel->get_users(10, $this->uri->segment(4, 0));
    $data['links'] = $this->pagination();
    $this->load->view('admin/users', $data);
}

public function delete() {
    $user_id = $this->input->post('user_id');
    if (!$this->UsersModel->delete_user($user_id)) {
        echo "Unknown error";
    }
    $this->show();
}

public function edit() {
    $user_id = $this->input->post('user_id');
    $state = $this->input->post('state');
    $group = $this->input->post('group');
    $data = array(
        'id' => $user_id,
        'account_status_code' => $state,
        'group_status_code' => $group
    );
    if (!$this->UsersModel->edit_user($data)) {
        echo "Unknown error";
    }
      $this->show();
}

2 个答案:

答案 0 :(得分:0)

首先进行分页的最佳方法是通过get,获取url友好并旋转用户以防万一需要传递它。 你需要在console.log()中设置数据,如果数据是这个值, 如果预期值到达日期,请尝试通过追加html进行切换: 例。 $(“#content”)html('。'); $(“#content”)追加(日期)。;

答案 1 :(得分:0)

  

分页功能正在运行,假设它在第4页。我之后   编辑其中一行,页面返回第一页。我想要   保留在第4页。

在浏览器中打开第4页时,您可以将其号码保存在会话中,然后在编辑后可以读取您在会话中存储的值,即.4。

@session_start();

function indexAction()
{
   $_SESSION['curr_page'] = 4; // or take it from $_GET
}

function saveAction(){
  // .... do stuff....

  header('location: page.php?page=' . $_SESSION['curr_page']);
}