0

Im actually implementing jquery datatables plugin to sort and search in tables, in in order to sort columns with select elements (dropdownlist) I had to implement this solution given here:JQuery Datatables sorting a select inside a column, which is:

var table = $('#example').DataTable({columnDefs: [
  { 
     targets: [0,1,2,3], 
     render: function(data, type, full, meta){
        if(type === 'filter' || type === 'sort'){
           var api = new $.fn.dataTable.Api(meta.settings);
           var td = api.cell({row: meta.row, column: meta.col}).node();
           var $input = $('select, input', td);
           if($input.length && $input.is('select')){
              data = $('option:selected', $input).text();
           } else {                   
              data = $input.val();
           }
        }

        return data;
     }
  }   ],});

Actually this code is working good, however the onchange event for those select elements are never fired. So my question is how can I modify this code to make the on change event works for all select elements in the columns?

7
  • $(elem).trigger("change") is how you can programmatically trigger event handlers. The better way to do it is to create a function that you call from your change handler as well as anywhere else. Commented Feb 20, 2018 at 23:36
  • @mhodges you probably did not understand my question, I dint say I dont have a function to be called from the on change, I have the function in my jquery code but it never gets colled, if I comment this block: if($input.length && $input.is('select')){ data = $('option:selected', $input).text(); } else { data = $input.val(); }, the onchange starts working again Commented Feb 21, 2018 at 18:30
  • Can you post an mcve? There is not enough code here to diagnose the issue. If you comment out the if/else, data will return undefined each time. I want to see what your HTML and what your change handler look like Commented Feb 21, 2018 at 18:36
  • You're right, I meant when I comment the whole (render:...) all onchange events work again, let me complete the sample, you might get a better idea. Thank you Commented Feb 21, 2018 at 18:41
  • @mhodges Please take a look on this code I put here:, here it is working fine because I'm adding the class in the select elements directly on the html, but in my code which es asp.net when the page runs, in the client side the class is removed then the onchange does not work. Commented Feb 21, 2018 at 19:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.