简体   繁体   English

引导程序提前无法正常工作

[英]Bootstrap typeahead not working

I am pulling stock symbols from Yahoo finance in a json object and I am trying to show them as a drop-down menu while the user starts typing the name of the company or the symbol in the search box . 我正在从Yahoo财务中的json对象中提取股票代码,并试图在用户开始在搜索框中输入公司名称或代码时将它们显示为下拉菜单。 Typeahead is not working as a drop down menu from the search box. Typeahead不能用作搜索框中的下拉菜单。 I think I am doing everything right.This is the code I have so far. 我认为我做的一切都正确,这是我到目前为止的代码。 Any help is appreciated. 任何帮助表示赞赏。

quote.js quote.js

$(document).ready(function() {

  // create autocomplete
  $('#form-quote input[name=symbol]').typeahead({

      // load autocomplete data from suggest.php
      source: function(query, callback) {
          $.ajax({
              url: '../suggest.php',
              type: 'POST',
              dataType: 'json',
              data: {
                  symbol: query
              },
              success: function(response) {
                  callback(response.symbols);

              }
          });
      }
  });

  // load data via ajax when form is submitted
  $('#form-quote').on('click', function() {

      // determine symbol
      var symbol = $('#form-quote input[name=symbol]').val();

      // send request to quote.php
      $.ajax({
          url: 'quote.php',
          type: 'POST',
          data: {
              symbol: symbol
          },
          success: function(response) {
              $('#price').text(response);
          }
      });


      return false;
  });

});

quote.php quote.php

<?php

//configuration
require("../includes/config.php");

//if form was submitted 

if($_SERVER["REQUEST_METHOD"] == "POST"){

    $stock = lookup(strtoupper($_POST["symbol"]));

    if(empty($_POST["symbol"])){

        //echo "You must enter a stock symbol";

    }else if($_POST["symbol"]){

    $price = number_format($stock['price'], 2);

    echo "A share of {$stock['name']} costs $$price";
    }
}

else{

    // render portfolio
render("stock_search.php", ["title" => "Get Quote"]);
}   
?>

quote_search.php quote_search.php

<form id = "form-quote" action="quote.php" method="post">
<fieldset>     
    <div class="control-group">
        <input name="symbol" autofocus autocomplete="off"  placeholder="Symbol"  type="text"/>
    </div>

    <div class="control-group">
        <button type="submit" class="btn">Get Quote</button>
    </div>

</fieldset>
<div id="price"></div>
<div id="suggestions"></div> 
</form>
<script type="text/javascript" src="js/quote.js" ></script>

suggest.php proposal.php

 <?php

// configuration
require("../includes/functions.php");

// if form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    // load suggestion data
$data = @file_get_contents("http://d.yimg.com/aq/autoc?query=  {$_POST['symbol']}&region=US&lang=en-US&callback=YAHOO.util.ScriptNodeDataSource.callbacks");

    // parse yahoo data into a list of symbols
$result = [];
    $json = json_decode(substr($data, strlen('YAHOO.util.ScriptNodeDataSource.callbacks('), -1));
    foreach ($json->ResultSet->Result as $stock)
        $result[] = $stock;

    echo json_encode(['symbols' => $result]);
}

?>

Typeahead takes only an array of strings as a source 提前输入仅将字符串数组作为源

  // i.e.
  ["INTC", "GOOG", "FB", /* etc */]

What your script does is create an array of the whole objects that Yahoo returns 您的脚本要做的是创建Yahoo返回的整个对象的数组

  // i.e.
  [
    {"symbol":"INTC","name": "Intel Corporation","exch": "NMS","type": "S","exchDisp":"NASDAQ","typeDisp":"Equity"},
    {"symbol":"INTC.MX","name": "Intel Corporation","exch": "MEX","type": "S","exchDisp":"Mexico","typeDisp":"Equity"},
    /* etc */
  ]

What you need to do is change your suggest.php so that the line: 您需要做的是更改您的proposal.php,以使该行:

foreach ($json->ResultSet->Result as $stock)
        $result[] = $stock;

becomes for exmaple: 例如:

foreach ($json->ResultSet->Result as $stock)
        $result[] = '('.$stock->symbol.') '.$stock->name;

尝试在Ajax中添加URL的完整路径,例如localhost/app/user/suggest.php

在使用json_encode进行响应之前,请确保将“ Content-type”设置为json。

header("Content-type: application/json");

Silly question but the json result you are expecting, does this comeback properly even when taking typeahead out of the equation? 愚蠢的问题,但您期望的json结果,即使从等式中提前输入,也可以正确恢复吗? Because the value passed to file_get_contents might be botched a bit unless that formatting bombed while pasting here. 因为传递给file_get_contents的值可能会被破坏一点,除非在粘贴到此处时炸毁了该格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM