I'm trying to set today's date as default date (already selected) whenever the form loads. I'm using 2amigos datepicker widget.
Following is the code of my datepicker -
<?= $form->field($model, 'ss_date')->widget(
DatePicker::className(), [
'id' => 'datepicker1',
'inline' => false,
'clientOptions' => [
'defaultDate' => date('Y-m-d'),
'autoclose' => true,
'todayHighlight' => true,
'format' => 'yyyy-mm-dd',
]
]);
?>
Following is the javascript that I'm trying
$(document).ready(function(){
var date = new Date();
var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());
$("#datepicker1").datepicker({dateFormat: 'yyyy-mm-dd'}).val(today);
});
I'm not getting any error, but the date is not getting selected when the form loads.
Related
Hi all i am newbie in yii2 need your advise
i use select2 widget, when i selected value it will throw another value that i had set in array before. so how can i do this in yii2. so far i doing this.i have try using jquery function using pluginevents in select2 but still stuck..here is my code
<?= $idnpwp = ArrayHelper::map(Mfwp::find()->all(),"id", "npwp", "nama_wp");?>
<?= $form->field($model, 'npwp')->widget(Select2::classname(), [
'language' => 'id',
'data' => $idnpwp,
'options' => ['placeholder' => 'Select a NPWP ...'],
'pluginOptions' => [
'allowClear' => true
],
'pluginEvents' => [
"change" => 'function(data) {
var data_id = $(this).val();
$("input#target").val($(this).val());
}',
]
]);
?>
<?= $form->field($model, 'nama_wp')->textInput(['id' => 'target']) ?>
how can i insert 'nama_wp' that already set in array into field nama_wp
thx for helping
First thing you need to change the
<?= $idnpwp = ArrayHelper::map(Mfwp::find()->all(),"id", "npwp", "nama_wp");?>
to
<?php $idnpwp = ArrayHelper::map(Mfwp::find()->all(),"id", "npwp", "nama_wp");?>
The you need to access the current value of the select2 via data.currentTarget.value, so change the code for change to the following.
<?php echo $form->field($model, 'npwp')->widget(Select2::classname(), [
'language' => 'id',
'data' => $idnpwp,
'options' => ['placeholder' => 'Select a NPWP ...'],
'pluginOptions' => [
'allowClear' => true
],
'pluginEvents' => [
"change" => 'function(data) {
var data_id = data.currentTarget.value;
$("#target").val(data_id );
}',
]
]);
?>
I am working in yii2 framework, I want to display date format mm/dd/yyyy,but in my database format was yyyy-mm-dd. In model-search I converted the format mm/dd/yy and I am getting result. After in felter search label it showing as yyyy-mm-dd,I want display mm/dd/yyyy format.Please give suggestion.
UserSearch model
$query = User::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$query->andFilterWhere(['like', 'start_date', trim($this->start_date)])
return $dataProvider;
index.php
<?= GridView::widget([
'dataProvider' => $dataProvider,
'tableOptions' => ['class' => 'table table-bordered table-hover '],
'headerRowOptions' => [
'class' => 'thead-light',
],
'rowOptions' =>function($model){
if($model->status == 0){
return ['class' =>'inactive-border-color',];
}
},
'filterModel' => $searchModel,
'columns' => [
[
'attribute' => 'start_date',
'format' => 'date',
'value' => 'termination_date',
'filterInputOptions' => [
class' => 'form-control',
'placeholder' => 'MM/DD/YYYY'
],
],
]
]); ?>
I want display mm/dd/yyyy format in filter search .
After you use your UserSearch model to create data provider, the value in its $start_date is not used for filtering anymore so you can change it to the format you need for output. You simply need to add following code in your controller's action anywhere between creating the data provider and rendering the view.
$searchModel->start_date = \Yii::$app->formatter->asDate($searchModel->start_date);
The other option is that instead of converting the value in $start_date property you will only convert it when creating data provider like this:
public function createDataProvider()
{
$query = User::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$query->andFilterWhere(['like', 'start_date', $this->normalizeDate($this->start_date)]);
return $dataProvider;
}
protected function normalizeDate($date)
{
//your code to convert date
return $convertedDate;
}
So I'm loading Kartik's Select2 widget in Kartik's DetailView using multiple input:
[
'attribute' => 'characters',
'format' => 'raw',
'type' => DetailView::INPUT_SELECT2,
'value' => function($form, $widget) {
<my_stuff_in_VIEW_mode>
},
'widgetOptions' => [
'options' => ['multiple' => true, 'placeholder' => Yii::t('app', 'Select...')],
'pluginOptions' => [
'allowClear' => true,
'minimumInputLength' => 3,
'maximumInputLength' => 20,
'language' => Yii::$app->language,
'ajax' => [
'url' => \yii\helpers\Url::to(['myAjaxServer']),
'dataType' => 'json',
'delay' => 250,
'data' => new JsExpression('function(params) { return {q:params.term, p:params.page}; }'),
'cache' => true,
],
'escapeMarkup' => new JsExpression("function (markup) { return markup; }"),
'templateResult' => new JsExpression("function (characters) { return '<b>' + characters.text + '</b>'; }"),
'templateSelection' => new JsExpression("function (characters) { return characters.text; }"),
],
],
],
When it comes to Kartik's DetailView, VIEW mode correctly shows my list of characters given by value, but when entering EDIT mode I got this:
which obviously is not what I expected: I want my characters' names rather than their IDs. However if I select new characters from this very same input dropdown, new tags are added which properly show the new names (thus this issue happens when updating data). After I confirm the new data and then try to update, new characters are shown with their IDs again.
JSON data is as follows:
{"results":[{"id":"8","text":"Character8"},{"id":"5","text":"Character5"}],"pagination":{"more":true}}
Any ideas?
UPDATED WITH SOLUTION BY KARTIK BELOW:
Add initValueText as an array of desired attribute values:
'initValueText' => ArrayHelper::getColumn($model->characters, 'name'),
You need to set Select2::initValueText when using Select2 with ajax to show the displayed name instead of id. For multiple select this needs to be setup as an array.
I'm using krajee DatePicker.
use yii\helpers\Html;
use yii\widgets\DetailView;
use yii\web\View;
use yii\data\ActiveDataProvider;
use kartik\widgets\DatePicker;
use yii\web\JsExpression;
echo DatePicker::widget([
'name' => 'dp',
'id' => 'dp',
'type' => DatePicker::TYPE_INLINE,
'value' => '',
'pluginOptions' => [
'startDate' => $model->fecha_inicio,
'format' => 'yyyy-mm-dd',
'beforeShowDay' => new \yii\web\JsExpression("function(date) {
startDate = new Date('".$model->fecha_inicio."');
endDate = new Date('".$model->fecha_fin."');
between=startDate<=date && endDate>=date;
console.log(date+' '+ (between));
dateFormat = date.getUTCFullYear() + '-' + ('0'+(date.getUTCMonth()+1)).slice(-2) + '-' + ('0'+date.getUTCDate()).slice(-2);
if (dateFormat == '".$model->fecha_inicio."') {
return {classes: 'start-date', tooltip: 'Title'};
}
if (dateFormat == '".$model->fecha_fin."') {
return {classes: 'end-date', tooltip: 'Title'};
}
if (between) {
return {classes: 'in-range available'}; //create a custom class in css with back color you want
}
return false;
}"),
],
'options' => [
// you can hide the input by setting the following
'class' => 'hide'
]
]);
Is there any way to render the DateRangePicker without it being able to recieve user input? (eg. no hover, no date selection). I want to render it on a webpage just to inform the user of a range, but the fact that the user can interact with it feels awkward in this scenario.
You can try to add "readonly" to the options. Just like this:
'options' => [
// you can hide the input by setting the following
'class' => 'hide',
'readonly' => 'readonly'
]
Try to use in your options array attribute disabled. SO it would be
'options' => [
'disabled' => 'true',
'class' => 'hide'
]
Well, this helped me do the trick, based on this answer.
Basically, I ended up using a wrapping div with the style:
<div style="pointer-events:none;"> ... </div>
This solved it easily and directly, and it seems to have decent cross-browser support.
<?php
echo '<div class="well well-sm" style="background-color: #fff; width:245px; pointer-events:none;">';
$date = new \DateTime($model->fecha_inicio);
$days = Proceso::calcularDias ($model->fecha_inicio,$model->fecha_fin);
echo DatePicker::widget([
'name' => 'dp',
'id' => 'dp',
'type' => DatePicker::TYPE_INLINE,
'value' => '',
'pluginOptions' => [
'defaultViewDate' => ([
'year'=>(int)$date->format('Y'),
'month'=>$date->format('m')-1,
'day'=>(int)$date->format('d')
]),
'format' => 'yyyy-mm-dd',
'beforeShowDay' => new JsExpression("function(date) {
startDate = new Date('".$model->fecha_inicio."');
endDate = new Date('".$model->fecha_fin."');
between=startDate<=date && endDate>=date;
dateFormat = date.getUTCFullYear() + '-' + ('0'+(date.getUTCMonth()+1)).slice(-2) + '-' + ('0'+date.getUTCDate()).slice(-2);
if (dateFormat == '".$model->fecha_inicio."') {
return {classes: 'start-date', tooltip: 'Title'};
}
if (dateFormat == '".$model->fecha_fin."') {
return {classes: 'end-date', tooltip: 'Title'};
}
if (between) {
return {classes: 'in-range available'}; //create a custom class in css with back color you want
}
return false;
}"),
],
'pluginEvents'=>[
],
'options' => [
'disabled' => 'true',
// you can hide the input by setting the following
'class' => 'hide'
]
]);
echo '</div>';
?>
I am using kartik yii2 editable extension to edit inline in gridview.
The extension is working fine.
Please refer this screen-shot link [http://awesomescreenshot.com/00753dvb73][1]
In this screen-shot the source field is a dropdown and I want the value of source instead id its id
My View
use kartik\editable\Editable;
[
'attribute'=>'source',
'format'=>'raw',
'value'=> function($data){
//$s = $data->getBacklog_source();//var_dump($s);exit;
return Editable::widget([
'name'=>'source',
'model'=>$data,
'value'=>$data->source,
'header' => 'Source',
'type'=>'primary',
'size'=> 'sm',
'format' => Editable::FORMAT_BUTTON,
'inputType' => Editable::INPUT_DROPDOWN_LIST,
'data'=>$data->getSource(), // any list of values
'options' => ['class'=>'form-control', 'prompt'=>'Select Source'],
'editableValueOptions'=>['class'=>'text-danger'],
'afterInput' => Html::hiddenInput('id',$data->id),
]);
}
],
The relation I made is:
public function getSource()
{
$source = BacklogSource::find()->all();
return ArrayHelper::map($source, 'id', 'Source');
}
public function getBacklog_complexity()
{
return $this->hasOne(BacklogComplexity::className(), [
'id' => 'complexity'
]);
}
Thanks for help in advance
I got the solution something like this:
[
'attribute'=>'status',
'format'=>'raw',
'value'=> function($data){
$s = BacklogStatus::findOne($data->status);
return Editable::widget([
'name'=>'status',
'model'=>$data,
'value'=>$s->Status,
'header' => 'Status',
'type'=>'primary',
'size'=> 'sm',
'format' => Editable::FORMAT_BUTTON,
'inputType' => Editable::INPUT_DROPDOWN_LIST,
'data'=>$data->getStatus(), // any list of values
'options' => ['class'=>'form-control', 'prompt'=>'Select Source'],
'editableValueOptions'=>['class'=>'text-danger'],
'afterInput' => Html::hiddenInput('id',$data->id),
]);
}
],