I have the following date pickers:
<div style="margin-bottom: 20px;">
<label for="startDate">Start Date: </label>
<input type="date" id="startDate" class="date-picker" />
<label for="endDate">End Date: </label>
<input type="date" id="endDate" class="date-picker" />
<button onclick="filterByDate()">Filter</button>
</div>
I have a table like this:
<thead class="thead-dark sticky-top" style="top: 50px;">
<tr>
<th>Job No</th>
<th>Company</th>
<th>Permit No</th>
<th>R No</th>
<th>TM Company</th>
<th>TM Type</th>
<th>Address</th>
<th>Checked By</th>
<th>Checked Date</th>
<th style="width:250px;">Comments</th>
</tr>
</thead>
Where I want to filter through the ‘Checked Date’ column in my table, which is assigned a ‘TbDate’ value and displayed in the DD/MM/YYYY HH:mm format:
<td>@firstJob.TbDate?.ToString("dd/MM/yyyy HH:mm")</td>
The rendered HTML of the date pickers and the table rows appear like this:
<div style="margin-bottom: 20px;">
<label for="startDate">Start Date: </label>
<input type="date" id="startDate" class="date-picker" pattern="d{2}/d{2}/d{4}">
<label for="endDate">End Date: </label>
<input type="date" id="endDate" class="date-picker" pattern="d{2}/d{2}/d{4}">
<button onclick="filterByDate()">Filter</button>
</div>
<tr class="data-row">
<td><a href="/Job?JobNo=505248" target="_blank">505248</a></td>
<td>SCS</td>
<td>SMA TEST</td>
<td>SMATEST1</td>
<td>></td>
<td></td>
<td>DO NOT PLAN, THIS IS A TEST, TEST TOWN</td>
<td>Test Name</td>
<td>27/10/2023 12:17</td>
<td>
<form id="CommentsBox_505248" style="display: flex; align-items: center; justify-content: flex-end; width: 100%;">
<input type="hidden" name="JobNo" value="505248">
<textarea name="Comments" placeholder="Add comments" style="flex: 1 1 auto; width: 100%; height: 80px; box-sizing: border-box; margin-right: 10px;"></textarea>
<button type="button" onclick="submitComment('505248')">Submit</button>
</form>
</td>
</tr>
How can I implement JavaScript to filter through the rows in my table between the range set in the date pickers?
I only want to filter through using the DD/MM/YYYY value and ignore the HH:mm values.
I am using Bootstrap, razor page, JQuery and Vanilla JS.
2
Answers
I think your date format is wrong.
Your ‘checkedAt’ date is formatted like dd/MM/yyyy HH:mm, so you can’t create new date using your ‘checkedAt’ date.
Usually, the date string is formatted like this : MM/dd/yyyy HH:mm.
If you change format of ‘checkedAt’ like this, you can solve your problem more easily.
I hope this answer will help you.
I don’t use Bootstrap or jQuery so can offer no guidance with whatever native methods they might have but with vanilla JS you could perhaps accomplish your goal like this.
The snippet below shows 3 possible table rows of dummy data based upon your edited HTML. I hope this is a reasonable facsimile of what you might see.
All inline event handlers have been moved out to a delegated listener which inspects the
event.target
to determine which function to run. The forms within the table probably don’t need IDs and the inline styles were also moved to external css.The
filterByDate
function initially checks that both start and end dates have been given values. With that filtered array of input element references we build two new Date objects and get the times for each. These times are used to compare with rows in the HTML table.Query the DOM to find all table rows of class
.row-data
and find the table-cell that contains the date string value. As per comment above, this would be simpler if the table-row itself had, for example, a dataset attribute that contained this date string.The datestring is modified to create a valid string format and a new Date object is created and used for the comparison. If the table row has a date within the range given by the input elements the row will be shown, otherwise it will be hidden.