2
Answers

Custom Button filter Using Jquery DataTable problem

Hi everyone! 
 
I have a problem with some filter. I want to make a button, with allowing me filtering a DataTable. For example: 
 Bialy = White 
I click the button 'Bialy' and DataTables showing me rows contains color 'Bialy'.
 
 JQuery DataTables code
  1. $(document).ready(function () {  
  2.             var dataTableUrl = '@Url.Action("GetToDatatable", "KoloryTowarow")';  
  3.             var removeUrl = '@Url.Action("Usun", "KoloryTowarow")';  
  4.             var table = $("#towarKolorDataTable").dataTable({  
  5.                 "sAjaxSource": dataTableUrl,  
  6.                 "order": [[1, "desc"]],  
  7.                 "columnDefs": [  
  8.                     {  
  9.                         'data': -1,  
  10.                         'orderData': -1,  
  11.                         'defaultContent''',  
  12.                         'className''control',  
  13.                         'orderable'false,  
  14.                         'targets': 0  
  15.                     },  
  16.                     {  
  17.                         "targets": 1,  
  18.                         "data": 1,  
  19.                         'orderData': 1,  
  20.                         "render"function (data, type, rowData) {  
  21.                             return data;  
  22.                         }  
  23.                     },  
  24.                     {  
  25.                         "targets": 2,  
  26.                         "data": 0,  
  27.                         'orderData': 0,  
  28.                         "sortable"false,  
  29.                         "width""16%",  
  30.                         "render"function (data, type, rowData) {  
  31.                             var result = "";  
  32.                             var editLink = '<a href="@Url.Action("Popraw", "KoloryTowarow")/' + rowData[0] + '" class="btn btn-primary btn-sm "><i class="fa fa-pencil" aria-hidden="true"></i> Edytuj</a>';  
  33.                             var removeLink = '<a href="#" class="btn btn-danger btn-sm removeRow margin-top-right-5"><i class="fa fa-trash" aria-hidden="true"></i>  Usun</a>';  
  34.                             var infoLink = '<span clas="data-toggle="popover" title="Utworzyl: ' + rowData[3] + ' - ' + rowData[2] + '  
  35. Zmodyfikowal: ' + rowData[4] + ' - ' + rowData[5] + '  
  36. Usunal: ' + rowData[6] + ' - ' + rowData[7] + '"><a href="#" class="fa fa-info-circle fa-fw" style="font-size:24px; vertical-align: -5px"></a></span>';  
  37.                             result += infoLink + editLink + removeLink;  
  38.                             return result;  
  39.                         }  
  40.                     }  
  41.                 ],  
  42.                 "fnCreatedRow"function (row, data, dataIndex) {  
  43.                     bindActionsDataTable(row, data);  
  44.                 },  
  45.                 "pageLength": 50,  
  46.                 "lengthMenu": [[50, 100, -1], [50, 100, "Wszystkie"]],  
  47.                 "paginate"true,  
  48.                 "lengthChange"true,  
  49.                 "sortClasses"false,  
  50.                 "dom""Bfrtip"//"lftipr",  
  51.                 "processing"true,  
  52.                 "responsive"true,  
  53.                 "serverSide"true,  
  54.                 "searching"true,  
  55.                 "serverData"function (url, data, callback) {  
  56.                     $.ajax({  
  57.                         "url": url,  
  58.                         "data": data,  
  59.                         "success": callback,  
  60.                         "contentType""application/x-www-form-urlencoded; charset=utf-8",  
  61.                         "dataType""json",  
  62.                         "type""POST",  
  63.                         "cache"false,  
  64.                         "error"function () {  
  65.                             console.log("DataTables warning: JSON data from server failed to load or be parsed. " +  
  66.                             "This is most likely to be caused by a JSON formatting error.");  
  67.                         }  
  68.                     });  
  69.                 },  
  70.                 buttons: [  
  71.                     {  
  72.                         text: 'Bialy',  
  73.                         action: function (e, dt, node, config) {  
  74.                             $.fn.dataTable.ext.search.push(  
  75.                                 function (settings, searchData, index, rowData, counter) {  
  76.                                     return searchData[1] == "Bialy";  
  77.                                           
  78.                                 }  
  79.                             )  
  80.                             table.draw();  
  81.                             $.fn.dataTable.ext.search.pop();  
  82.                         }  
  83.                     }  
  84.                 ],  
  85.                 "language": {  
  86.                     "lengthMenu""Wyswietl _MENU_ wyników na stronie",  
  87.                     "zeroRecords""Niestety, nic nie znaleziono",  
  88.                     "info""Strona _PAGE_ z _PAGES_",  
  89.                     "loadingRecords""Ladowanie ...",  
  90.                     "processing""Ladowanie ...",  
  91.                     "infoEmpty""Brak rekordów",  
  92.                     "infoFiltered""(przefiltrowano _MAX_ rekordów)",  
  93.                     "search""Szukaj:",  
  94.                     "paginate": {  
  95.                         "first""Pierwsza",  
  96.                         "last""Ostatnia",  
  97.                         "next""Nastepna",  
  98.                         "previous""Poprzednia"  
  99.                     },  
  100.                 }  
  101.             });  

 I'm confused, why it's not working. Probably problem is with 
  1. buttons: [  
  2.     {  
  3.         text: 'Bialy',  
  4.         action: function (e, dt, node, config) {  
  5.             $.fn.dataTable.ext.search.push(  
  6.                 function (settings, searchData, index, rowData, counter) {  
  7.                     return searchData[1] == "Bialy";  
  8.                           
  9.                 }  
  10.             )  
  11.             table.draw();  
  12.             $.fn.dataTable.ext.search.pop();  
  13.         }  
  14.     }  
  15. ],  
Someone can help me ? 

Answers (2)