How to add class to the Html::ul() method. I have the following code:
<?= Html::ul($posts, ['item' => function($item, $index) {
return Html::tag(
'li',
$this->render('post', ['item' => $item]),
['class' => 'post']
);
}]) ?>
I tried below code:
<?= Html::ul($posts, ['item' => function($item, $index) {
return Html::tag(
'li',
$this->render('post', ['item' => $item]),
['class' => 'post']
);
}], [ 'class' => 'myclass' ]) ?>
But it's rendering the ul as following:
<ul 0-class="myclass">
Nothing is mentioned in the yii2 document.
Any help would be appreciable. Thanks.
Below code worked! Might be helpful for someone.
echo Html::ul($posts, ['item' => function($item, $index) {
return Html::tag(
'li',
$this->render('post', ['item' => $item]),
['class' => 'post']
);
}, 'class' => 'myclass']);
Related
I'm using 2 widgets in one page, Both are using listview, one is for blogs and another one is for pages and both are using ScrollPager. I want to show more button in each separately. The problem is the show more button is shown only for pages but not for blog, if I remove the page widget it will display for blogs. I'm trying to display show more for pages and show more for blogs. I tried by pageParam but the problem still exist.
widget:
class UserPagesWidget extends \yii\base\Widget
{
public $usr_id;
public function run()
{
$dataProviderContent = new ActiveDataProvider([
'query' => Post::find()->Where(['user_id' => $this->usr_id])
->orderBy(['post_crdate' => SORT_DESC]),
'pagination' => [
'pageParam' => 'contentPagination',
'pageSize' => 5,
],
]);
return $this->render('/user/widget/pstList', [
'dataProviderContent' => $dataProviderContent,
]);
}
}
Render file:
<?= ListView::widget([
'dataProvider' => $dataProviderContent,
'summary'=>'',
'itemOptions' => ['class' => 'item'], // LY - FOR ( LOAD MORE )
'emptyText' => 'No Content',
'itemView' => function ($model, $key, $index, $widget) {
return $this->render('/user/profileContent/usr-pst-content',['model' => $model]);
},
'pager' => ['class' => \kop\y2sp\ScrollPager::className()],
]);
?>
Blog Widget:
class UserBlogsWidget extends \yii\base\Widget
{
public $usr_id;
public function run()
{
$dataProviderBlog = new ActiveDataProvider([
'query' => Blog::find()->Where(['user_id' => $this->usr_id])
->orderBy(['blog_update' => SORT_DESC]),
'pagination' => [
'pageParam' => 'blogPagination',
'pageSize' => 6,
],
]);
return $this->render('/user/widget/blgList', [
'dataProviderBlog' => $dataProviderBlog,
]);
}
}
Blog render file:
<?= ListView::widget([
'dataProvider' => $dataProviderBlog,
'summary'=>'',
'itemOptions' => ['class' => 'item'], // LY - FOR ( LOAD MORE )
'emptyText' => 'No Blogs',
'itemView' => function ($model, $key, $index, $widget) {
return $this->render('/user/profileContent/usr-blg-content',['model' => $model]);
},
'pager' => ['class' => \kop\y2sp\ScrollPager::className()],
]);
?>
Both widgets are called in one view file.
<div class="row chn-row">
<div class="col-sm-12" style="padding:15px">
<!-- separing -->
<?= UserPagesWidget::widget(['usr_id' => $model->user_id]) ?>
<!-- separing -->
</div>
<div class="col-sm-12" style="padding:15px">
<?= UserBlogsWidget::widget(['usr_id' => $model->user_id]) ?>
</div>
</div>
The problem is the show more button is displayed only for pages.
You need to adjust selectors to distinguish between two widgets. By default ScrollPager uses some generic selector (.list-view) that will match all list views. You should explicitly set ID for ListView widgets and use it in ScrollPager config as selector for widget initialization.
<?= ListView::widget([
// ...
'dataProvider' => $dataProviderBlog,
'options' => ['id' => 'blog-list-view'],
'pager' => [
'class' => \kop\y2sp\ScrollPager::className(),
'container' => '#blog-list-view',
'paginationSelector' => '#blog-list-view .pagination',
],
]) ?>
<?= ListView::widget([
// ...
'dataProvider' => $dataProviderContent,
'options' => ['id' => 'content-list-view'],
'pager' => [
'class' => \kop\y2sp\ScrollPager::className(),
'container' => '#content-list-view',
'paginationSelector' => '#content-list-view .pagination',
],
]) ?>
See https://kop.github.io/yii2-scroll-pager/#general-options
Following code should search records for date. But whatever I click,main branch in condition will be executed,so I will get outprinting:
choice_date is false
It will be searched for records which are <=department_created_date,but never for records >=department_created_date
Any ideas,how to fix this?
Here is my RadioList
<?php
use yii\helpers\Html;
use kartik\grid\GridView;
use yii\widgets\ActiveForm;
$this->title = Yii::t('app', 'Departments');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="departments-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php
$form = ActiveForm::begin();
$model = new backend\models\DepartmentsSearch();
?><?= $form->field($model, 'choice_date')->radioList(array(0 => 'Before', 1 => 'After'))->label('Please, choose Datesearching!'); ?>
<p><?= Html::a(Yii::t('app', 'Create Departments'), ['create'], ['class' => 'btn btn-success']) ?></p>
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'branches_branch_id',
'label' => Yii::t('app', 'Branch'),
'value' => function($model) {
if ($model->branches_branch_id) {
return $model->branchesBranch->branch_name;
} else {
return NULL;
}
},
'filterType' => GridView::FILTER_SELECT2,
'filter' => backend\models\Branches::getBranchList(),
'filterWidgetOptions' => [
'pluginOptions' => ['allowClear' => true],
],
'filterInputOptions' => ['placeholder' => 'Branch', 'id' => 'grid-Branch-search-rechtsart']
],
'branchesBranch.branch_name',
'department_name',
[
'attribute' => 'companies_company_id',
'label' => Yii::t('app', 'Company'),
'value' => function($model) {
if ($model->companies_company_id) {
return $model->companiesCompany->company_name;
} else {
return NULL;
}
},
'filterType' => GridView::FILTER_SELECT2,
'filter' => backend\models\Companies::getCompanyList(),
'filterWidgetOptions' => [
'pluginOptions' => ['allowClear' => true],
],
'filterInputOptions' => ['placeholder' => 'Company', 'id' => 'grid-Company-search-rechtsart']
],
'department_created_date',
['class' => 'yii\grid\ActionColumn'],
],
]);
?>
</div>
and here is my searching class:
<?php
namespace backend\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\Departments;
class DepartmentsSearch extends Departments {
public $choice_date;
public function rules() {
return [
[['department_id'], 'integer'],
[['choice_date'], 'boolean'],
[['department_name', 'department_created_date', 'department_status', 'companies_company_id', 'branches_branch_id'], 'safe'],
];
}
public function scenarios() {
return Model::scenarios();
}
public function search($params) {
$query = Departments::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate())
return $dataProvider;
/*
Whatever I click in RadioList,property will be 0,so I'll get setFlash->'choice_date is false'
*/
if ($this->choice_date == 0) {
Yii::$app->session->setFlash('If-branch is false', 'choce_date is false');
$query->andFilterWhere(['<=', 'department_created_date', $this->department_created_date]);
} else {
Yii::$app->session->setFlash('if-branch is true', 'choice_date is true');
$query->andFilterWhere(['>=', 'department_created_date', $this->department_created_date]);
}
$query->joinWith('companiesCompany');
$query->joinWith('branchesBranch');
$query->andFilterWhere(['like', 'department_name', $this->department_name])
->andFilterWhere(['like', 'companies.company_name', $this->companies_company_id])
->andFilterWhere(['like', 'branches.branch_name', $this->branches_branch_id])
->andFilterWhere(['like', 'department_status', $this->department_status]);
return $dataProvider;
}
}
Further ideas,how to fix this misery?
P.S.: For Bizley's help, here is an extraction of my controller:
class DepartmentsController extends Controller{
public function actionIndex(){
$searchModel = new DepartmentsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
further methods are irrelevant,here. Why will property of date-record not be rendered in view?
}
Your $model in the view is RadioForm so the form sends this field as RadioForm[choice_date].
But you are expecting choice_date from DepartmentsSearch model and this field is left empty.
Remove the RadioForm model, it's totally unneeded. Use DepartmentsSearch model in the form view.
Update:
Since OP finally told that this is a GridView I can update my answer to suit him.
There is filterSelector property in GridView you can use - you can set there additional jQuery selectors that will be used for filtering.
Here is updated code with comments below:
<?php $form = ActiveForm::begin();
echo $form->field($searchModel /* 1 */, 'choice_date')->radioList([
0 => 'Before', 1 => 'After'
], ['itemOptions' => ['class' => 'choiceRadio' /* 2 */]])->label('Please, choose Datesearching!');
ActiveForm::end(); /* 3 */ ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'filterSelector' => '.choiceRadio', /* 4 */
// ...
This is the $searchModel I was talking about. You don't need any other model here.
You can set here any CSS class you want, it's for filterSelector.
Don't forget to close form like you did in your original code.
This is the selector for the radio input. Mind the notation - . for CSS class, # for CSS id.
This is my gridview
<?php Pjax::begin(['id' => 'vouchers']) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
['header'=>'Cancel Voucher',
'value'=> function($data)
{
return Html::a(Yii::t('app', ' {modelClass}', [
'modelClass' =>"Cancel",
]), ['generalentries/cancel_voucher','voucher_no'=>$data->voucher_no],
['class' => 'btn btn-xs btn-danger',
'onclick'=>"return confirm('Are you Sure you want to CANCEL this Voucher?');"]
);
},
'format' => 'raw'
],
],
]); ?><?php Pjax::end() ?>
and this is my cancel_voucher action
public function actionCancel_voucher( $voucher_no )
{
$entries = TabGLEntry::find()->where(['voucher_no' => $voucher_no])->all();
foreach($entries as $entry)
{
$entry->update_note = FALSE;
$entry->is_deleted = 1;
$entry->save();
}
$this->redirect( Yii::$app->request->referrer );
}
How can i update the grid view using pjax when i click on the Cancel Button?
I tried giving button an id and then on the onClick event of the button i tried to update the gridview using $.pjax.reload({container:"#vouchers"}); But it didn't work
EDIT
This is how i tried
<?php
\yii\widgets\Pjax::begin([
'id' => 'countries',
'timeout' => false,
// 'enablePushState' => false,
'clientOptions' => ['method' => 'POST']])
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
['header'=>'Cancel Voucher',
'value'=> function($data)
{
return Html::a(Yii::t('app', ' {modelClass}', [
'modelClass' =>"Cancel",
]), ['generalentries/cancel_voucher','voucher_no'=>$data->voucher_no],
['class' => 'btn btn-xs btn-danger','id' =>'cancel_voucher',
'onclick'=>"return confirm('Are you Sure you want to CANCEL this Voucher?');"]
);
},
'format' => 'raw'
],
<?php
$this->registerJs(
'jQuery(document).ready(function($){
$(document).ready(function () {
$("#cancel_voucher").on("click", function(){
alert("hai");
$.pjax.reload({container:"#countries"});
});
});
});'
);
?>
Issue
You have to return content, which will be showing in Pajax block instead of $this->redirect( Yii::$app->request->referrer );
public function actionCancel_voucher( $voucher_no )
{
$entries = TabGLEntry::find()->where(['voucher_no' => $voucher_no])->all();
foreach($entries as $entry)
{
$entry->update_note = FALSE;
$entry->is_deleted = 1;
$entry->save();
}
if (Yii::$app->getRequest()->getIsPjax()) {
//render part of view with grid
return $this->renderAjax($url, $params);
}
else { $this->redirect( Yii::$app->request->referrer ); }
}
For correct pajax redirect you have to setup X-Pjax-Url header.
if (Yii::$app->getRequest()->getIsPjax()) {
$response = Yii::$app->getResponse();
$destination = Url::to($url);
$response->getHeaders()->set('X-Pjax-Url', $destination);
$response->getHeaders()->set('Location', $destination);
return null;
}
How to access variable ($imodel->grade) in tampil.php page and then subsequently put on the page view.php with post method. Sorry for the question, i'm newbie in OOP, MVC and Yii. Please help me guys
Here is The code of actionIndex:
public function actionIndex()
{
$imodel = new Grade();
if($imodel->load(Yii::$app->request->post()))
{
$imodel->load(Yii::$app->request->post());
$imodel->grade;
$query = Nilaiharian::find(); //select nis, $grade, nama from nilaiharian
$dataProvider = new ActiveDataProvider(['query' => $query,]);
return $this->render('tampil', ['imodel' => $imodel, 'dataProvider' => $dataProvider,] );
}
else
{
return $this->render('index', ['imodel' => $imodel, ] );
}
}
And this is a code of tampil.php
<?php
echo "Nilai untuk ".$imodel->grade;
echo "<br>";
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'nis',
$imodel->grade,
'nama',
['class' => 'yii\grid\ActionColumn',
'template' => '{view}{update}{delete}',
'buttons' => [
'view' => function ($url, $model) {
return Html::a('<i class="glyphicon glyphicon-eye-open"></i>',
$url, ['title' => Yii::t('app', 'Show'), 'name'=>'view', 'value'=>'view', 'style' => 'margin-right:10px;', ]);
},
'update' => function ($url, $model) {
return Html::a('<i class="glyphicon glyphicon-pencil"></i>',
$url, ['title' => Yii::t('app', 'Edit'), 'name'=>'update', 'style' => 'margin-right:10px;', ]);
},
'delete' => function ($url, $model) {
return Html::a('<i class="glyphicon glyphicon-trash"></i>',
$url, ['title' => Yii::t('app', 'Delete'), 'name'=>'delete', 'data-confirm'=>'Are you sure you want to delete this record?']);
}
]
],
],
]);
?>
It there any way to make 'data-confirm' => Please enter the number' not just confirm but some like JS prompt and get imputed values to sent it to controller?
<?php \yii\widgets\Pjax::begin(['id' => 'pjax-orders_table','clientOptions' => ['method' => 'POST'], 'enablePushState'=>false]) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'id'=>'orders_table',
'columns' => [
['class' => 'yii\grid\SerialColumn'],
///some columns
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update} {delete}{some}',
'buttons' => [
'some' => function ($url,$model,$key) {
if($model->status=='not confirm')
{
return Html::a('<span class="glyphicon glyphicon-trash"</span>',['my/some', 'id' => $model->id],[
'title' => Yii::t('yii', 'Delete'),
'data-confirm' => Please enter the number',
'data-method' => 'post',
]);
}
},
],
],
],
]); ?>
<?php \yii\widgets\Pjax::end() ?>
In controller
public actionSome()
{ $dataProvider = new ActiveDataProvider();
$dataProvider->query = Orders::find()->all();
return $this->render('some',['dataProvider'=>$dataProvider]);
}
instead of Html::a() use Html::button()where button id = some_item_id and then write this JS code
$('.buttons_class').click(function(){
var selection;
do{
selection = parseInt(window.prompt("Please enter a number from 1 to 100", ""), 10);
}
while(isNaN(selection));
if(!isNaN(selection))
{
$.post("some",{id:45,prompt_value:selection}, function(response)
{
console.log(response);
$.pjax.reload({container:'#pjax-orders_table'});
});
}
})