2017-01-11 134 views
0

我有一個名爲employee_name的字段,根據此字段值,我想自動填充另一個字段employee_id。我搜索了,我發現this答案,並嘗試在我的表單上實現這一點,但我得到Error in ajax request。在我的表格jQuery代碼是yii2根據另一個字段自動填充相關字段

$('#emp').focusout(function() { 
     empName = this.value; 
     if (empName != '' || empName != null) { 
      $('#depcustomer-employee_name').val(empName); 
     } 
     $.ajax({ 
      url: '".yii\helpers\Url::toRoute("deposit/employeeid")."', 
      dataType: 'json', 
      method: 'GET', 
      data: {name: $(this).val()}, 
      success: function (data, textStatus, jqXHR) { 
       $('#depcustomer-employee_id').val(data.id); 
      }, 
      beforeSend: function (xhr) { 
       alert('loading!'); 
      }, 
      error: function (jqXHR, textStatus, errorThrown) { 
       console.log('An error occured!'); 
       alert('Error in ajax request'); 
      } 
     }); 
    }); 

我的控制器名稱爲Deposit和我的控制器代碼是

public function actionEmployeeid($name){ 
$model= app\modules\settings\models\DepEmployee::findOne(['employee_name'=>$name]); 
return \yii\helpers\Json::encode([ 
    'id'=>$model->employee_id 
]); 

可能是什麼原因可能是我的Ajax代碼是不工作?

我的表格很大。這裏的員工字段項的一部分

<div class="row"> 
       <div class="col-md-6">    
       <?= $form->field($model, 'employee_id')->textInput(['maxlength' => true]) ?> 
       </div> 
       <div class="col-md-6"> 
        <label for='emp'>Employee Name</label> 
        <?= Html::activeHiddenInput($model, 'employee_name')?> 

        <?php 
         echo AutoComplete::widget([ 
          'name' => 'employee_name', 
          'id' => 'emp', 
          'clientOptions' => [ 
           'source' => $dataEmp, 
           'autoFill'=>true, 
           'minLength'=>'2', 
           'select' => new JsExpression("function(event, ui) { 
            $('#depcustomer-name').val(ui.item.id); 
           }") 
          ], 
         ]); 
        ?>  
       </div> 
      </div> 
+0

你怎麼能指望jQuery來知道這一點:'警予\傭工\地址:: toRoute( 「存款/僱員」)' ?請發佈您的表單以獲得更多幫助。 – stfsngue

+0

@stfsngue所以,我應該用像'http://localhost/testingDeposit/backend/web/index.php?r = deposit%2Fdeposit%2Femployeeid'這樣的絕對網址來代替它 – gojiraki

+0

試試這個答案。 – stfsngue

回答

1

根據您的自動填充數據,您已有employee_id。所以不需要發出Ajax請求來獲得員工ID。

DepEmployee型號

public static function getEmpData() 
{ 
    $dataEmp = DepEmployee::find() 
     ->select(['employee_name as value', 'employee_name as label','employee_id as id']) 
     ->asArray() 
     ->all(); 

    return $dataEmp; 
} 

_form

<?= AutoComplete::widget([ 
     'name' => 'employee_name', 
     'id' => 'emp', 
     'clientOptions' => [ 
       'source' => DepEmployee::getEmpData(), 
       'autoFill'=>true, 
       'minLength'=>'2', 
       'select' => new JsExpression("function(event, ui) { 
        $('#depcustomer-name').val(ui.item.id); 
        $('#depcustomer-employee_id').val(ui.item.id); 
       }") 
      ], 
]);?> 
0

如果我是你,我會做的事:

這裏的看法:

<?= $form->field($model, 'employeeName')->textInput([ 
    // I use onfocusout instead of focusout 
    'onfocusout' => ' 
     $.post("generateemployeeid?name="+$(this).val(), function(data) { 
      $("#employee_id_container").html(data); 
     }); 
    ', 
]) ?> 

<div id="employee_id_container"></div> // <- I will autofill here 

現在這裏是誰,將填補該功能ID輸入:(應該在你的控制器)

public function actionGenerateemployeeid($name) { 
    $employeeModel = DepEmployee::find() 
     ->where(['employee_name' => $name]) 
     ->one(); 

    if($employeeModel !== NULL) { 
     echo 'Employee ID: <input type="text" name="EmployeeID" value="'.$employeeModel->employee_id.'" readonly><br>'; 
    } 
    else { 
     // error 404 
    } 
} 

簡歷: jquery函數取出員工姓名併發送給將在數據庫中查找員工ID的控制器。然後發送一個帶有默認值(員工ID)的輸入文本作爲響應,並在窗體中加載該輸入。

相關問題