从JSON请求中自动选择Select2:使用数据表编辑器

时间:2019-02-09 15:04:02

标签: jquery datatables jquery-select2 jquery-datatables-editor

我正在使用select2作为预先输入的东西:随着用户的输入,结果将根据更改的输入进行填充。我使用templateResult通过图像,按钮等来自定义搜索结果。templateSelection将仅将ajax JSON的某些部分返回到select2输入。

效果很好。

我现在正在使用数据表编辑器来显示表单。作为编辑器的一部分,当表单以编辑方式打开时,基于下面我的代码的相同URL ajax请求将向URL添加initialValue = true和value =“ something”参数。

在AJAX php页面上,我捕获是否initialValue = true,然后使用与发送的值相对应的JSON数据进行响应。

这是我的select2:

{
   "label": "Attach to Contact:",
   "name": "assigned_to_contact_id",
   "type": "select2",
        "opts": {
             value: "",
             initialValue: true,
             ajax: {
                    url: "ajax_get_json.php?what=company_autocomplete_contacts",
                    dataType: 'json',
                    delay: 250,
                    data: function (params) {
                        return {
                            query: params.term, // search term
                            page: params.page
                        };
                    },
                    processResults: function (data, params) {
                        // parse the results into the format expected by Select2
                        // since we are using custom formatting functions we do not need to
                        // alter the remote JSON data, except to indicate that infinite
                        // scrolling can be used                            
                        params.page = params.page || 1;

                        return {
                            results: data,
                            pagination: {
                                more: (params.page * 30) < data.total_count
                            }
                        };
                    },
                    cache: true
                },
                escapeMarkup: function (markup) {
                    return markup;
                }, // let our custom formatter work
                minimumInputLength: 1,
                templateResult: formatResults_editor,
                templateSelection: formatResultsSelection_editor,
                allowClear: true,
                placeholder: {
                    "id": "",
                    "text": "(Searching all locations)"
                }
            }
 }

以下是我的格式化功能:

function formatResults_editor(results) {

    if (results.loading) {
        return "Searching...";
    }

    //set image to contact pic
    var image = '';

    if (results.no_contact_pic_requested == 'Y') {
        image = 'company_specific_files/contacts_images/no_pic_requested.png';
    } else {
        image = 'company_specific_files/contacts_images/' + results.company_id + '/' + results.id + '/' + 'webcam.jpg';
        //check to see if the pic exists, else set default image
        $.ajax({
            type: "POST",
            async: false,
            url: 'company_specific_files/contacts_images/' + results.company_id + '/' + results.id + '/' + 'webcam.jpg',
            error: function (response, status, xhr) {
                //if no image present, use default image
                image = 'company_specific_files/contacts_images/blank_avatar.png';

            },
            success: function (response, status, xhr) {
                //this is how I see if there's an image:  check the content-type response from ajax call. Make sure ajax is not async to work
                var ct = xhr.getResponseHeader("content-type") || "";

                if (ct == "image/jpeg") {
                    image = 'company_specific_files/contacts_images/' + results.company_id + '/' + results.id + '/' + 'webcam.jpg';
                } else {
                    image = 'company_specific_files/contacts_images/blank_avatar.png';
                }

            }
        });
    }

    var markup = "<div class='clearfix'><img style='float:left; padding-right: 5px' class='img-responsive' height='50' width='50' src='" + image + "'>" + ' ' + results.label + "</div>";
    return markup;

}

function formatResultsSelection_editor(results) {


    if (results.id === '') {
        return '(Searching all locations)'; //placeholder added this way since using ajax per docs.
    }

    return results.contact_name + ' ' + results.birthdate;

}

当用户在select2中键入/搜索名称时,一切正常:填充该值,结果在下拉框中设置格式,结果在select2输入中显示为选择后。

现在,如果打开编辑器并填充select2的值,则AJAX请求如下所示:

ajax_get_json.php?what=company_autocomplete_contacts&initialValue=true&value=%224258%22  

...,该页面的响应如下:

{"id":"1","text":"sample text","location":"Bayview","contact_name":"Mark","birthdate":"2010-05-28","label":"Mark from Bayview","value":"22","location_id":"4322","company_id":"432342","no_contact_pic_requested":"N"}

那么为什么select2不能像从templateSelection中选择的那样自动填充JSON响应标签?

在具有初始值的占位符时,在表单以编辑方式打开时,它仍会显示“正在搜索...”,实际上它应该像用户搜索时一样显示“标记(2010-05-28)”并选择templateResult选项。

据我所知,似乎没有任何进展。

1 个答案:

答案 0 :(得分:0)

如果有人遇到过此事:

这与Datatables Editor有关。

我不能使用templateSelection。我用id和文本格式化结果。从那里开始,它将相应地填充。