Date Search

Date Search

SalmanSSalmanS Posts: 102Questions: 12Answers: 0

Hi Everyone,

I would like to know if there is a way we can search rendered dates, here is my code with Date column Hire Date.

'columns': [ { data: 'emp_no' }, { data: 'first_name' }, { data: 'last_name' }, { data: 'gender' }, { data: 'hire_date', "render": function (data) { var dateObj = new Date(data); var month = ('0' + (dateObj.getMonth() + 1)).slice(-2); var date = ('0' + dateObj.getDate()).slice(-2); var year = dateObj.getFullYear(); //var shortDate = year + '/' + month + '/' + date; return (date + '/' + month + '/' + year); }

Any suggestion is highly appreciated.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 4,305Questions: 0Answers: 766

    Hi @SalmanS ,

    It should search those, since it's being returned for all rendering types (display, filter, etc). We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

  • SalmanSSalmanS Posts: 102Questions: 12Answers: 0

    Apologies, here is the fiddle please.

    http://live.datatables.net/gocuzusa/7/edit

  • kthorngrenkthorngren Posts: 5,314Questions: 19Answers: 1,188

    Your example was generating this error:
    VM46 jquery.dataTables.js:3104 Uncaught TypeError: Cannot set property '_DT_CellIndex' of undefined

    You added an Action column but only populated two rows with the column. The error was generated because the column was missing in the other rows. I removed those rows. Searching for the date that you render seems to work:
    http://live.datatables.net/zamiyona/1/edit

    Kevin

  • SalmanSSalmanS Posts: 102Questions: 12Answers: 0

    Hi Kevin,
    Many thanks for the update, the on.click is not alerting the name from first column?
    If you have a moment, please see the same fiddle, do you see any errors?
    or am I missing something?

  • kthorngrenkthorngren Posts: 5,314Questions: 19Answers: 1,188
    edited January 12

    You are getting this error:
    Uncaught ReferenceError: e is not defined

    You can look at the browser's console for these errors.

    You need to pass the e parameter in your event handler functions:
    http://live.datatables.net/zamiyona/2/edit

    Kevin

  • SalmanSSalmanS Posts: 102Questions: 12Answers: 0

    Thanks kevin,
    I tried the following and it doesnt work

    http://live.datatables.net/zamiyona/6/edit

       $('.view_btn').click(function(e){
          e.preventDefault();
          var user_id = $(this).val();
          var name = $(this).closest("tr").find('td:eq(0)').text();
    
    
         var username = 'xyz';
         alert(name);
       });
    
  • kthorngrenkthorngren Posts: 5,314Questions: 19Answers: 1,188
    Answer ✓

    You code is working. If you look at the console you will see this message:
    Ignored call to 'alert()'. The document is sandboxed, and the 'allow-modals' keyword is not set.

    Alerts don't work when in edit mode in JS BIN. You have two choices:

    1. Use console.log to output in the console
    2. In the upper right corner of the output tab click the arrow that is pointing up and to the right. This will open the page in a new tab allowing the alerts to work.

    Kevin

Sign In or Register to comment.