Hello,
I’m trying to add a row to a CGridView with the value from an autocomplete. The idea came from this application made in Yii: http://demo.pointofsalesweb.com/sales/index but I’m really new into the framework…
My view is like this:
<table>
<div class="form" id="detalle">
<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'detalle-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array(
'onsubmit'=>"return false;",/* Disable normal form submit */
'onkeypress'=>" if(event.keyCode == 13){ send(); } " /* Do ajax call when user presses enter key */
),
)); ?>
<tr>
<div class="row">
<?php echo CHtml::label('Ingreso de Productos','detalleLabel')?>
</div>
</tr>
<tr>
<td>
<div class="well">
<?php echo CHtml::label('Buscar Producto','Producto_id'); ?>
<?php echo CHtml::hiddenField('Producto_id'); ?>
<?php
// ext is a shortcut for application.extensions
Yii::import('zii.widgets.jui.CJuiAutoComplete');
$this->widget('ext.myAutoComplete', array(
'name' => 'producto_autocomplete',
'sourceUrl' => $this->createUrl('facturacion/autocompleteProducto'),
// attribute_value is a custom property that returns the
// name of our related object -ie return $model->related_model->name
'value' => $detalleFactura->isNewRecord ? '': $detalleFactura->producto->Producto,
'options' => array(
'minLength'=>2,
'autoFill'=>false,
'focus'=> 'js:function( event, ui ) {
$( "#producto_autocomplete" ).val( ui.item.name );
return false;
}',
'select'=>'js:function( event, ui ) {
$("#Producto_id")
.val(ui.item.id);
return false;
}'
),
'htmlOptions'=>array('class'=>'input-1', 'autocomplete'=>'off'),
'methodChain'=>'.data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.name + "</a>" )
.appendTo( ul );
};'
));
?>
<?php echo CHtml::ajaxSubmitButton('Añadir',array('facturacion/ajax')); ?>
</div>
</td>
</tr>
</div><!--Fin detalle-->
</table>
<table>
<tr>
<td>
<?php
$this->widget('bootstrap.widgets.TbExtendedGridView', array(
//'filter'=>$person,
'type'=>'striped bordered',
'dataProvider' => $dataProvider,
'template' => "{items}\n{extendedSummary}",
'columns' => array(
array('name'=>'Codigo', 'header'=>'Codigo'),
array('name'=>'Nombre', 'header'=>'Nombre del Producto'),
array('name'=>'Precio', 'header'=>'Precio Unitario'),
array(
'name'=>'Cantidad',
'header'=>'Cantidad',
'headerHtmlOptions' => array('style' => 'width:50px'),
'class'=>'bootstrap.widgets.TbJEditableColumn',
'jEditableOptions'=>array(
'type'=>'text',
'cssclass'=>'form',
'width'=>'80px')
),
array('name'=>'Impuestos', 'header'=>'Impuestos'),
array('name'=>'Descuento', 'header'=>'Descuento'),
array('name'=>'Subtotal', 'header'=>'Subtotal'),
),
'extendedSummary' => array(
'title' => 'Totales',
'columns' => array(
#'Subtotal' => array('label'=>'Subtotal', 'class'=>'TbSumOperation'),
'Impuestos' => array('label'=>'Impuestos', 'class'=>'TbSumOperation'),
'Descuento' => array('label'=>'Descuentos', 'class'=>'TbSumOperation'),
'Subtotal' => array('label'=>'Total', 'class'=>'TbSumOperation')
)
),
'extendedSummaryOptions' => array(
'class' => 'well pull-right',
'style' => 'width:300px'
),
));
?>
</td>
</tr>
<?php $this->endWidget(); ?>
</table>
And the Controller action Ajax look like this:
public function actionAjax()
{
$model = new Facturacion;
$detalleFactura = new detalleFactura;
$detalle = array();
$detalle['id'] = $_POST['Producto_id'];
$detalle['producto_autocomplete'] = $_POST['producto_autocomplete'];
//Query para obtener los valores faltantes para el Grid
$Precio = Yii::app()->db->createCommand()
->select('precio')
->from('Producto')
->where('id = :id', array(':id'=>$detalle['id']))
->queryRow();
$detalle['Precio'] = $Precio['precio'];
$detalle['Cantidad'] = 1;
$impuestos = 0;
$impuestos = ($detalle['Precio'] * $detalle['Cantidad']) * 0.12;
$detalle['Impuestos'] = $impuestos;
$detalle['Descuento'] = 0;
$detalle['Subtotal'] = 0;
$dataProvider = new CArrayDataProvider($detalle, array(
'keyField'=>'id'));
echo CHtml::encode(print_r($detalle,true));
}
This returns me an array with the values, but from here I’m lost…
Any help will be appreciated.