My array
$data = [
'subcategoryName' => 'asd',
'fromdate' => $fromdate,
'todate' => $todate,
'amount' => $finalamount,
];
Array data provider
$provider = new ArrayDataProvider([
'allModels' => $data,
'pagination' => [
'pageSize' => 10,
],
'sort' => [
'attributes' => ['subcategoryName'],
],
]);
// get the rows in the currently requested page
$rows = $provider->getModels();
return $rows;
Now I want to display this data on my grid view but I am getting this error Call to a member function getCount() on array
Please tell me that how can I display my array on yii2 grid view
You should'nt return here result of function getModels(). I assume u pass this to GridView, but u have to pass ArrayDataProvider.
Change this:
$rows = $provider->getModels();
return $rows;
To this:
return $provider;
Related
while i am fetching this records then getting this error how to solve it ?
public function actionIndex()
{
$query = Site::model();
$pagination = new CPagination([
'defaultPageSize' => 5,
'totalCount' => $query->count(),
]);
$countries = $query->orderBy('name')
->offset($pagination->offset)
->limit($pagination->limit)
->all();
return $this->render('view', [
'countries' => $countries,
'pagination' => $pagination,
]);
}
you should distinguish between yii1 and yii2.from your codes,you used yii1 and yii2 together.
In the initialize function of OrdersTable.php, I have
$this->hasOne('CurrentReview', [
'className' => 'Reviews',
'finder' => 'latest',
'foreignKey' => 'order_id
]);
and in ReviewsTable.php, I have
public function findLatest(query $q)
{
return $q->orderDesc('id')->limit(1);
}
I am trying to get only the latest review associated with the order, but I am only ever able to get the first one.
$order = $this->Orders->get($id, ['contain' => [
'CurrentReview'
]]);
What am I missing?
I am almost getting what I need with
$order = $this->Orders->get($id, ['contain' => [
'Reviews' => function ($q) {
return $q->find('latest');
}
]]);
but it's inside an array that I don't want
Hi i have problem with pagination in linkerPage.
My problem https://gyazo.com/cc9b04c3114d6cfd09ef376b5d4374ac
I use https://github.com/kartik-v/yii2-tabs-x
when i want using pagination I gets raw html, how on the screen.
My console browser shows error
"Resource interpreted as Document but transferred with MIME type application/json:" {my url}
My controller
public function actionTestTabs()
{
Yii::$app->response->format = Response::FORMAT_JSON;
$MovieData = seanse::find()->innerJoinWith(['idMovie', 'idRoom']
)->where(['active' => 'active'])
->groupBy('name');
$date = seanse::find()->innerJoinWith(['idMovie', 'idRoom']
)->where(['active' => 'active'])
->orderBy('name')->all();
$dataProvider = new ActiveDataProvider([
'query' => $MovieData,
'pagination' => [
'totalCount' => 1,
'pageSize' => 1,
],
]);
return $this->renderPartial('test', [
'MovieData' => $MovieData,
'dataProvider' => $dataProvider,
'date' => $date,
]);
}
My view
foreach ($month as $item) {
$items[] = [
'label' => $item,
'linkOptions' => ['data-url' => Url::to(['repertuar/test-tabs'])],
];
}
echo TabsX::widget([
'align' => TabsX::ALIGN_CENTER,
'items' => $items,
]);
what is wrong ?
Whats the problem? You've setted "json" response header in line:
Yii::$app->response->format = Response::FORMAT_JSON;
Just remove this line from code or comment it.
I used below code to generate list in yii2
Controler code
$data = [['id'=>1, 'name'=>'name1'],
['id'=>2, 'name'=>'name2'],
['id'=>3, 'name'=>'name3'],
['id'=>4, 'name'=>'name4'],
['id'=>5, 'name'=>'name5'],
['id'=>6, 'name'=>'name6'],]
$provider = new ArrayDataProvider([
'allModels' => $data,
'pagination' => [
'pageSize' => 5,
],
'sort' => [
'attributes' => ['id', 'name'],
],
]);
$lists = $provider->getModels();
return $this->render('list', [
'provider' => $provider,
'lists' => $lists,
]);
View code
foreach($lists as $list){
.....
}
Pagination
\yii\widgets\LinkPager::widget([
'pagination'=>$provider->pagination,
]);
This code is working but i need search or filter option in this list
like name ='name2' search
I am new for yii2 framework Please suggest any suitable solution for this problem
Thanks
ArrayDataProvider implement sort only. You have 2 choice:
Fitler data before creating dataProvider
Extend ArrayDataProvider and implement filters.
I have a Create Payslip page and I want to pass a dynamic model since this page/view requires more than one earning items.
My actionCreateNew in my controller:
$session = Yii::$app->session;
$model = new Payslip();
$model->user_id = $id;
$model1 = new EarningDetails();
$model2 = new DeductionDetails();
$items = PayrollItems::find()->where(['store_id' => $id])->andwhere(['active' => 1])->andwhere(['payroll_type' => 'Earnings'])->all();
$count = count($items);
for($i = 1; $i <= $count; $i++){
$earning[$i] = new EarningDetails();
$earning[$i]->earning_item_id = $items[$i];
}
and here's my return statement in the same action controller:
return $this->render('create', [
'model' => $model,
'model1' => $model1,
'model2' => $model2,
'items' => $items,
]);
and in my view:
<?php
$earnings = PayrollItems::find()->where(['store_id' => $session['store_id']])->orwhere(['store_id' => null])->where(['payroll_type' => 'Earnings'])->all();
//$earningslistData = ArrayHelper::map($earnings,'payroll_items_id', 'payroll_name');
$earningslistData = ArrayHelper::map($earnings,'payroll_items_id',function($items, $defaultValue) {
return $items->payroll_name;
});
for($i = 1; $i <= $count; $i++) {
echo $form->field($earning[$i], 'earning_item_id')->widget(Select2::classname(), [
'data' => $earningslistData,
'options' => ['placeholder' => 'Select an Earning ', 'id' => 'earning_item'],
'pluginOptions' => [
'allowClear' => true
],
]);
}
?>
Problem is, how do I return a dynamic model? So I could use it in my view. In my return statement above, there's no $earning since it's a dynamic model and I don't know to return it.
If you have any other implementation about dynamic models, please pleas let me know.
You're halfway there.
First off, there is no Dynamic Model in your snippet, just an array of models. A Dynamic Model is an actual thing in Yii.
Now, if you want the $earning array you created in the controller to be available in the view, just add an entry to the array in the call to render():
return $this->render('create', [
'model' => $model,
'model1' => $model1,
'model2' => $model2,
'items' => $items,
'earning' => $earning,
]);
You may also want to move the definition of $earningslistData to the controller and pass it to the view in the same manner.