简体   繁体   English

使用ajax select2预定义的呈现项目

[英]select2 predefined rendered item with ajax

I can easily retrieve and format AJAX data to work with Select2, like this 我可以像这样轻松地检索和格式化AJAX数据以与Select2一起使用

function formatItem (item) {
    if (!item.id) {
        return item.text;
    }
    var _item = '<span class="' + item.deleted +'"><i class="fa fa-user"></i> ' + item.doctor + ', <small>' + item.sex + ' (' + item.user + ')</small></span>';
    return $(_item);
}

var tabSelect = $('#t');
tabSelect.select2({
    ajax: {
        url: '../ajax/results.php',
        data: function (params) {
            return {
                search: params.term,
                page: params.page || 1
            };
        }
    },
    templateResult: formatItem,
    templateSelection: formatItem
});

So far so good: <select> items are rendered correctly as HTML, with the nice Font-Awesome icon and the <small> tag. 到目前为止一切顺利: <select>项目已正确显示为HTML,带有漂亮的Font-Awesome图标和<small>标签。

User now selects an item and submits the (search) form. 用户现在选择一个项目并提交(搜索)表单。 Of course I'd like to mark the current item as selected , and here comes the trouble. 当然,我想将当前项目标记为selected ,麻烦来了。

Select2 docs have a way to predefine an AJAX retrieved item , but this works for simple text and not for rendered items with all the HTML bells and whistles Select2 docs提供了一种预定义AJAX检索项目的方法 ,但这仅适用于简单文本,不适用于具有所有HTML内容的渲染项目

So, when adapting the provided example: 因此,在调整提供的示例时:

$.ajax({
    type: 'GET',
    url: '../ajax/results-selected.php', // url to retrieve a single item
    data: {
        iData: [here goes the ID for retrieving single item]
    }
}).then(function (data) {
    // create the option and append to Select2
    var item = data.results[0];
    console.log(item);
    var option = new Option(item, item.id, true, true);
    tabSelect.append(option).trigger('change');

    // manually trigger the `select2:select` event
    tabSelect.trigger({
        type: 'select2:select',
        params: {
            data: data
        }
    });
});

I get the HTML rendered item, but with a series of undefined (please notice that console.log(item) returns the expected result: in other words, I have the fields but have no idea how to render them) 我得到了HTML呈现的项目,但是有一系列undefined (请注意console.log(item)返回预期的结果:换句话说,我有字段但不知道如何呈现它们)

在此处输入图片说明

I tried lot of things, but none of them work, ie: 我尝试了很多东西,但是没有一个起作用,即:

var option = new Option(formatItem(item), item.id, true, true);

I read around but could not find a valid solution working: please, any help? 我四处阅读,但找不到有效的解决方案:请帮忙吗?

I managed it to work by returning the whole HTML snippet in the JSON response (as text item) 我设法通过在JSON响应中返回整个HTML代码段(作为text项)来使其正常工作

// the JSON response
{
    "results": [
        {
            "id": "1234",
            "text": "<span class=\"\"><i class=\"fa fa-user\"></i> Doe John, <small>Mr. (Site Owner)</small></span>"
         }
    ]
}


// the updated snippet
$.ajax({
    type: 'GET',
    url: '../ajax/results-selected.php', // url to retrieve a single item
    data: {
        iData: [here goes the ID for retrieving single item]
    }
}).then(function (data) {
    // create the option and append to Select2
    var item = data.results[0];
    console.log(item);
    var option = new Option(item.text, item.id, true, true);
    tabSelect.append(option).trigger('change');

    // manually trigger the `select2:select` event
    tabSelect.trigger({
        type: 'select2:select',
        params: {
            data: data
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM