当搜索没有结果时,Select2过滤器值消失

2021/01/16 15:11 · jquery ·  · 0评论

我正在使用带有yadcf和select2的DataTables的服务器端实现来添加功能。用于填充表格的数据是通过对Django视图的Ajax请求获取的,select2自动完成功能的数据也是如此。但从单独的Django视图中进行。

我的问题如下。如果搜索查询不返回结果,则select2搜索字段中的值将消失并且无法在DataTables中重置-您必须刷新页面。non-select2字段中的值保持可见并且可以重置。

有问题的列中的过滤器类型multi_selectselect2没有问题的列内过滤器类型range_daterange_date使用bootstrap-datetimepicker作为日期选择器类型。

以下是我的select2列参数。

{
 "column_number": 3,
 "filter_type": "multi_select",
 "select_type": "select2",
 "select_type_options": {
    dropdownCssClass : 'bigdrop',
    multiple: true,
    minimumInputLength: 1,
    ajax: {
      url: '{% url 'ffTestApp:searchData' 'product' %}',
      delay: 250,
      dataType: 'json',
      data: function (params) {
        return {
          q: params.term,
          v: yadcf.exGetColumnFilterVal(table,4),
          s: yadcf.exGetColumnFilterVal(table,5)
        };
      },
      processResults: function (data, params) {
        params.page = params.page || 1;
        return {
          results: data.items
        };
      },
    },
    escapeMarkup: function (markup) { return markup; },
    templateResult: function(data) {
      return data.id;
    },
    templateSelection: function(data) {
      return data.id;
    }
  }
}

下面是我的Django视图,用于提供自动完成功能。

def searchData(request, column):
    try:
        q = request.GET.getlist('q')[0]
    except:
        pass
    else:
        if q != "":
            try:
                products = request.GET.getlist('p[]')
            except:
                pass
            else:
                product_q = list()
                for p in products:
                    product_q.append("p.name='{}'".format(p))
                if len(product_q)>0:
                    product_q = "({}) AND ".format(" OR ".join(product_q))
                else:
                    product_q = ""

            try:
                versions = request.GET.getlist('v[]')
            except:
                pass
            else:
                version_q = list()
                for v in versions:
                    version_q.append("tp.version='{}'".format(v))
                if len(version_q)>0:
                    version_q = "({}) AND ".format(" OR ".join(version_q))
                else:
                    version_q = ""

            try:
                serials = request.GET.getlist('s[]')
            except:
                pass
            else:
                serial_q = list()
                for v in serials:
                    serial_q.append("s.serial='{}'".format(v))
                if len(serial_q)>0:
                    serial_q = "({}) AND ".format(" OR ".join(serial_q))
                else:
                    serial_q = ""

            autoComplete = connection.cursor()
            if column == "product":
                query = "SELECT DISTINCT p.name FROM ffTestApp.ffTestApp_product AS p INNER JOIN ffTestApp.ffTestApp_testprogram AS tp " \
                    "ON tp.product_id=p.id INNER JOIN ffTestApp.ffTestApp_session AS s ON s.test_program_id = tp.id " \
                    "WHERE {1}{2}p.name REGEXP '{0}' ORDER BY p.name ASC;".format(q, version_q, serial_q)
            elif column == "version":
                query = "SELECT DISTINCT tp.version FROM ffTestApp.ffTestApp_testprogram AS tp INNER JOIN ffTestApp.ffTestApp_session AS s " \
                    "ON s.test_program_id=tp.id INNER JOIN ffTestApp_product AS p ON tp.product_id=p.id WHERE {1}{2}tp.version REGEXP '{0}'" \
                    " ORDER by tp.version;".format(q, product_q, serial_q)
            elif column == "serial":
                query = "SELECT DISTINCT s.serial FROM ffTestApp.ffTestApp_session AS s INNER JOIN ffTestApp_testprogram AS tp ON s.test_program_id=tp.id" \
                    " INNER JOIN ffTestApp_product AS p ON tp.product_id=p.id WHERE {1}{2}" \
                    "s.serial REGEXP '{0}' ORDER BY s.serial;".format(q, product_q, version_q)
            elif column == "status":
                query = "SELECT DISTINCT final FROM ffTestApp.ffTestApp_session WHERE final REGEXP '{0}' ORDER BY final;".format(q)

            autoComplete.execute(query)
            m = []
            for i in autoComplete.fetchall():
                value = dict()
                value["id"] = i
                m.append(value)
            results = {"items": m}
    return JsonResponse(results, safe=False)

谢谢。

为select2添加以下代码:

$('select').select2({
    minimumResultsForSearch: -1
});
本文地址:http://jquery.askforanswer.com/dangsousuomeiyoujieguoshiselect2guolvqizhixiaoshi.html
文章标签: ,   ,   ,   ,  
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!

文件下载

老薛主机终身7折优惠码boke112

上一篇: 的DOM路径
下一篇:

评论已关闭!