Creating a protocol search ("name": "firstname", "lastname": ....)

Creating a protocol search ("name": "firstname", "lastname": ....)

meetinmeetin Posts: 4Questions: 3Answers: 0

Hi,

basically what I'm trying to do is turn protocol, which goes like this: "name": "firstname", "lastname": ....
into multicolumn search. So the keys are column names and values are values that I'm searching for. I'm trying to make an union of both so the search result is a combination of searched for values in columns.

This is what I got:

var tds = $('.dataTable').find('tr');

//here I map all the columns so I can later call them in function by the index number!

                var columnMap = [];
                $(tds[1]).find('td').each(function(i, el) {
                    var obj = {};
                    obj[$(el).data('tabletype')] = i;
                    columnMap.push(obj);
                });
                log(columnMap)

//on input keyup fire search

                $('#searchBar').on( 'keyup change', function () {

                    var val = $(this).val();
                    var valArr = val.split(',');
                    var searchableArr = [];
                    var isProtocol = false;

//default column is 0

                    var column = 0;

//some fiddling with protocol so I get an array of objects

                    valArr.forEach(function(data){
                        var key = data.split(':')[0];
                        var value = data.split(':')[1];
                        if(typeof value != "undefined"){
                            isProtocol = true;
                        }
                        searchableArr.push({
                            key: key ? key.replace(/\s+/g,"") : key,
                            value: value ? value.replace(/\s+/g,"") : value
                        })
                    });

//if it is protocol (otherwise go for the default) try to make out which column is key targeted to

                    if(isProtocol){
                        searchableArr.forEach(function(item){
                            columnMap.forEach(function(partial){
                                if(partial[item.key]){
                                    column = partial[item.key]
                                }
                            });
                            val = item.value
                        });
                    }
                    log(val, column);

//here I get all that I need: the right column index and the right search value

                    window.table
                            .column(parseInt(column, 10))
                            .search(val)
                            .draw();
                } );

So where is the problem? The problem is how do I search multicolumns as the official plugin can't really be used here(or can it be? Also I'm experiencing some problems when backspacing (deleting characters) from the input. Some results are still filtered even though the search bar is empty.

By the way: I'm using a custom search bar (I couldn't just turn off the search functionality and still use the default search for my protocol).

Has anyone else done this yet? What am I missing here and how to filter multicolumns?

Sincerely, Jan

Answers

  • allanallan Posts: 61,667Questions: 1Answers: 10,096 Site admin

    val here is an object, but search() doesn't accept an object as the first parameter - it takes a string according to the documentation.

    If you want to do column based searching you would need to use column().search() and set up the search term for each column individually.

    Allan

  • meetinmeetin Posts: 4Questions: 3Answers: 0

    @allan, thank you for your response. val is indeed a string (val = item.value).

    So if I want to do an union of all columns I need to make search for each one of them and each one of them will return some results and then I need to make an union right?

  • allanallan Posts: 61,667Questions: 1Answers: 10,096 Site admin

    So if I want to do an union of all columns I need to make search for each one of them

    Just this part. For example consider this little example: http://live.datatables.net/voyupaco/1/edit .

    On column 1 (index 0) I've got it search for all rows which have n in it. On column 2 it searches for rows which have j in that column. DataTables' built in filtering will therefore reduce the result set to rows which match both.

    Allan

This discussion has been closed.