使用Select2 Ajax填充选择选项

时间:2015-09-04 08:29:19

标签: javascript ajax json jquery-select2

我使用Select2 Ajax在我的html选择

上应用自动完成功能

这是select2代码:

        <script>
            $(function(){
            $(".marques-multi").select2({
                minimumInputLength: 3,
                tags: [],
                ajax: {
                    url: "user/marques",
                    dataType: 'json',
                    type: "GET",
                    quietMillis: 50,
                    data: function (term) {
                        return term;
                    },
                      processResults: function (data) {
                        return {
                          results: data
                        };
                    },
                    transport: function (params, success, failure) {
                        var $request = $.ajax(params);

                        $request.then(success);
                        $request.fail(failure);

                        return $request;
                  }
                }
            });
        });
    </script>

这是我的HTML:

    <select class="form-control input-sm marques-multi" name="marque"></select>

这就是URL:&#34; user / marques&#34;数据(www.mywebsite.com/user/marques/marques?term=nike)返回:

[
{"id_marque":"50670","marque_name":"NIK HUBER GUITARS"},{"id_marque":"50671","marque_name":"NIKALAS CATLOW"},{"id_marque":"1","marque_name":"NIKE"},{"id_marque":"50672","marque_name":"NIKE"},
{"id_marque":"50673","marque_name":"NIKE"},{"id_marque":"50674","marque_name":"NIKE 6.0"},{"id_marque":"50675","marque_name":"NIKE ACCESSORIES"},{"id_marque":"50676","marque_name":"NIKE ACG"},{"id_marque":"50677","marque_name":"NIKE ACTION SPORTS"},{"id_marque":"50678","marque_name":"NIKE AIR MAX"},{"id_marque":"50679","marque_name":"NIKE BAIN"}
]

当我在选择输入中作为示例nik编写时,我在控制台中看到请求返回此json bellow而没有错误但是select没有填充此数据它保持空白。

1 个答案:

答案 0 :(得分:1)

如果可能,请将JSON上的数据属性从CRXSSFRow myRow = (XSSFRow) rowIter.next(); Iterator cellIter = myRow.cellIterator(); List cellRowList = new ArrayList(); while (cellIter.hasNext()) { XSSFCell originalCell = (XSSFCell) cellIter.next(); XSSFCell myCell = originalCell; try { // XSSFCell newCell = myCell; //( How to clone or copy a new one) XSSFCellStyle style = workbook.createCellStyle(); // the XSSFWorkbook from which you clone style.cloneStyleFrom(originalCell.getCellStyle()); myCell.setCellStyle(style); } catch (Exception e) { syso(e); } } 更改为id_marquemarque_name

所以来自:

id

text

Fiddle