jquery - Select2 filter values disappear when search returns no results -
i using serverside implementation of datatables yadcf , select2 added functionality. data used populate table acquired via ajax request django view, data select2 autocomplete functionality, separate django view.
my issue 1 follows. if search query, not return results, values in select2 search fields disappear , cannot reset in datatables - must instead refresh page. values in non-select2 fields remain visible , can reset.
the filter types within problematic columns multi_select
select type of select2
. filter types within non-problematic columns range_date
, range_date
using bootstrap-datetimepicker
datepicker type.
below select2 column parameters.
{ "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; } } }
below django view used provide autocomplete functionality.
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() p in products: product_q.append("p.name='{}'".format(p)) if len(product_q)>0: product_q = "({}) , ".format(" or ".join(product_q)) else: product_q = "" try: versions = request.get.getlist('v[]') except: pass else: version_q = list() v in versions: version_q.append("tp.version='{}'".format(v)) if len(version_q)>0: version_q = "({}) , ".format(" or ".join(version_q)) else: version_q = "" try: serials = request.get.getlist('s[]') except: pass else: serial_q = list() v in serials: serial_q.append("s.serial='{}'".format(v)) if len(serial_q)>0: serial_q = "({}) , ".format(" or ".join(serial_q)) else: serial_q = "" autocomplete = connection.cursor() if column == "product": query = "select distinct p.name fftestapp.fftestapp_product p inner join fftestapp.fftestapp_testprogram tp " \ "on tp.product_id=p.id inner join fftestapp.fftestapp_session s on s.test_program_id = tp.id " \ "where {1}{2}p.name regexp '{0}' order p.name asc;".format(q, version_q, serial_q) elif column == "version": query = "select distinct tp.version fftestapp.fftestapp_testprogram tp inner join fftestapp.fftestapp_session s " \ "on s.test_program_id=tp.id inner join fftestapp_product p on tp.product_id=p.id {1}{2}tp.version regexp '{0}'" \ " order tp.version;".format(q, product_q, serial_q) elif column == "serial": query = "select distinct s.serial fftestapp.fftestapp_session s inner join fftestapp_testprogram tp on s.test_program_id=tp.id" \ " inner join fftestapp_product p on tp.product_id=p.id {1}{2}" \ "s.serial regexp '{0}' order s.serial;".format(q, product_q, version_q) elif column == "status": query = "select distinct final fftestapp.fftestapp_session final regexp '{0}' order final;".format(q) autocomplete.execute(query) m = [] in autocomplete.fetchall(): value = dict() value["id"] = m.append(value) results = {"items": m} return jsonresponse(results, safe=false)
thank you.
add below code select2:
$('select').select2({ minimumresultsforsearch: -1 });
Comments
Post a Comment