2016-05-12 10 views
2

Ich benutze Datepicker von Kartik. Ich benutze es mit Feldbereich und ich möchte eine Suchfunktion daraus machen. Aber ich kann keinen Wert erhalten oder Wert auf eine Variable davon speichern. Wie man es löst?Erhalten Wert von Datepicker in yii2

Dies ist der Code:

<?php 
      // With Range 
      $layout3 = <<< HTML 
       <span class="input-group-addon" style="background: #e9e9e6;">Tanggal Awal</span> 
       {input1} 
       <!--<span class="input-group-addon" style="background: #e9e9e6;">aft</span> 
       {separator}--> 
       <span class="input-group-addon" style="background: #e9e9e6;">Tanggal Akhir</span> 
       {input2} 
       <span class="input-group-addon kv-date-remove" style="background: #e9e9e6;"> 
       <i class="glyphicon glyphicon-remove"></i> 
       </span> 
HTML; 

      echo DatePicker::widget([ 
       'type' => DatePicker::TYPE_RANGE, 
       'name' => 'dp_addon_3a', 
       'value' => date('Y-m-d'), 
       'name2' => 'dp_addon_3b', 
       'value2' => date('Y-m-d'), 
       // 'separator' => '<i class="glyphicon glyphicon-resize-horizontal"></i>', 
       'layout' => $layout3, 
       'pluginOptions' => [ 
        'autoclose' => true, 
        'format' => 'yyyy-mm-dd' 
       ] 
      ]); 
      $value = 'value'; 
      $value2 = 'value2'; 
      ?> 

Antwort

0

Sie werden Speicher in Ihrem Modell

<?php 
    $form = ActiveForm::begin([ 
     'action' => ['your_action'], 
     'method' => 'method_you_want', 
    ]); 
?> 
<?= 
    $form->field($model, 'dateattribute')->widget(DatePicker::className(), 
    [ 
     // config of your datepicker 
    ]); 
?> 

der Wert des Datumsauswahl tun. In Ihrem Controller haben Sie zB ein neues Objekt des Modells:

$model = new YourModel(); 
return $this->render('your_action', ['model' => $model]); 

Hoffe, dass ich klar war, zögern Sie nicht, wenn Sie nicht fragen

+0

Kann ich meine eigenen dateattribute machen? weil ich nur ein Attribut für das Datum habe. Wohingegen ich 2 Werte bekommen möchte. – RiefSapthana

0

Ihrer Ansicht :) versuchen, diese

use kartik\date\DatePicker; 
    <div class="col-sm-6"> 
       <?php 
        echo '<label class="control-label">Allocation Date</label>'; 
        echo DatePicker::widget([ 
         'name' => 'Allocation[allocation_datetime]', 
         'value' => date('d-m-Y'), 
         'type' => 1, 
         'pluginOptions' => [ 
          'format' => 'dd-mm-yyyy', 
          'todayHighlight' => true, 
          'class' => 'form-control', 
         ], 
        ]); 
       ?> 
     </div> 

In Ihrem Controller

+0

Es tut mir leid, aber es gibt einen Fehler undefinierte Variable Daten – RiefSapthana

+0

öffentliche Funktion actionYouractionname() { if (Yii :: $ app-> getRequest() -> isAjax) { $ data = Yii :: $ app- > Anfrage-> Post(); $ model-> allocation_datetime = date ('Y-m-d H: i: s', strtotime ($ data ['Allocation'] ['allocation_datetime'])); $ model-> save(); } –

0

Dies könnte für Sie hilfreich sein. Folgendes ist der Layout-Code.

<?php 

    use yii\bootstrap\ActiveForm; 
    use yii\helpers\ArrayHelper; 
    use yii\helpers\Url; 
    use yii\helpers\Html; 
    use kartik\date\DatePicker; 

    $model = new MyAllocation(); 
    $form = ActiveForm::begin([ 
       'id' => 'allocation', 
       'method' => 'post', 
       'options' => ['class' => 'form-horizontal', 
        'enctype' => 'multipart/form-data', 
       ], 
      ]); 
    ?> 

    <div class="row"> 
     <div class="col-sm-12"> 

     <div class="col-sm-6"> 
       <?php 
        echo '<label class="control-label">Allocation Date</label>'; 
        echo DatePicker::widget([ 
         'name' => 'WorkAllocation[allocation_datetime]', 
         'value' => date('d-m-Y'), 
         'type' => 1, 
         'pluginOptions' => [ 
          'format' => 'dd-mm-yyyy', 
          'todayHighlight' => true, 
          'class' => 'form-control', 
         ], 
        ]); 
       ?> 
     </div> 
     </div> 
    </div> 

    <div class="form-group"> 
    <?= Html::submitButton('Allocate', ['class' => 'btn btn-success', 'name' => 'submit']) ?> 
    </div> 

und im Anschluss ist der Code-Controller:

<?php 

use app\modules\work\models\WorkAllocation; 
public function actionYouractionname() 
     { 
      if (Yii::$app->getRequest()->isAjax) 
      { 
       $data = Yii::$app->request->post(); 

       // var_dump($data); 

       $model->allocation_datetime = date('Y-m-d H:i:s', strtotime($data['WorkAllocation']['allocation_datetime'])); 
       $model->save(); 
      } 
     } 
+0

Wie kann ich den Wert erhalten, ohne ein Modell zu haben? – RiefSapthana

+0

var_dump ($ _ POST); Sie erhalten alle Parameter einschließlich Datum in der Post. dann verwenden Sie es nach Ihren Bedürfnissen. –

+0

Es tut mir leid, aber ich habe versucht, den Controller auf der Oberseite, aber ich bekomme Fehler. Es heißt nicht "Allokation". Und wie wäre es mit dem Knopf für die Aktion? – RiefSapthana

Verwandte Themen