2017-09-20 68 views
0

我在OpenCart中工作,並有一個列表和一個表單.tpl一起工作,表單創建新記錄,並且信息顯示在列表中。該列表給出了每個記錄的選項(編輯,刪除,存檔)我試圖讓編輯功能工作。當單擊編輯特定行時,我希望顯示的表單中已經填入了現有信息的輸入。然後可以由用戶更新。目前發生的情況是,單擊編輯時它不會啓動表單,而是編輯當前記錄,但是具有零和空列。我不知道如何重新啓動表單。並顯示用戶的信息更新動態填充列表中的記錄

型號:

public function editCampaign($campaign_id, $data) { 
     $this->db->query("UPDATE " . DB_PREFIX . "campaigns SET campaign_name = '" . $this->db->escape($data['campaign_name']) . "', campaign_giving_goal = '" . (float)$data['campaign_giving_goal'] . "', campaign_giving_count_goal = '" . (float)$data['campaign_giving_count_goal'] . "', campaign_owner = '" . $data['campaign_owner'] . "', date_beginning = '" . $this->db->escape($data['date_beginning']) . "', date_ending = '" . $this->db->escape($data['date_ending']) ."' WHERE campaign_id = '" . (int)$campaign_id . "'"); 

    } 

控制器:

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

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

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

     if (isset($this->request->post['editButton'])) { 
       $campaign_id = $this->request->post['editButton']; 
      $this->model_campaigns_campaign->editCampaign($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->getForm(); 
    } 

檢視:

<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> 
        <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"><button type="submit" id="campaign-del" name="editButton" value="<?php echo $order['campaign_id']; ?>" onclick="javascript:changeFormAction('<?php echo $edit; ?>');"> 
          <?php echo $button_edit; ?> 
         </button></li> 
         <li class="campaign-dropdown-list">Archive</li> 
         <li class="campaign-dropdown-list"> 
          <button type="submit" id="campaign-del" name="deleteButton" value="<?php echo $order['campaign_id']; ?>" 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> 

JS:

<script type="text/javascript"> 

    function changeFormAction(action) { 
     var form = document.getElementById('form-order'); 
     form.action = action; 
     return true; 
    } 
</script> 

回答

1
  1. 使編輯按鈕的類型type="button",所以沒有提交。
  2. 然後,當你點擊它時,它應該激活一個 類型POST的ajax請求到控制器動作edit(),傳遞活動 id作爲請求值。
  3. edit()只負責從db中獲取 相應記錄的詳細信息,並返回呈現的form.tpl內容(將其中的詳細信息傳遞給它)。
  4. 呈現的形式將內容然後由Ajax對象 (所述success回調內側)被讀取和寫入到表單 容器(像<div class="form-container"></div>)某處 之前,內部或之後campaign-table列表。您可以改爲在每條記錄中定義一個 表單容器,然後讓ajax填充發布的活動ID所屬的form.tpl內容 。
  5. 形式將有自己的提交按鈕命名爲updateButton (在其value屬性活動ID),這將發送 表單字段的值 - 由用戶完成 - 名爲update()控制器 行動。當然,這個活動ID也會被髮送到 。
  6. update()動作將讀取所有POST傳遞值並更新數據庫中的記錄。最後,應重新發送包含或不包含表格的 廣告系列列表 - 這取決於您,您想要如何使用 --以及相應的「更新 成功/失敗」消息。

祝你好運!