简体   繁体   中英

Yii autofill with related values

I am doing a small application. For that Ihave two table like sles and stores The sales table looks like this

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


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

I have done the model and CRUD for both tables. In stores table I have entered some vales as per the table. Now in sales controller I have rendered both sales and stores. so here my action create looking like this

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,
    ));
  }

and in sales(_form.php) is like this

<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>

Now here when I am doing a sales I want that when I will enter one store name by entering keys then it will start to show the related stores names and the store_location and store_code will be auto-fill. Now can someone kindly tell me how to do this? Any help and suggestions will be appreciable. Thanks a lot.

Edit With this only one field can be autocomplete. But I want all the other related fields should be also autocomplete with this.

A few thoughts first, and then a general answer to your question.

First, a small niggle, you are really only validating the store model, because you reassign $valid right after checking the sale. Instead, the validation logic should be something more like this:

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

If either is invalid, it will return false.

Second, I don't think this code is going to do what you want it to do. If you autocomplete the store information based on existing data and submit it back to the Create action, then your code will try to create a brand new store entry based on that information, introducing duplicate stores in your database. If you always created a brand new sale and a brand new store at the same time, this would work, but I'm pretty sure you don't want that.

Instead, to keep it simple this should be an action dedicated to creating only sales objects. Don't try to create a new store object, just start autofilling details into HTML span or div tags instead of into form fields. The only form field will be the field that is the store id, which will be added to your sales object to indicate the foreign key relationship.

Now, to get autofilling going on several fields, I am going to give you an outline and let you fill in the details. You are going to need a new action in your controller, and some Javascript on the page to handle the autofill.

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.
}

Now you'll have a piece of Javascript on your view page that does something like the following, using jQuery as an example since it's already in 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.
    });
});

I realize that this is not copy-and-paste code, but I hope it gives you a really good launching pad. You'll learn a ton if you take the time to fill in the blanks and figure out how to do it fully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM