基于服务器的DataTables + YADCF,基于AJAX的select2:选择选项清除Select2选择

时间:2015-12-25 18:58:23

标签: datatables yadcf

设置:

  1. DataTables使用带分页的远程数据(基于AJAX)
  2. YADCF正在使用Select2使用AJAX调用来抓取选项
  3. 选择Select2选项会触发网格刷新和过滤(一切正确)
  4. 问题:

    在DataTables拉出更新的行集后,YADCF重新运行其初始化例程,Select2失去其状态(即不再选择所选的选项,不再在DOM中)。

    此:

    GOOD STATE

    网格重新加载后,

    变为此状态(select2控件重新初始化并丢失通过AJAX提取的所有选项,包括已选择的选项): BAD STATE

    在这种情况下如何避免YADCF重新初始化?

    调试问题一段时间后,我发现在从YADCF线路每次网格刷新后调用function appendFilters(...)

    https://github.com/vedmack/yadcf/blob/master/jquery.dataTables.yadcf.js#L3332

    反过来由DataTables' draw事件。

    谢谢!

    修改

    DataTables配置数组:

    var dataTableConfig = {
        "autoWidth": false,
        "deferLoading": 220,
        "pageLength": 5,
        "searchDelay": 0,
        "searching": true,
        "placeholder": null,
        "ordering": true,
        "paging": true,
        "info": true,
        "columns": [
            {
                "name": "company",
                "data": {
                    "_": "company",
                    "filter": "company",
                    "display": "company"
                },
                "visible": true,
                "searchable": true,
                "orderable": true,
                "className": "column_Company"
            }
        ],
        "showGlobalSearch": true,
        "enableColumnFilter": true,
        "columnFilterPosition": "table",
        "resetPaging": true,
        "select": {
            "style": "single"
        },
        "serverSide": true,
        "ajax": {
            "url": "/datasource/",
            "type": "post"
        }
    };
    

    YADCF INIT:

    colCfg = [
        {
            "column_number": 2,
            "filter_type": "select",
            "data": [],
            "filter_default_label": "(select..)",
            "enable_auto_complete": false,
            "sort_as": "alpha",
            "sort_order": "asc",
            "filter_match_mode": "contains",
            "exclude_label": "exclude",
            "select_type": "select2",
            "select_type_options": {
                "width": "300",
                ajax: {
                    url: '/datasource/',
                    dataType: 'json',
                    method: 'post',
                    delay: 750,
                    data: function (params) {
                        return {
                            q: params.term,
                            page: params.page
                        };
                    },
                    processResults: function (data, params) {
                        params.page = params.page || 1;
                        return {
                            results: data.results,
                            pagination: {
                                more: (params.page * 20) < data.total_count
                            }
                        };
                    },
                    cache: true
                },
                minimumInputLength: 1,
                templateResult: formatItem,
                templateSelection: formatItemSelection,
                escapeMarkup: function(v) {return v;}
            },
            "case_insensitive": true,
            "filter_delay": 500
        }
    ];
    yadcf.init(dataTable, colCfg);
    

0 个答案:

没有答案