Yii 1.1: ajax-input

Insertion of a single model that is repeated in a form
1 follower

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

<div <?php echo get_class($model) . "='{$index}'"; ?> 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">
                Nº <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>
</div>

Be the first person to leave a comment

Please to leave your comment.

Create extension
Downloads