i have a auto complete field in my view whose code is as follows
<?php
$data = Members::find()
->select(['first_name as value', 'first_name as label','id as id'])
->asArray()
->all();
echo 'Name' .'<br>';
echo AutoComplete::widget([
'name' => 'member_name',
'clientOptions' => [
'source' => $data,
'minLength'=>'3',
'autoFill'=>true,
'select' => new JsExpression("function( event, ui ) {
$('#receipt-member_id').val(ui.item.id);//#receipt-member_id is the id of hiddenInput.
}")],
]);
?>
auto complete works correctly, it shows the first name of all the 'members'.
But there can be many guys with similar first name, so what i want is to concatenate first_name and last_name. In normal dropdowns it can be done as follows
<?php
$models1 = Members::find()->all();
$data = array();
foreach ($models1 as $model1)
$data[$model1->id] = $model1->first_name . ' '. $model1->last_name;
echo $form->field($model, 'member_id')->dropDownList(
$data,
['prompt'=>'Select...']);
?>
how can i do this with the autocomplete widget?
try this way
$data = Members::find()
->select(['concat(first_name,last_name) as value', 'first_name as label','id as id'])
->asArray()
->all();
If you need space between 2 fields, use this:
$data = Members::find()
->select(['concat(first_name, SPACE(1), last_name) as name', 'first_name as label','id as id'])
->asArray()
->all();
SPACE() method will generate space accord the specific number you entered, for you :D
Related
I have text edit box with Autocomplete::widget on the web page.
Autocomplete populates from DB. When I start to type and choose Autocomplete item (which is the First Name from DB, in my case) I need to get the associated id from DB. How could this get implemented?
<?php
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use app\models\Rating;
use app\models\Teacher;
use yii\jui\AutoComplete;
use yii\web\JsExpression;
/* #var $this yii\web\View */
/* #var $model app\models\Rating */
/* #var $form ActiveForm */
$data = Teacher::find()
->select(['lname as value', 'lname as label','id as id'])
->asArray()
->all();
//print_r($data[0].id);//exit;
?>
<div class="rating-input">
ФИО
<?php
echo AutoComplete::widget([
'name' => 'Teacher',
'id' =>'ddd',
'clientOptions' => [
'source' => $data,
'autoFill'=>true,
'minLength'=>'1',
],
]);
https://paste.ee/p/BtQav
try this
<input type="hidden" name="teacher_id" id="teacher_id" value="">
<?php
echo AutoComplete::widget([
'name' => 'Teacher',
'id' =>'ddd',
'clientOptions' => [
'source' => $data,
'autoFill'=>true,
'minLength'=>'1',
'select' => new \yii\web\JsExpression("function( event, ui ) {
// $('#teacher_id').val(ui.item.id);//#teacher_id is the id of hiddenInput.
console.log(ui.item.id);
}")
],
]);
You have more information in documentation page : http://www.yiiframework.com/wiki/830/simple-jui-autocomplete-in-yii2/
I want to retrive custom post and want to sort it by title. However when i made a dump of what exact SQL request is sent, i found out the orderby is using menu_order instead of title
Here's the code:
$args=array(
'post_type' => 'custom_post',
'orderby' => 'title',
'order' => 'ASC',
'post_status' => 'publish',
'posts_per_page' => '-1',
);
Heres the dump
"SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'custom_post' AND ((wp_posts.post_status = 'publish')) ORDER BY wp_posts.menu_order ASC "
Hence when i retrive the custom posts, its not in the order i want it to be.
Your help is appreciated
You can create new WP_Query instance to achieve the exact expected result.
<?php
$args = array(
'post_type' => 'custom_post_type',
'order' => 'ASC',
'orderby' => 'title',
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<div class="smal-sec">
<h3><?php the_title();?></h3>
<?php the_content();?>
</div>
<?php
endwhile;
endif;
wp_reset_query(); // Restore global post data stomped by the_post().
?>
This query will help you to display all posts order by title (A -> Z). Please make sure no such plugins are there, which is actually overwriting the WP_Query instance.
In de documentation for activeDropDownList() -> $htmlOptions says this: options: array, specifies additional attributes for each OPTION tag. The array keys must be the option values, and the array values are the extra OPTION tag attributes in the name-value pairs. For example,
array(
'value1'=>array('disabled'=>true,'label'=>'value 1'),
'value2'=>array('label'=>'value 2'),
);
So it should be posible to do it.
I have something like this:
<?php
$criteria = new CDbCriteria;
$criteria->condition = "estado = 'activa'";
$criteria->order = "fecha_inicio";
echo $form->dropDownList($model,'salida',
CHtml::listData(Salida::model()->findAll($criteria), 'id', 'fecha_inicio', 'actividad.nombre'),
array('prompt'=>'-- Selecciona --',
'options' => array(
'26'=>array('disabled'=>true,),
'23'=>array('disabled'=>true,),
))
);?>
The drop down it's generating correctly. How can do this for all option taking the value form one model's field?
I figured out:
$opciones = CHtml::listData(Salida::model()->findAll($criteria), 'id', 'precio');
$opciones_f = array();
foreach ($opciones as $id => $precio) {
$opciones_f[$id] = array('data-precio'=>$precio);
}
echo $form->dropDownList($model,'salida',
CHtml::listData(Salida::model()->findAll($criteria), 'id', 'fecha_inicio', 'actividad.nombre'),
array('prompt'=>'-- Selecciona --','options' => $opciones_f,)
similarly I have 2 tables in database,accounts and banks,and One to Many Relation between them.
'banks' => array(self::BELONGS_TO, 'Banks', 'banks_id'), in Accounts model.
I need to use Chtml::dropDownList in account creation form,so I edit _form.php to it:
<div class="row">
<?php echo $form->labelEx($model,'banks_id'); ?>
<?php echo CHtml::dropDownList($model, 'banks_id', CHtml::listData(Banks::getBanksList(), 'id', 'title','country')); ?>
<?php echo $form->error($model,'banks_id'); ?>
</div>
and also in Banks model develop `getBanksList by:
public static function getBanksList()
{
$userLanguage = Yii::app()->user->getState('lang');
$result = array();
$banks = self::model()->findAll();
foreach ($banks as $bank){
array_push($result, array(
'id' => $bank->id,
'title' => ($userLanguage == $bank->default_lang)?$bank->default_name:$bank->en_name,
'country' => Yii::t('countries',$bank->country),
));
}
return $result;
}
but it raise Object of class Accounts could not be converted to string error ,and in trace it occured at
public static function getIdByName($name)
{
return str_replace(array('[]', '][', '[', ']', ' '), array('', '_', '_', '', '_'), $name);
}
in Chtml.php file.where is the problem?I searched a lot,but I couldn't solve the problem.
I understood where is my problem,I should use $form instead of Chtml.
in ZF1 form and form elements had a setDescription method that was outputted as <p>description here</p> in view ....
ZF2 seems that doesn't have this method
so my question is how can i add description to form elements ?
this is my view :
<div>
<?
$form = $this->form;
$form->prepare();
echo $this->form()->openTag($form);
foreach ($form->getElements() as $el) {
?>
<div class="form_element">
<?=$this->formRow($el);?>
</div>
<?
}
echo $this->partial('system/form/buttons_form_part.phtml', array('form' => $form));
echo $this->form()->closeTag();
?>
</div>
Using ZF 2.1.5, one solution might be setOptions().
In the form definiton:
$file = new Element\File('file');
$file->setLabel('Photo (.jpg, .gif, .png)');
$file->setOptions(array('description' => 'test description'));
…
When rendering the form element:
$element = $form->get(…);
var_dump($element->getOptions());
Will give you access to:
array(1) { ["description"]=> string(16) "test description" }
If you mean a label for an Element you can use the setLabel method when you create the form (in Controller).
$name = new Element('name');
$name->setLabel('Your name');
Or if you use an array to create your form elements use this:
array(
'spec' => array(
'name' => 'name',
'options' => array(
'label' => 'Your name',
),
'attributes' => array(
'type' => 'text'
),
)
),
Here is a link:
enter link description here