2016-11-22 96 views
0

我使用TypeaheadBasic ::構件擴展名自動完成輸入,但我想在輸入限制。我想只允許包含在數組中的項目。如何限制typeaheadbasic yii2擴展

下面是我的代碼。

use kartik\typeahead\TypeaheadBasic; 
echo TypeaheadBasic::widget([ 
     'model' => $model, 
     'attribute' => 'client', 
     'data' => $clientData, 
     'options' => ['placeholder' => 'Filter as you type ...', 'id'=>'client_id_name'], 
        'pluginOptions' => ['highlight' => true], 
       ]); 

請給我的解決方案。感謝提前!

+0

爭奪w「選擇Repository」部分.http://demos.krajee.com/widget-details/typeahead。 – yafater

回答

1

如果你希望只允許用戶從一組選項中進行選擇,爲什麼不使用Select2。通過這種方式,您的用戶可以輸入以篩選選項並選擇其中一個選項。

+0

是的@marche你是對的! –

0

我的上述問題的另一種解決方法是如下:

,但我不知道這是正確的/標準方式或不..但對我來說:)做工精細

這裏是我的看法:typeheadbasic擴展

$clientData = $my_dynamic_data_arr; // this arr var i used in jquery script for chekcing if name is exists or not 
    echo TypeaheadBasic::widget([ 
         'model' => $model, 
         'attribute' => 'client', 
         'data' => $clientData, 
         'options' => ['placeholder' => 'Filter as you type ...', 'id' => 'client_profile'], 
         'pluginOptions' => ['highlight' => true], 
        ]); 

這裏是我的jQuery腳本來處理上述問題

var client_arr = $.makeArray(<?php echo json_encode(array_values($clientData)); ?>); // $clientData is php array variable from above 

    $("#clientProfileForm").submit(function(event) { 
     var client_name = $("#client_profile").val(); 
     if ($.inArray(client_name, client_arr) === -1) { 
      $("#clientProfileError").html("<spna style='color:red'>Client not exists</span>"); 
      return false; 
     } 
     if ($('#client_profile').val() === '') { 
      event.preventDefault(); 
      $("#clientProfileError").html("<spna style='color:red'>Client name is required</span>"); 
     } 
    });