Changing the name of the columns[i] index for server side ajax search

Changing the name of the columns[i] index for server side ajax search

mmalmeidammalmeida Posts: 15Questions: 2Answers: 0

Consider the datatable with:

                "ajax": {
                    "type": "GET",
                    "url": "...",
                    "data": function(request) {
                        request.page = $("#codeListTable").DataTable().page() != -1 ? $("#codeListTable").DataTable().page() + 1 : 1;
                    }
                },

I have added a column filter with [1]:

$("#codeListTable").dataTable().columnFilter({  
            sPlaceHolder: "head:before",
            bUseColVis: true,
            sRangeFormat: "{from}{to}",
            aoColumns: columnsFilterArray
        });

Question: How can I pass the search parameters to the Ajax request?
The best solution I've found involves processing in the data: function(). E.g.:

"data": function(request) {
                        request.page = $("#codeListTable").DataTable().page() != -1 ? $("#codeListTable").DataTable().page() + 1 : 1;
**request["codesFilter.filterMap['CODE']"] = request.columns[1].search.value;**
                    }

The problem with this approach is that it's easily breakable. If for example one column is added, all indexes (in the example request.columns[1]) will be incorrect and will have to be changed.

Is there a more elegant way of doing this? For example, is it possible to configure the columns object to be in the form columns['CODE'] instead of columns[1], where CODES could be fetched based on a data-xxx attribute in the html?

Any help appreciated!

[1]columnsFilterArray is built using a function which actually might be useful for more people. We build the type of filter based on the the data-xxx type in the header of the table:

/**
 * Creates an array of filters based on the headers provided.
 * Headers must have a data-type with one of the following types:
 *  - string
 *  - data-range
 *  - number-range
 */
var createFilterArray = function(header){
    var columnsFilterArray = [];
    for (var int = 0; int < header.length; int++) {
        if( $(header[int]).data('type') === "string" || $(header[int]).hasClass("string")){
            columnsFilterArray.push({ type: "text", bRegex:true });
        }else if( $(header[int]).data('type') === "date-range" || $(header[int]).hasClass("date-range")){ 
            columnsFilterArray.push({ type: "date-range" } );
        }else if( $(header[int]).data('type') === "number-range" || $(header[int]).data('type') === "num"
            || $(header[int]).hasClass("number-range") ||  $(header[int]).hasClass("num")){ 
            columnsFilterArray.push({ type: "number-range" } );
        } else {
            columnsFilterArray.push( null );
        }
    }
    return columnsFilterArray;
}; 
This discussion has been closed.