2013-07-16 40 views
0

我正在做一個小應用程序。對於IHAVE兩條表像SLES和商店 銷售表看起來像這樣Yii自動填充相關值

============ 
    Sales 
============ 
id 
store_id 


Stores 
============= 
id 
store_name 
store_location 
store_code 
description 

我已經做了兩個表的模型和CRUD。在商店表中,我已經按照表格輸入了一些值。 現在在銷售控制器中,我提供了銷售和商店。所以在這裏我的行動創造這樣看

public function actionCreate() 
    { 
    $model=new Sales; 
    $stores = new Stores; 

    // Uncomment the following line if AJAX validation is needed 
    // $this->performAjaxValidation($model); 

    if(isset($_POST['Sales'], $_POST['Stores'])) 
    { 
     $model->attributes=$_POST['Sales']; 
     $stores->attributes=$_POST['Stores']; 
     $valid = $model->validate(); 
     $valid = $stores->validate(); 
     if($valid) 
     { 
     $stores->save(false); 
     $model->store_id = $stores->getPrimaryKey(); 
     $model->save(false); 
     $this->redirect(array('view','id'=>$model->id)); 
     } 
    } 

    $this->render('create',array(
     'model'=>$model, 
     'stores'=>$stores, 
    )); 
    } 

和銷售(_form.php這個)就是這樣

<div class="row"> 
    <?php echo $form->labelEx($stores,'store_name'); ?> 
    <?php echo $form->textField($stores,'store_name',array('size'=>60,'maxlength'=>80)); ?> 
    <?php echo $form->error($stores,'store_name'); ?> 
    </div> 

    <div class="row"> 
    <?php echo $form->labelEx($stores,'store_location'); ?> 
    <?php echo $form->textField($stores,'store_location',array('size'=>45,'maxlength'=>45)); ?> 
    <?php echo $form->error($stores,'store_location'); ?> 
    </div> 

    <div class="row"> 
    <?php echo $form->labelEx($stores,'store_code'); ?> 
    <?php echo $form->textField($stores,'store_code',array('size'=>45,'maxlength'=>45)); ?> 
    <?php echo $form->error($stores,'store_code'); ?> 
    </div> 

現在這裏的時候,我做銷售我想,當我將進入一個商店通過輸入密鑰名稱,然後它將開始顯示相關stores namesstore_locationstore_code將自動填充。現在有人可以告訴我該怎麼做?任何幫助和建議將是可觀的。非常感謝。

編輯this只有一個字段可以自動完成。但我希望所有其他相關領域也應該自動完成。

+0

你需要使用自動建議像這個演示http://papermashup.com/demos/autosuggest/。完成後,觸發jquery ajax自動填充根據名稱獲取字段數據庫 – nickle

+0

任何其他擴展或任何演示與Yii – NewUser

+0

我認爲直接你不能使用你需要使用jQuery的Ajax。從數據庫檢查 – nickle

回答

0

首先想到一些想法,然後對您的問題進行一般性回答。

首先,一個小竅門,你真的只是驗證商店模式,因爲你檢查銷售後重新分配$valid。相反,驗證邏輯應該更像這樣:

$valid = $model->validate() && $stores->validate(); 

如果其中任何一個都無效,它將返回false。

其次,我認爲這段代碼不會做你想做的事情。如果您根據現有數據自動填充商店信息並將其提交回創建操作,那麼您的代碼將嘗試根據該信息創建一個全新的商店條目,在數據庫中引入重複的商店。如果你總是在同一時間創建一個全新的銷售和一個全新的商店,這將工作,但我敢肯定,你不想這樣。

相反,爲了簡單起見,這應該是專門用於創建銷售對象的操作。不要嘗試創建新的商店對象,只需開始自動填充詳細信息到HTML spandiv標籤而不是表單域。唯一的表單字段將是商店ID字段,該字段將被添加到您的銷售對象以指示外鍵關係。

現在,爲了讓自動填充在多個領域進行,我將給你一個大綱並讓你填寫細節。您將需要在控制器中執行一項新操作,並在頁面上使用一些Javascript來處理自動填充。

public function actionGetStoreDetails($id) { 
    // Get the Store model associated with $id 
    // Create a JSON object based on this model. Hint, check out CJSON::encode() 
    // Return the result of the above function call. 
} 

現在,您的視圖頁上會出現一段JavaScript,它使用jQuery作爲示例,因爲它已經在Yii中。

$(function() { 
    $('your-store-id-field-html-id-here').keyup(function() { 
     // Get the current value of the form field 
     // Make a $.get() request to the new action defined above, passing the ID 
     // In the callback function, you'll get a JSON object 
     // Take the elements of the JSON object, like store.store_name, and assign them to <span id="store_name"></span> type fields in your HTML. 
    }); 
}); 

我意識到這不是複製粘貼代碼,但我希望它能給你一個非常好的啓動板。如果你花時間填寫空白並找出完整的方法,你會學到很多東西。