Typeahead和AJAX查询不起作用

时间:2013-04-17 18:36:09

标签: php jquery twitter-bootstrap bootstrap-typeahead

我正在努力让 TB Typeahead 使用AJAX结果和PHP但不能。看到这是我为此做的代码:

$('#search').typeahead({
    limit: 10,
    minLength: 2,
    source: function() {
        return $.ajax({
            url: $('base').attr('href') + 'index.php?route=product/search/AjaxProductSearch',
            type: "GET",
            data: "search=" + $("#search").val(),
            success: function(result) {
                return result;
            }
        });
    }
});

例如用“co”调用的结果是:

["Combo AMD A4 (14,708.06 BsF)","Combo AMD A8 (17,900.05 BsF)","Combo
Core i3 (17,200.01 BsF)","Combo Core i5 (20,399.95 BsF)","Combo Intel
G465 (13,699.99 BsF)","Combo Intel G630 (15,199.97 BsF)"]

但选项没有显示,为什么?怎么了?此外,还可以添加一些方法来单击选项并转到特定的URL?

我还检查了this postthis other too,最后检查了this other,但没有结果

编辑感谢@dikirill提供的惊人帮助,我得到了部分代码:

$('#search').typeahead({
    limit: 10,
    minLength: 2,
    source: function(query, process) {
        $.ajax({
            url: $('base').attr('href') + 'index.php?route=product/search/AjaxProductSearch',
            type: "GET",
            data: "search=" + $("#search").val(),
            success: function(result) {
                return process(result.names);
            },
            dataType: 'json'
        });
    },
    updater: function(item) {
        var values = $('#search').data('values');
        alert(values);
        for (var index in values) {
            if (values[index].name == item) {
                location.href = values[index].href;
                break;
            }
        }
        return item;
    }
});

什么不起作用?好吧,当我从typeahead结果中立即点击任何元素时,我想要重定向到与该元素关联的URL。看到这个PHP函数是返回值的函数:

public function AjaxProductSearch() {
    $this->load->model('catalog/product');

    if (isset($this->request->get['search'])) {
        $search = $this->request->get['search'];
    } else {
        $search = '';
    }

    $output = array();
    if (isset($this->request->get['search'])) {
        $data = array(
            'filter_name' => $search
        );

        $product_total = $this->model_catalog_product->getTotalProducts($data);
        $results = $this->model_catalog_product->getProducts($data);

        $output = array();
        foreach ($results as $result) {
            $output['names'][] = $result['name'];
            $output['values'][] = array(
                'name' => $result['name'],
                'href' => $this->url->link('product/product', 'product_id=' . $result['product_id'] . $url)
            );
        }
    }
    echo json_encode($output);
}

然后返回一个结果就是这个结果:

{"names":["Combo AMD A4","Combo AMD A8","Combo Core i3","Combo Core i5","Combo Intel G465","Combo Intel G630"],"values":[{"name":"Combo AMD A4","href":"http:\/\/store.devserver\/amd-a4"},{"name":"Combo AMD A8","href":"http:\/\/store.devserver\/amd-a8"},{"name":"Combo Core i3","href":"http:\/\/store.devserver\/intel-corei3"},{"name":"Combo Core i5","href":"http:\/\/store.devserver\/intel-corei5"},{"name":"Combo Intel G465","href":"http:\/\/store.devserver\/intel-g465"},{"name":"Combo Intel G630","href":"http:\/\/store.devserver\/intel-g630"}]}

正如你所看到的那样,名称允许我按原样构建预先输入,而其他是我获得与href相关联的名称的地方。换句话说,如果我选择Combo AMD A4,那么我应该重定向到href":"http:\/\/store.devserver\/amd-a4并且我的代码无效,出了什么问题?

编辑现场演示为here

1 个答案:

答案 0 :(得分:1)

试试我的例子:

$('#search').typeahead({
    limit: 10,
    minLength: 2,
    source:function (query, process)
        $.ajax({
            url: $('base').attr('href') + 'index.php?route=product/search/AjaxProductSearch',
            type: "GET",
            data: "search=" + $("#search").val(),
            success: function(result) {
                if (typeof result.values != "undefined") {
                    $('#search').data('values', result.values);
                    return process(result.names);
                }
            },
            dataType: 'json'
        });
    },
    updater: function(item) {
        var values = $('#search').data('values');
        for (var index in values) {
            if (values[index].name == item) {
                location.href = values[index].href;
                break;
            }
        }
       return item;
    }
});

结束时缺少'dataType'。