dongshan0202405 2014-03-19 15:40
浏览 33
已采纳

隐藏Bootstrap Typeahead中的值

I'm using Bootstrap Typeahead with PHP to display a list from a database using source.php:

<?php

if (isset($_POST['query'])) {
require( "config.php" );
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );

$query = $_POST['query'];

$sql = "SELECT * FROM articles WHERE title LIKE '%{$query}%'";
$array = array();

foreach ($conn->query($sql) as $row) {
    $array[] = $row['title'] . ',' . $row['id'];
}

// Return the json array
echo json_encode($array);
}
?>

You can see that I add both 'title' and 'id' to the array. All I want it to display in the typeahead is the title but I need the id for the links. Here is the JS:

$('#typeahead').typeahead({
source: function (query, process) {
    $.ajax({
        url: "source.php",
        type: "POST",
        data: 'query=' + query,
        dataType: 'JSON',
        async: true,
        success: function(data){
            process(data);
        }
    })
},
sorter: function (items) {
items.unshift(this.query); // Includes a new row with exact search query
return items.sort();
},
        updater: function (item) {
            document.location = "/companies/" + item.replace(/ /g, '-').replace(/\,/g,'/').toLowerCase() + "/";
            return item;
        }
});

In the line beginning document.location I replace the comma between the two values with a forward slash and it works e.g. /england/123/. But it's the typeahead display that shows it as England,123 rather than just England.

Any help would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • dongyun8891 2014-03-19 17:43
    关注

    OK managed to get a result with the following:

    PHP:

    if (isset($_POST['query'])) {
    require( "config.php" );
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    
    $query = $_POST['query'];
    
    $sql = "SELECT * FROM articles WHERE title LIKE '%{$query}%'";
    $array = array();
    
    foreach ($conn->query($sql) as $row) {
        $array[] = array('label' => $row['title'], 'id' =>$row['id']);
    }
    
    // Return the json array
    echo json_encode($array);
    }
    

    JS:

    $('#typeahead').typeahead({
    source: function (query, process) {
        $.ajax({
            url: "http://www.stubowker.com/amex/cms/source.php",
            type: "POST",
            data: 'query=' + query,
            dataType: 'JSON',
            async: true,
            success: function(data){
    objects = [];
        map = {};
        $.each(data, function(i, object) {
            map[object.label] = object;
            objects.push(object.label);
        });
        process(objects);
            }
        })
    },
    sorter: function (items) {
    items.unshift(this.query); // Includes a new row with exact search query
    return items.sort();
    },
        updater: function (item) {
            document.location = "/companies/" + map[item].label.replace(/ /g, '-').toLowerCase() + "/" + map[item].id + 
    
    "/";
            return item;
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line