简体   繁体   中英

YADCF custom_func not calling custom function

I have a database field that contains either integer 1 (representing the value 'document') or integer 2 (representing the value 'email') and I use datatables to format/display these integers as the words 'document' or 'email' within the table column. I would therefore like to use YADCF to be able to select 'document' or 'email' and have datatables filter the resultset accordingly. Link to screen shot of table column.

I have used the custom_func feature as follows (code stripped down for brevity), but regardless of all other aspects of the datatable looking and working correctly, with a filter selector looking as expected and holding the selected value, and no console errors, my custom function is not being called. I have tried manually calling the custom function immediately before initialising YADCF, to check the scope, and it is being called just fine. Can anyone see anything obviously wrong please?

$(document).ready(function() {
    function customFilterDocEmail(filterVal, columnVal) {
        alert('customFilterDocEmail called');
        var found;
        if (columnVal === '') {
            return true;
        }
        switch (filterVal) {
            case 'doc':
                found = columnVal === '1';
                break;
            case 'email':
                found = columnVal === '2';
                break;
            default:
                found = 1;
                break;
        }
        if (found !== -1) {
            return true;
        }
        return false;
    }
})


var table = $('#table').DataTable({
  serverSide: true,
  processing: true,
  ajax: {
    url: '/api/datatables/getJson/doctext/doctexts',
    type: 'POST', 
    data: function(d) {d.CSRFToken = '****';}
  },
  stateSave: true,
  responsive: true,
  pageLength: 25,
  order: [[0, 'asc']],
  columns: [ 
    { data: 'txt_type', width: '10%' },
    { data: 'txt_title' },
    { data: 'txt_name', width: '20%' },
    { data: 'link', orderable: false, width: '5%' },
  ],
  }
});

yadcf.init(table, [ 
  { column_number: 0, filter_type: 'custom_func', custom_func: customFilterDocEmail,
    data: [{ value: 'doc', label: 'Document' }, { value: 'email', label: 'Email' }] },
  { column_number: 1, column_data_type: 'text', filter_type: 'text', filter_delay: 500, filter_default_label: '', },
  { column_number: 2, column_data_type: 'text', filter_type: 'text', filter_delay: 500, filter_default_label: '', },
  ], 'footer');
  • YADCF Version: 0.9.3.beta.11
  • DataTables Version: 1.10.16

UPDATE: I haven't a clue what I'm doing wrong above, but I have come up with a little hack that saves having to use 'custom_func' and a custom filter. I've used the standard 'select' filter type but intercepted the filter string within the filter() method of my DatatablesSSP script thus:

$str = $requestColumn['search']['value'];    
// returned search values for doctext 'txt_type' are Document/Email, so we need to map these to 1/2.
if ($column['db'] == 'txt_type') {
    if ($str == 'Document') { $str = '1'; }
    if ($str == 'Email') { $str = '2'; }
}

This works a treat :)

您必须将customFilterDocEmail函数放置在$(document).ready块之外,这样它才是全局的, yadcf看到它

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM