从动态填充的列表中删除行

时间:2017-09-19 19:54:02

标签: php html opencart

在OpenCart MVC中工作,事情更清楚。由于删除功能现在在技术上有效,因此修改了代码,但是从多个记录列表中删除它总是删除最后一个是我的代码。你选择哪一个并不重要,var _dump总是显示最后一条记录的id。

型号:

public function deleteCampaign($campaign_id) {


    $this->db->query("DELETE FROM `" . DB_PREFIX . "campaigns` WHERE campaign_id = '" . (int)$campaign_id . "'");

    var_dump("DELETE FROM `" . DB_PREFIX . "campaigns` WHERE campaign_id = '" . (int)$campaign_id . "'");
    exit;
    return $campaign_id;
}

控制器:

   public function delete() {
        $this->load->language('campaigns/campaign');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('campaigns/campaign');



        if (isset($this->request->post['campaign_id'])) {
    $campaign_id = $this->request->post['campaign_id'];
    $this->model_campaigns_campaign->deleteCampaign($campaign_id);

            $this->session->data['success'] = $this->language->get('text_success');

            $this->response->redirect($this->url->link('campaigns/campaign', 'token=' . $this->session->data['token'] . $url, true));
        }
        $this->getList();
    }

查看:

 <div class="panel-body" id="campaign-body">
    <form method="post" action="" enctype="multipart/form-data" id="form-order">

      <div class="table-responsive">
        <table class="table" id="campaign-table">
          <thead>
            <tr>
            <td><?php echo $column_campaign_id; ?></td>
              <td><?php echo $column_campaign; ?></td>
              <td><?php echo $column_giving_goal; ?></td>
              <td><?php echo $column_giving_count_goal; ?></td>
              <td><?php echo $column_owner; ?></td>
              <td></td>
            </tr>
          </thead>
          <tbody>
            <?php if ($orders) { ?>
            <?php foreach ($orders as $order) { ?>
            <tr class="campaign-list">

              <td><?php echo $order['campaign_id']; ?></td>
              <td><?php echo $order['campaign_name']; ?></td>
              <td><?php echo $order['campaign_giving_goal']; ?></td>
              <td><?php echo $order['campaign_giving_count_goal']; ?></td>
              <td><?php echo $order['campaign_owner']; ?></td>
              <td><input type="text" name="campaign_id" value="<?php echo $order['campaign_id']; ?>" />
                <div class="dropdown">
                  <button class="campaign-dropdown-btn dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"><?php echo $text_campaign_dropdown;?><span class="caret"></span></button>
                  <ul class="campaign-list-more dropdown-menu">
                    <li class="campaign-dropdown-list">Edit</li>
                    <li class="campaign-dropdown-list">Archive</li>
                    <li class="campaign-dropdown-list">
                        <button type="submit" id="campaign-del" name="deleteButton" onclick="javascript:changeFormAction('<?php echo $delete; ?>');">
                            <?php echo $button_delete; ?>
                        </button>
                    </li>
                  </ul>
                </div>
              </td>

            </tr>
            <?php } ?>
            <?php } else { ?>
            <tr>
              <td class="text-center" colspan="8"><?php echo $text_no_results; ?></td>
            </tr>
            <?php } ?>
          </tbody>
        </table>
      </div>
    </form>
    <div class="row">
      <div class="col-sm-6 text-left"><?php echo $pagination; ?></div>
      <div class="col-sm-6 text-right"><?php echo $results; ?></div>
    </div>
  </div>

The results of changing hidden to text in the input

1 个答案:

答案 0 :(得分:1)

第一步:删除记录。

工作原理:

当您单击删除按钮时,将调用javascript函数(changeFormAction)。在提交表单之前,该函数将表单的操作更改为给定的操作名称(“delete”)。给定的操作名称对应于控制器操作。

对于每个广告系列记录,广告系列ID都会保存为删除按钮的value属性的值。提交表单后,其值将在控制器操作delete()中读取,模型方法deleteCampaign()将被调用,并将其作为参数接收。

在HTML中:

将此脚本放在<head>标记中:

<script type="text/javascript">
    /**
     * Change form action dependent of the clicked form button.
     * 
     * @param action string Form action.
     * @return bool
     */
    function changeFormAction(action) {
        var form = document.getElementById('form-order');
        form.action = action;
        return true;
    }
</script>

之后,使用with:

替换dropddown中的最后一个<li>标记
<li class="campaign-dropdown-list">
    <button type="submit" name="deleteButton" value="<?php echo $order['campaign_id']; ?>" onclick="javascript:changeFormAction('<?php echo $delete; ?>');">
        <?php echo $button_delete; ?>
    </button>
</li>

在控制器中:

if (isset($this->request->post['deleteButton']) && $this->validateDelete()) {
    $campaign_id = $this->request->post['deleteButton'];
    $this->model_campaigns_campaign->deleteCampaign($campaign_id);

    //...
}