【问题标题】:Codeigniter Bootstrap Modal DeleteCodeigniter Bootstrap 模态删除
【发布时间】:2017-01-19 15:59:53
【问题描述】:

所以我想使用删除按钮删除表格中的一行,然后显示一个模式来确认删除过程。但是,我需要将 ID(在本例中使用文件名)传递给 URL 以完成删除过程。但是,我似乎无法传递完整的 URL 以便在模式中的“是”按钮中删除。我正在尝试模拟这样的过程:Plunker

这是代码。

模态:

<div class="modal-body text-center">
      <p> Are you sure you want to remove this from the list? </p><br><br>
      <button type="submit" class="btn btn-custom-1">Yes</button>
      <button type="button" class="btn btn-custom" data-dismiss="modal">Cancel</button>
</div>

删除按钮:

<button type="button" class="btn btn-custom-3" data-href="<?php echo base_url() . 'admin_forms/delete_carsticker/' . $row->filename; ?>" data-toggle="modal" data-target="#delete-modal"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span>  Delete </button>

Javascript:

  $('#delete-modal').on('show.bs.modal', function(e) {
    $(this).find('.btn-custom-1').attr('href', $(e.relatedTarget).data('href'));
});

【问题讨论】:

    标签: javascript php jquery twitter-bootstrap codeigniter


    【解决方案1】:

    在 codeigniter 中我们必须使用base_url()。它是具有模型、视图和控制器的 MVC 框架。

    对于删除,控制器中必须有一些函数来执行删除。

      <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    
     class SomeClass extends CI_Controller {
       public function delete($id){
         $this->load->model('some_model');/* load model*/
          if($this->some_model->delete($id)){ /*$id is ID and delete is some function in some_model (Model)*/
            redirect('some_success_function')
           }else{
             redirect('previous_function')
           }
        }
     }
    

    进入 html(视图)

    网址如下所示

     <?php
    if(isset($files)){
        foreach($files as $row){ 
    
            ?>
        <a href="#" data-href="<?php echo base_url() . 'admin_forms/delete_carsticker/' . $row->filename; ?>" data-toggle="modal" data-target="#confirm-delete">Delete record #23</a><br>
    
        <button class="btn btn-default" data-href="<?php echo base_url() . 'admin_forms/delete_carsticker/' . $row->filename; ?>" data-toggle="modal" data-target="#confirm-delete">
            Delete record #23
        </button>
    <?php   }
    }
    ?> 
    

    这里的SomeClass 是控制器class,包含函数delete($id),它使用$id 来删除值。控制器调用模型函数delete($id) 执行删除操作。

    编辑

    我已经更新了代码,模型部分没有问题。我已经在 localserver 中运行了您的代码,在 javascript 中没有问题。检查数据是否正确来自控制器,使用print_r($files) 进行验证

    【讨论】:

    • 嗨!是的,我目前有一个控制器,这是代码。 function delete_carsticker($filename) { $this-&gt;session-&gt;set_flashdata('deletesuccess', 'You have successfully deleted the account.'); $this-&gt;model_accounts-&gt;acc_delete($filename); redirect('admin_forms/car_sticker'); } 主要问题是模态YES按钮似乎没有获取控制器采用的URL,在我的例子中是$filename
    • 检查正确的文件数据从控制器传到视图
    【解决方案2】:

    我会使用一个简单的全局变量来存储 ID。

    var delete_id = ""; // or a file name
    
    $(document).on('click','.btn-custom-3', function(){
        delete_id = $(this).attr("data-href");
        // id also open the modal via jQuery too...  
    });
    

    然后当你做 YES 时使用 delete_id。

    $(document).on('click','.btn-custom-1', function(){
        if(delete_id != ""){
            // do something with it and come the modal but you can close it like you are doing it directly in the HTML...
        }
    });
    

    【讨论】:

    • 如何在YES中正确使用delete_id?抱歉,我是 javascript 新手
    • 我更新了我的示例。由于 delete_id 是全局的,所有处理程序都可以使用它。
    猜你喜欢
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 2014-03-07
    • 2020-07-07
    • 2022-01-15
    • 1970-01-01
    相关资源
    最近更新 更多