ajax-input Insertion of a single model that is repeated in a form

  1. AjaxInput
  2. Requirements
  3. Usage

AjaxInput

Component to the insert and update a model several times on a form, ideal when you have a relationship HAS-MANY relationship between models.

Requirements

  • Yii 1.x
  • Bootstrap 3.x (Used in the example View)

Usage

1) Extract content in your extensions directory

2) Add in the actions of the controller, the action (ext.ajaxInput.AddItemAction) that adds a new view of model: ~~~ public function actions() { ... 'addItem' => 'ext.ajaxInput.AddItemAction', ... } ~~~

3) Call the widget in your create/update view, inside your form: ~~~ // Button for add a new model echo CHtml::link('Add new model', null, array(

'class' => 'btn btn-info addItem',
'model' => 'TestModel',
'view' => '_testForm'

));

// Listing of the model views $this->widget('ext.ajaxInput.AjaxInputWidget', array(

'modelClass' => 'TestModel',
'view' => '_testForm',
'ajaxUrl' => $this->createUrl('addItem'),
// 'items' => $items // Only for update, this contain a array of the models

)); ~~~

4) Example recommended of your view (_testForm): ~~~

class="col-xs-12 col-sm-6 col-md-4">
<div class="panel panel-default">

    <div class="panel-heading">
        <div span class="label label-success"><span numberItem="<?php echo get_class($model); ?>"></span>
        </div>
        <?php echo CHtml::link('&times;', '#', array(
            'class'=> 'close removeItem',
            'tabindex' => '-1',
            'onclick' => "removeItem('" . get_class($model) . "', {$index});"
        )); ?>
    </div>
    
    <?php if (isset($model['id'])): ?>
    <?php echo CHtml::activeHiddenField($model, '[' . $index . ']id'); ?>
    <?php endif; ?>
    
    <div class="panel-body">
    
        <div class="form-group">
            <?php echo CHtml::activeLabelEx($model, '[' . $index . ']name', array('class' => 'control-label')); ?>
            <?php echo CHtml::activeTextField($model, '[' . $index . ']name', array('class' => 'form-control input-sm', 'maxlength' => 20)); ?>
            <?php echo CHtml::error($model, '[' . $index . ']name', array('class' => 'control-error')); ?>
        </div>
        
    </div>
    
</div>

~~~
0 0
1 follower
205 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: User Interface
Developed by: rafaelt88
Created on: Sep 8, 2015
Last updated: 2 years ago

Downloads

show all

Related Extensions