【发布时间】:2019-02-17 03:28:34
【问题描述】:
我有一个来自here 示例的工作输入元素,非常简单并且完美运行。
现在,我的问题是我需要处理的数据实际上有两个数据元素,比如{"Index":"<Index>","Value":"<Name>"}。
我需要在列表中显示一个像<index> - <Name> 这样的字符串,允许用户键入任何组合,包括索引和/或值的一部分,并且在选择时只保留索引(这是唯一的唯一列表的一部分)。
我将上述示例中的列表修改为:
var States = JSON.parse('[{"Index":"1","Value":"Alabama"}, ' +
'{"Index":"2","Value":"Alaska"}, ' +
'{"Index":"3","Value":"Arizona"}, ' +
'{"Index":"4","Value":"Arkansas"}, ' +
'{"Index":"5","Value":"California"}, ' +
'{"Index":"6","Value":"Colorado"}, ' +
'{"Index":"7","Value":"Connecticut"}, ' +
'{"Index":"8","Value":"Delaware"}, ' +
'{"Index":"9","Value":"Florida"}, ' +
'{"Index":"10","Value":"Georgia"}, ' +
'{"Index":"11","Value":"Hawaii"}, ' +
'{"Index":"12","Value":"Idaho"}, ' +
'{"Index":"13","Value":"Illinois"}] ' ) ;
并将 HTML 设置为:
<input name="states"
id="states"
type="text"
placeholder="enter a state"
ng-model="selected"
value="(state.Index)"
typeahead="(state.Value) for state in states | filter:$viewValue | limitTo:8"
class="form-control">
它没有做这项工作。
总而言之,预输入列表中的条目应如下所示:
1 - Alabama
2 - Alaska
...
当用户选择一个条目(比如第一个条目)时,脚本中收到的值应该是“1”(而不是“1 - Alabama”)。
编辑
我在下面实现了 tanmay 建议的代码,但不知何故,它对我不起作用。而不是得到(例如)6 - Colorado 我只得到一个-,这意味着没有数字(索引)或州名。有趣的是,显示的选项数量确实与我输入的内容相符(意思是,如果我输入 m,我只会得到一条 - 的记录,对应于 Alabama)。
这里是整个 HTML 和 JS 文件:
HTML:
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta name="viewpoint" content="with=device-width initial-scale=1.0">
<meta http-equiv="CACHE-CONTROL" content="NO-CACHE">
<title>MyTest - Partial</title>
<script src="Public_Libs/JQuery/jquery.js"></script>
<script src="Public_Libs/Bootstrap/js/bootstrap.min.js"></script>
<script src="Public_Libs/Bootstrap/js/bootstrap-select.min.js"></script>
<link rel="stylesheet" href="Public_Libs/Bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="Public_Libs/Bootstrap/css/bootstrap-select.min.css">
<link rel="stylesheet" href="Public_Libs/Bootstrap/css/bootstrap-theme.min.css">
<script src="Public_Libs/Angular/angular.min.js"></script>
<script src="Public_Libs/Angular/angular-route.min.js"></script>
<script src="Public_Libs/Angular/angular-sanitize.min.js"></script>
<script src="Public_Libs/Angular/angular-ui-bootstrap.min.js"></script>
<script src="Index_Controller.js"></script>
</head>
<body>
<div ng-app="angularTypeahead">
<script type="text/ng-template" id="customTemplate.html">
<a>
<span bind-html-unsafe="match.label.Index | typeaheadHighlight:query"></span> -
<span bind-html-unsafe="match.label.Value | typeaheadHighlight:query"></span>
</a>
</script>
<div class="container-fluid" ng-controller="TypeaheadCtrl">
<h2><img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/15309/angular-logo.svg" alt="Angular.js Logo"> Angular.js Typeahead</h2>
<div class="form-group">
<label for="states">Search for US States</label>
<input name="states" id="states" type="text"
typeahead-template-url="customTemplate.html"
placeholder="enter a state" ng-model="selected"
typeahead="state.Index as state for state in states | filter:$viewValue | limitTo:8"
class="form-control">
</div>
<pre>{{selected}}</pre>
<button class="btn btn-success" type="submit">Submit</button>
</div>
</div>
</body>
</html>
JS:
// https://angular-ui.github.io/
// setup app and pass ui.bootstrap as dep
var myApp = angular.module("angularTypeahead", ["ui.bootstrap"]);
// define factory for data source
myApp.factory("States", function() {
var states = JSON.parse('[{"Index":"1","Value":"Alabama"}, ' +
'{"Index":"2","Value":"Alaska"}, ' +
'{"Index":"3","Value":"Arizona"}, ' +
'{"Index":"4","Value":"Arkansas"}, ' +
'{"Index":"5","Value":"California"}, ' +
'{"Index":"6","Value":"Colorado"}, ' +
'{"Index":"7","Value":"Connecticut"}, ' +
'{"Index":"8","Value":"Delaware"}, ' +
'{"Index":"9","Value":"Florida"}, ' +
'{"Index":"10","Value":"Georgia"}, ' +
'{"Index":"11","Value":"Hawaii"}, ' +
'{"Index":"12","Value":"Idaho"}, ' +
'{"Index":"13","Value":"Illinois"}] ' ) ;
return states;
});
// setup controller and pass data source
myApp.controller("TypeaheadCtrl", function($scope, States) {
$scope.selected = undefined;
$scope.states = States;
});
我如何复制示例的代码(请参阅 tanmay 答案中的链接)以使其对我不起作用?
【问题讨论】:
标签: javascript angularjs typeahead