NewUser
NewUser

Reputation: 13333

Add create new link in Yii Framework drop down list

I am a newbie in Yii Framework and doing a small application in Yii Framework, I have the database for Invoice and Customers like this

   ==== Invoice ====
   id
   customer_id
   invoice_title
   invoice_no
   invoice_issue_date
   created_by
   updatd_by

   === Customers ===
   id
   customer_name
   address
   business_address
   city
   state

Now as per my requirment I need all the available customer name should come in a dropdown list in Invoice create form so I made changes in Invoice form.php to call all the available customers name like this

<div class="row">
<?php echo $form->labelEx($customers,'customer_name'); ?>
<?php echo $form->dropdownList($customers,'customer_name', CHtml::listData(Customers::model()->findAll(), 'id', 'customer_name'), array('empty'=>'Choose one')); ?>
<?php echo $form->error($customers,'customer_name'); ?>
</div>

It is showing the available customers name from the customer table.But I need one thing more.That is it will show an addition link in dropdown called as Create One.Where the admin will click on this link and one lightbox will come with the create customer form where all the data entered will be save in the customer table.I am also uploading some images for reference.Any help and suggestions will be highly appreciable.Reference image has been uploaded here.enter image description here

[Update] I have gone one step forward and made this changes

   <div class="row">
      <?php echo $form->labelEx($customers,'customer_name'); ?>
      <div id="job">
      <?php echo $form->dropDownList($customers,'customer_name',CHtml::listData(Customers::model()->findAll(),'id','customer_name'),array('prompt'=>'Select')); ?>
      <?php echo CHtml::ajaxLink(Yii::t('customers','Create customers'),$this->createUrl('customers/create'),array(
      'onclick'=>'$("#customers").dialog("open"); return false;',
      'update'=>'#jobDialog'
      ),array('id'=>'showJobDialog'));?>
    <div id="jobDialog"></div>
    </div>
    </div>

It is working But I want create Customers link inside the drop down list not outside the dropdown.So how to do that?Any help and suggestion will be highly appreciable.

Upvotes: 2

Views: 4746

Answers (1)

adamors
adamors

Reputation: 2656

You could create an empty select item with the label New Client, like this

$form->dropdownList($customers,'customer_name', CHtml::listData(Customers::model()->findAll(), 'id', 'customer_name'), array('empty'=>array('choose'=>'Choose one','new'=>'New Client')))

and have a jquery function stand by, triggering a lightbox popup if the 'New Client' is selected.

$('your_select').change(function(){
  if($(this).val() == 'new') {
   // do something
  }
})

UPDATE to reflect your update

<?php 
  echo $form->dropDownList(
                     $customers,'customer_name',
                     CHtml::listData(Customers::model()->findAll(), 'id', 'customer_name'),
                     array('prompt'=>'Select', 'empty'=>array('choose'=>'Choose'), 'id'=>'customersSelect')
              ); 
?>

<script type='text/javascript'>
   $(document).ready(function(){
       $('#customersSelect').change(function(){
          if($(this).val() == 'choose') {
             $("#customers").dialog("open");
          }
       });
    });
</script>

Upvotes: 1

Related Questions