X-editable和Select2具有不工作的远程数据

时间:2022-10-25 20:39:36

I'm trying to make x-editable and select2 work with remote search.

我试图让x-editable和select2在远程搜索中工作。

Here is the HTML with Blade templating. The table id is "table".

这是带有刀片模板的HTML。表id是“table”。

<td>
    <a href="#" id="{{ $template->id }}" data-value="{{ $template->food_item_id }}" name="food_item" data-type="select2" data-pk="{{ $template->id }}" data-title="" class="editable-click food_item">
        {{ $template->food_item_name }}
   </a>
</td>

I'm using selector to setup x-editable

我用selector来设置x-editable

    $('#table').editable({
        selector: 'tbody tr td .food_item',
        url: '/update',

        select2: {
            cacheDataSource: true,
            allowClear: true,
            placeholder: 'Select food item',
            width: '200px',
            id: function (item) {
                return item.id;
            },
            ajax: {
                url: '/json',
                dataType: "json",
                type: 'post',
                data: function(term, page) {
                  return {
                    q: term
                  };
                },
                results: function(data, page) {
                  return {
                    results: data
                  };
                }
            }
        }
    });

The remote JSON is in the following format:

远程JSON格式如下:

[{"id":"1","text":"Jasmine Tea"},{"id":"2","text":"Green Tea"},{"id":"3","text":"Herbal Tea"},{"id":"4","text":"Tulsi Tea"},{"id":"5","text":"Chamomile Tea"},{"id":"6","text":"Mint Tea"},{"id":"7","text":"Ginger Green Tea"}]

I only get

我只得到

No results found

没有找到结果

Appreciate any help to make this work.

感谢你帮我做这项工作。

1 个解决方案

#1


1  

You must to change JSON.

必须更改JSON。

Remember to create the object results

记住要创建对象结果

Example:

例子:

{"results":[{"id":145,"text":" CHECKER "},{"id":146,"text":" CHEROCKE"},
{"id":147,"text":" CHERY "},{"id":148,"text":" CHEVROLET "},
{"id":410,"text":" JINCHENG"},{"id":502,"text":" MARCHETTI "},
{"id":511,"text":" MATCHELSS "},{"id":664,"text":" SAIC CHERY"},
{"id":680,"text":" SCHEUERLE WABCO "},{"id":853,"text":" WINCHEMG"}]}

#1


1  

You must to change JSON.

必须更改JSON。

Remember to create the object results

记住要创建对象结果

Example:

例子:

{"results":[{"id":145,"text":" CHECKER "},{"id":146,"text":" CHEROCKE"},
{"id":147,"text":" CHERY "},{"id":148,"text":" CHEVROLET "},
{"id":410,"text":" JINCHENG"},{"id":502,"text":" MARCHETTI "},
{"id":511,"text":" MATCHELSS "},{"id":664,"text":" SAIC CHERY"},
{"id":680,"text":" SCHEUERLE WABCO "},{"id":853,"text":" WINCHEMG"}]}