【问题标题】:How to create a new list for typeahead in twitter bootstrap depending on a previous selection?如何根据先前的选择在 twitter bootstrap 中为 typeahead 创建一个新列表?
【发布时间】:2012-10-07 08:13:57
【问题描述】:

我在这里看到了类似这样的 bootstrap typeahead 插件的分支:

https://gist.github.com/1866577

但是,我无法理解如何构建我的 Web 应用程序所需的表单元素。基本上,我希望我的 Web 应用程序模仿以下网站提供的功能:

slugbooks.com

我需要 3 个自动完成表单,就像他们在那个网站上的一样。但我想使用 Twitter Bootstrap,因为我的 Web 应用程序的其余部分都在 Bootstrap 中。我需要像大学一样的第一个自动完成功能......这很容易通过引导输入插件来完成。我已经弄清楚了这部分。现在,具有自动完成功能的下拉列表的下一个是它变得复杂的地方..

由于这个 StackOverflow 问题,我已经学会了如何制作预先输入的下拉菜单:

Adding a dropdown button to Twitter bootstrap typeahead component

现在我只需要一种方法来根据选择的大学和选择的部门动态生成下拉列表的数据源。此外,选择适当的课程(第 3 个下拉菜单)应将我重定向到特定页面(例如:site.com/college/coursename.html)

我将如何使用 Twitter Bootstrap Typeahead 完成此任务?

数据示例:

学院:

  • UTEXAS
  • 迈阿密
  • 南加州大学
  • 斯坦福

部门:

  • 1,2,3
  • 4,5,6
  • 7,8,9
  • A,B,C

课程:

  • 一个
  • b
  • c
  • d
  • e
  • f
  • g
  • h
  • i
  • j
  • k
  • l

警告:我是 javascript/AJAX 菜鸟,所以如果涉及 javascript/AJAX,请指导我...

【问题讨论】:

    标签: html autocomplete twitter-bootstrap frontend typeahead


    【解决方案1】:

    以下内容应该可以帮助您入门:Live demo (jsfiddle)

    // This wrapping is here only for visibility on JSFIDDLE (and because it's fun)
    !function(colleges, ajaxLoadDepartments){
        var $one = $('#one');
        var $two= $('#two');
    
        var loadSourceForTwoWith = function(item) {  // Function that reset the departments, accepts the name of the college
            $two.data('typeahead', false).val('');       // Clear departments
            ajaxLoadDepartments(item, function(departments) {     // This fonction should be the success of the ajax call
                $two.typeahead({                           // this ajax call should return the departments of the `item` college
                    source: departments                    // as an array of strings
                }).focus();
            });
        };
    
        $one.typeahead({
            source: colleges,
            updater: function(item) { // Item is selected, should return item
                loadSourceForTwoWith(item);
                return item;
            }
        });
    
    }( /* Don't bother too much with what's below, just use your what is above with your real code(ajax) and data */
        ['UTEXAS', 'UMIAMI', 'USC', 'STANFORD'],
        function(item, callback) {
            var jsonResponse = {
                'STANFORD' : ['X1','X2','X3'],
                'USC' : ['X4','X5','X6'],
                'UMIAMI' : ['X7','X8','X9'],
                'UTEXAS' : ['XA','XB','XC']
            };
    
            $.ajax('/echo/json/', {
                type: 'post',
                data: {json: JSON.stringify(jsonResponse[item])}
            }).done(function(data) {
                callback(data);
            });
        }
    );
    

    这个简单的示例向您展示了如何:

    • 为所选项目设置您自己的回调:请参阅updater
    • 重置预输入源(非常困难的方式):参见.data('typeahead', false)
    • 使用回调函数异步加载
    • 在 JS 中与人头混淆

    在任何复制/粘贴之前,您应该知道这仅使用标准的 typeahead 插件,并且没有对错误处理(输入、ajax 错误、延迟用户感知)做任何事情

    【讨论】:

    • 感谢您的意见!这很棒,但有 3 个缺点,其中一个对我来说似乎很重要。 1> 我想要 2 个下拉列表,就像您在 jsfiddle 中显示的那样,但只有第一个,“部门”应该填充源 html 文件中的数据..即数据源属性可以在 html 文件中内联填充,如在带有状态的 twitter 引导示例中 (twitter.github.com/bootstrap/javascript.html#typeahead) 第二个字段“部门”的数据应通过调用 mysql 数据库以安全的方式填充......即仅通过站点
    • 我发现这个 youtube 视频对于使用我在问题中提到的要点之一为我提供总体方向很有用.. 但我似乎无法将我的头绕在 javascript 部分周围.. youtube.com/watch?v=euj3aq2x74o ..我也想模仿slugbooks网站上的功能..所以单击箭头应该显示字段1的所有部门......然后单击field2上的箭头(当在field1中没有选择任何内容时)应该显示默认消息,如“请先在 field1 中选择一些东西”..
    • 最后,如果我选择一个部门 (field1),然后立即单击 field2 旁边的箭头 .. 我想查看该所选部门下列出的所有课程。
    • @sambehera 2nd 的数据仅对演示可见,但实际上是通过 JsFiddle 的 Ajax API 加载的。剩下的,我相信当你的 JS 技能提高时,你会明白的。我怀疑你会得到更多答案,因为这不是“我的代码”网站。
    • 我想这是您能提供的最大帮助。谢谢你的回答。
    猜你喜欢
    • 2014-11-30
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 2020-03-24
    相关资源
    最近更新 更多