Search code examples
datatablesyadcf

Trying to create a yadcf filter for a column with images


I need to create a filter on a tipical columns created with images: each field is an image with this format:

<img src='http://lab.onclud.com/psm/blackcircle.png' class='notasg'>

I've created a fiddle example here: fiddle

An explication:

  • there are only 2 diferents status: [assigned/not assigned] although there are 4 diferents images (black, red, yellow and green).
  • Only black image correspond to not assigned status. The others three ones (red, yellow and green) correspond to assigned status.
  • As you could see, I've tried to differentiate those status by class HTML tag in img elements (notasg/asgn).

Thanks in advance.

PD:

I'm getting data from a json, so I can't put:

<td data-search="notassigned">

directly on HTML code. As a solution, I've used createdCell (columnDefs option) as you could see on the next updated to create data-search attribute on td element fiddle.

In this one, as you could test, your previously created filter doesn't work. I've tried some solutions, but no one has worked.

Please help me again on this one. Thanks in advance.


Solution

  • You can make use of the datatables HTML5 data-* attributes, and then tell yadcf to rely on this dt feature with the use of html5_data

    So your td will look something like

    <td data-search="assigned"><img src='http://lab.onclud.com/psm/redcircle.png' class='asgn'></td>

    and yadcf init will look like

    var oTable = $('#example').DataTable();

        yadcf.init(oTable, [
            {
                column_number: 0,
                html5_data: 'data-search',
                filter_match_mode: 'exact',
                data: [{
                    value: 'assigned',
                    label: 'Assigned'
                }, {
                    value: 'notassigned',
                    label: 'Not assigned'
                }]
            }]);
    

    Notice that I used filter_match_mode: 'exact', because I used data-search="notassigned" and data-search="assigned", and since the assigned word included inside notassigned I had to tell yadcf to perform an exact search, this can be avoided if you will use unique search term in your data-search= attribute,

    See working jsfiddle

    Another solution as introduced by kthorngren from datatables forum is to use the following dt init code

    var oTable = $('#example').DataTable({
        columnDefs: [{
            targets: 0,
            render: function(data, type, full, meta) {
                if (type === 'filter') {
                    return full[0].search('asgn') >=1 ? "assigned" : full[0].search('notasg') >= 1 ? "notassigned" : data
                } else {
                    return data
                }
            }
        }],
    });
    

    and yadcf init (removed html5_data)

    yadcf.init(oTable, [
        {
            column_number: 0,
            filter_match_mode: 'exact',
            data: [{
                value: 'assigned',
                label: 'Assigned'
            }, {
                value: 'notassigned',
                label: 'Not assigned'
            }]
        }
    ]);
    

    third option - look here