26

I am using a serverside implementation of DataTables with yadcf and select2 for added functionality. The data used to populate the table is acquired via an Ajax request to a Django view, as is the data for the select2 autocomplete functionality; but from a separate Django view.

My issue is as follows. If a search query would not return results, the values in the select2 search fields will disappear and cannot be reset in DataTables - you must instead refresh the page. The values in non-select2 fields remain visible and can be reset.

The filter types within problematic columns are multi_select with a select type of select2. Filter types within non-problematic columns are range_date and range_date using bootstrap-datetimepicker as datepicker type.

Below are my 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 is my Django view used to 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()
                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)

Thank you.

6
  • Can't help without a link to a test page, you can try to replicate using ajax source (from file) and host it on any js playground website
    – Daniel
    Sep 8, 2016 at 15:11
  • Hey Daniel, it seems that this bug also exists within one of your examples, linked. Try the following: Search for 'Some Data 11' in the Some Data column. Search for 'b_value' in the Values column. You'll see that the search parameter in the Some Data column disappears. yadcf-showcase.appspot.com/cumulative_filtering.html
    – amitchone
    Sep 9, 2016 at 11:06
  • The b_value (and the others except for the a_value,e_value) should not be present because its a cumulative_filtering mode... will see why is that...
    – Daniel
    Sep 11, 2016 at 8:28
  • Hi Daniel, any updates on this?
    – amitchone
    Sep 15, 2016 at 13:13
  • Oops, nope, you better open an issue otherwise I wont remember it :|
    – Daniel
    Sep 15, 2016 at 14:00

1 Answer 1

2

add below code for select2:

$('select').select2({
    minimumResultsForSearch: -1
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.