複数の選択肢があるオートコンプリートテキストボックスを使用しようとしています。しかし、私はjQueryUiのオートコンプリートコンポーネントでいくつかの問題を経験しています。オートコンプリート候補のリストは正しく表示されません。 は、サーバーから私はこのような値を持っている:jQuery UI複数選択のオートコンプリートでHTMLページにデータが表示されない
[{"id":"1","text":"Test1 [1001]"},{"id":"2","text":"Test2 [1002]"}]
しかし、それはUIで表示されていないです。ここで
が私のHTMLコードは次のとおりです:
<label class="control-label">Group <span class="symbol required" aria-required="true"></span></label>
<input type="text" class="form-control" id="HSModels" tablename="ProductInfo" required />
<input type="hidden" class="form-control" id="HSModelsID" required />
マイスクリプト:
$("#HSModels")
.bind("keydown", function (event) {
if (event.keyCode === $.ui.keyCode.TAB &&
$(this).data("ui-autocomplete").menu.active) {
event.preventDefault();
}
})
.autocomplete({
minLength: 2,
source: function (request, response) {
$.getJSON("/api/Common/AutoCompleteListDataByTableName", { tableName: "ProductInfo", query: request.term },
response
);
},
search: function() {
// custom minLength
var term = extractLast(this.value);
if (term.length < 2) {
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function (event, ui) {
var HSModelsIDVal = $("#HSModelsID").val();
HSModelsIDVal += ", " + ui.item.id;
$("#HSModelsID").val(HSModelsIDVal)
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join(", ");
return false;
}
//});
});
function split(val) {
return val.split(/,\s*/);
}
function extractLast(term) {
return split(term).pop();
}
とサーバ側で:
[HttpGet]
public HttpResponseMessage AutoCompleteListDataByTableName(string tableName, string query = "")
{
GenericRepository<DropdownListData> repository = new GenericRepository<DropdownListData>(_csmDb);
try
{
var parameters = new List<SqlParameter> { new SqlParameter("@TableName", tableName), new SqlParameter("@QueryText", query) };
List<DropdownListData> dataList = repository.ExecuteStoreProcedureToList("[GetAutoCompleteListDataByTableName]", parameters);
return Request.CreateResponse(HttpStatusCode.OK, dataList, RequestFormat.JsonFormaterString());
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.OK, new Confirmation { output = "error", msg = ex.Message }, RequestFormat.JsonFormaterString());
}
}
UIで、それはこのようなものです 何かご意見は?私は別の問題に直面しました。つまり、データをもう一度ロードしていない問題を選択した後です。放火犯で
:
1回目:http://localhost:40315/api/Common/AutoCompleteListDataByTableName?tableName=ProductInfo&query=te
そして、2回目のGET: 私は何かを事前にlike this
感謝をしようと、次のよhttp://localhost:40315/api/Common/AutoCompleteListDataByTableName?tableName=ProductInfo&query=%2C+te
をGET。
ありがとう、それは完全に働いています:) – Metaphor
もう1つのクエリは、1つのアイテムが選択後にテキストボックスから削除された場合、隠しフィールドからIDを削除する方法? – Metaphor
@Metaphorかなりの労力を要します。ラベルに何かをラップし、 'x'ボタンを追加することを検討してください。この方法で、ラベルのインデックスを取得し、両方の配列からその項目をポップすることができます。 – Twisty