开/关按钮(切换按钮)通过编辑按钮隐藏和显示

时间:2016-09-09 08:55:19

标签: jquery codeigniter

我创建了类别模块,并将切换按钮设置为活动或非活动类别。我使用了codeigniter框架。

我的问题:我选中了按钮,然后编辑按钮显示和取消选中按钮,然后隐藏编辑按钮。

控制器:

class Category extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('category_model');
        $this->load->library('form_validation');
    }
  public function index() {
        if (!empty($_SESSION['admin'])) {
            $data = array(
                'page_title' => 'Category',
                'page_name' => 'category/index',
                'result' => $this->category_model->list_all()
            );
            $this->load->view('template', $data);
        } else {
            redirect('login');
        }
    }

public function update_cat_status() {
        $data = $this->category_model->update_cat_status($_GET);
        echo json_encode($data);
    }

 public function edit($id) {
        if (!empty($_SESSION['admin'])) {
            $this->form_validation->set_rules('cat_name', 'Category Name', 'required');
            if ($this->form_validation->run() == FALSE) {
                $data = array(
                    'page_title' => 'Edit Category',
                    'page_name' => 'category/edit',
                    'error' => validation_errors(),
                    'result' => $this->category_model->select_id($id),
                    'id' => $id
                );
                $this->load->view('template', $data);
            } else {
                $this->category_model->update($_POST, $id);
                redirect('category');
            }
        } else {
            redirect('login');
        }
    }
}

查看:

<table id="example" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%">
  <thead>
    <tr>
      <th>Category Name</th>
      <th>Status</th>
      <th>Edit</th>
    </tr>
  </thead>
  <tbody>
      <?php foreach ($result as $value): ?>
                                    <tr>
                                        <td><?= $value['name']; ?></td>
                                        <td> <div class="onoffswitch">
                                                <input type="hidden" value="<?php echo $value["id"]; ?>"/>
                                                <input type="checkbox" class="js-switch"
                                                <?php
                                                if ($value['status'] == 1) {
                                                    echo "checked";
                                                }
                                                ?>>
                                            </div>
                                        </td>
                                        <td><a href="<?= base_url("category/edit/{$value['id']}"); ?>" class="btn btn-success">Edit</a></td>
                                    </tr>
                                <?php endforeach; ?>
  </tbody>
</table>

型号:

class Category_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }
public function insert($data) {
        $this->db->insert('category', array('name' => $data['cat_name'], 'created_on' => date("Y-m-d h:i:sa")));
    }

    public function list_all() {
        return $this->db->get('category')->result_array();
    }
}

jquery的:

$(document).ready(function () {
    //category switch
    $('.onoffswitch').change(function () {
        var cat_id = $(this).children(':hidden').val();
        if ($(this).children(':checked').length === 0)
        {
            var status = 0;
        }
        else
        {
            var status = 1;
        }
        $.ajax({
            type: 'GET',
            url: base_url + "category/update_cat_status",
            data: {status: status, cat_id: cat_id},
            success: function (response) {
                console.log(response);
            }
        });
    });
});

截图

Screenshot

0 个答案:

没有答案