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 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛