I am new to Yii2 and I am trying to figure out how it works.
Now my scenario is that I have a controller which queries a DB and outputs a $table, that I use for the Typeahead Widget:
Typehead::widget([
$data_select_name = ArrayHelper::getColumn($table, 'name');
'dataset' => [ [ 'local' => $data_select_name, 'limit' => 10 ] ]
]);
Now in the view I have a table which for each $item in $table outputs a row with the details.
How could I bind the Typeahead widget with the table, in order to inject these suggestions as the feed for the table generation? (I think with ajax, but let me know)
Thanks!
Related
I am looking at the doc page: https://backpackforlaravel.com/docs/4.1/crud-filters.
The simple question is: where must I call $this->crud->filters()? I tried to add at the end of setupListOperation() in my controller, but I don't see filters over my table. For curiosity: I assigned esit of a call to a var and dumped it, and it's an empty collection.
protected function setupListOperation()
{
CRUD::setFromDb(); // columns
CRUD::column('assignmentTypes')->type('relationship');
/**
* Columns can be defined using the fluent syntax or array syntax:
* - CRUD::column('price')->type('number');
* - CRUD::addColumn(['name' => 'price', 'type' => 'number']);
*/
dd($this->crud->filters()); // <<<<------------- EMPTY COLLECTION!
}
Please note that manually adding a filter works!
$this->crud->addFilter([
'type' => 'text',
'name' => 'first_name',
'label' => 'First Nane'
], false,
function ($value) { // if the filter is active
$this->crud->addClause('where', 'first_name', 'LIKE', "%$value%");
}
);
But I ask you why filters() doesn't do something automatically.
You can use $this->crud->filters() or CRUD::filters() anywhere you'd like, but it makes most sense to use it in your setupListOperation() like you have, but AFTER you've already added some filters. Before the addFilter() statements the result is an empty collection, because there are no filters.
Backpack Filters are enabled automatically if you add one to your List operation.
I am new to developing in yii2 (and yii). I have a query that returns name and last name of customers along with other columns. I am using activeDataProvider to display the records in a table but I need the name and lastname be displayed in one table column.
What I want to know is:
1. how do I concatenate the columns in yii2 activerecord?
2. How do I display the columns in one table columns made gridView?
below is what I have tried that none works:
1. $query->select(['customer.*', 'fullname'=>concat('customer.name'," ",'customer.lastname')]);
2. 'dataProvider' => $model['dataProvider'],
'columns' => [
'id',
'name'.' '.'lastname',
'customer_orders',
'created_at:datetime'
Use GridView like below.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
[
'attribute' => 'name',
'value' => function ($model) {
return $model->name . ' ' . $model->lastname;
}
]
'customer_orders',
'created_at:datetime'
]
]) ?>
Refer Yii2 GridView
I have the following dataprovider. Parentgroups is related to parentchildren via one-many relationship.
I am trying the concat the ChildFirstName and ChildLastName but it is not displaying. Only ID and ParentID is displaying.
$query = Parentgroups::find();
$query->with(
[
'parents' => function( $query){
$query->select([ 'Name', 'ID' ]);
},
'parentchildren' => function ( $query ){
$query->select([ new \yii\db\Expression("CONCAT('ChildFirstName', 'ChildLastName') as childName"), 'ID', 'ParentID' ]);
}
]
);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => false,
]);
I referred to this link: https://github.com/yiisoft/yii2/issues/8276
But it didn't worked for me. Any help would be appreciated. Thanks.
You need to add childName property into your child model, so it will be populated automatically by yii. Yii2 docs about selecting extra fields
I'm using Yii2 framework with advanced template.
I get problem with column alias in my controller file, here's my code:
$models = new ActiveDataProvider([
'query' => User::find()->select(['member'=>'fullname'])
]);
The above query equivalent with:
SELECT fullname AS member FROM User;
I send the data to the view using this code:
return $this->render('view', [
'model' => $models,
]);
I want to call the data in my view using GridView widget, here's my code:
echo GridView::widget([
'dataProvider' => $model,
'columns' => [
'member',
],
]);
However, I got an error that tell me the 'member' parameter is not defined.
How can I show the data of my query by calling the column name? (in my case it using alias)
I really appreciate any kind of helps!!
You should simply declare this attribute in your model :
class User extends ActiveRecord
{
public $member;
Read more : https://www.yiiframework.com/doc/guide/2.0/en/db-active-record#selecting-extra-fields
ActiveDataProvider works only with model attributes. member obviously is not presented there.
First of all, maybe it's better to refactor column names to be more clear instead of writing aliases? I don't see any benefit in your example.
If you nevertheless need to use aliases, as alternative for adding additional properties to class, you can work with them with help of ArrayDataProvider and SqlDataProvider.
Examples of usage:
ArrayDataProvider:
use yii\data\ArrayDataProvider;
$dataProvider = new ArrayDataProvider([
'allModels' => User::find()->select(['member' => 'fullname'])->all(),
]);
SqlDataProvider:
use yii\data\SqlDataProvider;
use yii\db\Query;
...
$query = (new Query())
->select(['member' => 'fullname'])
->from('users');
$command = $query->createCommand();
$dataProvider = new SqlDataProvider([
'sql' => $command->sql,
'params' => $command->params,
'totalCount' => $query->count(),
]);
For more details and features of usage please see official docs.
For your case it's better to use ArrayDataProvider, SqlDataProvider is for more complex queries.
In case of one alias and using model methods adding additional attribute as suggested by soju can be better.
But in my opinion it's useless and it's better to refactor column names in case of some ambiguity.
Mycart is stored in sessions which I wanna display in cgridview . I have used cgridview with dataprovider but can i do so with session value.Is it possible ?if not what would be the best way out to achieve so .If yes as I have no idea how do i proceed .
Please provide some guidance or examples.Do let me know if I'm not clear I'll make sure to clarify myself.
You could always create your own HTML table if you don't want to use data provider.
Yii CGridView require a data provider, therefore you can't use CGridView if you can't supply the data provider. Use CArrayDataProvider instead:
$sessionData = Yii::app()->user->getState('cart');
// $dataProvider = new CArrayDataProvider($sessionData); //throws undefined index:id exception
$dataProvider = new CArrayDataProvider($sessionData, array('keyField'=>'id'));
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'your-grid',
'dataProvider' => $dataProvider,
'columns' => array(
'item_name',
'qty',
// etc
)
));