Yii Framework Forum: JCUIAUTOCOMPLETE get JSON data - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

JCUIAUTOCOMPLETE get JSON data JSON data format not acceptable by jcuiautocomplete

#1 User is offline   Chinmi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 21-May 15

Posted 21 April 2017 - 12:14 AM

Dear Master of Yii,

My name is Chinmi, iam beginner in Yii.

i use widget zii.widgets.jui.CJuiAutoComplete to get nama and quantity from tbl_buku database.
and then also i want to automaticaly change another textfield quantity when i choose selected data nama from jcuiautocomplete.

here is program

//my controller BukuController.php

public function actionJudulbukulist(){
$sql = 'SELECT nama, quantity FROM tbl_buku WHERE nama LIKE :nbk';
$command = Yii::app()->db->createCommand($sql);
$command->bindValue(":nbk", '%'.$_GET['term'].'%', PDO::PARAM_STR);
$rows = $command->queryAll();
echo CJSON::encode($rows);
Yii::app()->end();
}

my view
<div class="">
<?php
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name'=>'nama',

'sourceUrl'=>Yii::app()->createUrl('Buku/Judulbukulist'),


'options'=> array(
'minLength'=>2,
'type'=>'get',
'select'=>"js:function(event, ui) {

$('#Buku_quantity').val(ui.item.quantity);

}",

),
'htmlOptions'=>array(
'size'=> 100,
'style'=>'height:20px;',

),
));
?>
</div>

// have id= "Buku_quantity"
<div class=""><?php echo $form->textField($model,'quantity',array('size'=>4,'maxlength'=>4));?></div>



i need help, why when i enter 2 letter pop up on the buttom cjuiautocomplete field only blank.
------------
i try to see JSON from judulBukulist() function it return

[{"nama":"Buku Test","quantity":"280"},{"nama":"KURA - KURA","quantity":"950"}]

-------------
and then i try to remove quantity from sql syntac

$sql = 'SELECT quantity FROM tbl_buku WHERE nama LIKE :nbk';

return data

[{"nama":"KURA - KURA"},{"nama":"Buku Test"}]

it still not working it is the same when i enter 2 letter will popup blank
--------------
and then i try to change $rows = $command->queryAll(); with $rows = $command->queryColumn();
its return data

["KURA - KURA","Buku Test"]

it working when i enter 2 letrer pop up will show up



but it will not provide quantity data which i will use for change another texfield "Buku_quantity" id.


Please help me master...


thank you for your advice Master of Yii.


Regard,

Chinmi
0

#2 User is offline   Chinmi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 21-May 15

Posted 25 April 2017 - 02:01 AM

[SOLVED] this is the solution..

i finnaly find the solution.

cJuiAutocomplete only accept several format like array, or array object and function.


here is my all program..

//my controller BukuController.php
public function actionJudulbukulist()
{


$sql = 'SELECT nama,quantity FROM tbl_buku WHERE nama LIKE :nbk';
$command = Yii::app()->db->createCommand($sql);
$command->bindValue(":nbk", '%'.$_GET['term'].'%', PDO::PARAM_STR);
//$rows = $command->queryColumn();
$rows = $command->queryAll();
$result = array();
foreach($rows as $row){
$result[] = array(
'label'=> $row['nama'],
'value'=>$row['nama'],
'quantity'=> $row['quantity'],
);
}
echo CJSON::encode($result);
Yii::app()->end();
}



//my view
<div class="">
<?php
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name'=>'nama',

'sourceUrl'=>Yii::app()->createUrl('Buku/Judulbukulist'),


'options'=> array(
'minLength'=>2,
'type'=>'get',
'select'=>"js:function(event, ui) {

$('#Buku_quantity').val(ui.item.quantity);

}",

),
'htmlOptions'=>array(
'size'=> 100,
'style'=>'height:20px;',

),
));
?>
</div>

// have id= "Buku_quantity"
<div class=""><?php echo $form->textField($model,'quantity',array('size'=>4,'maxlength'=>4));?></div>

//////////
the controller result will be like

[{"label":"KURA - KURA","value":"KURA - KURA","quantity":"950"},{"label":"Buku Test","value":"Buku Test","quantity":"280"}]

this format JSON will be accepted by cJuiAutocomplete.
there are a label and value defined.

and key quantity is use for change another text field which have id ="Buku_quantity"

i hope this will be usefull for anyone who read this and have a problem like me...

Regard,
^_^
Chinmi
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users