Date Range Selection with Materialize CSS
Date Range Selection with Materialize CSS
C_Lemon
Posts: 5Questions: 0Answers: 0
**https://datatables.net/forums/discussion/71974/how-to-implement-datatable-with-materializecss?**:
In continuation to the above solution posted by Willaim, can we add date range selection as well? I am trying to add the below code, but not getting any output. (I dont know any javascript)
var minDate, maxDate;
// Custom filtering function which will search data in column four between two values
$.fn.dataTable.ext.search.push(function (settings, data, dataIndex) {
var min = minDate.val();
var max = maxDate.val();
var date = new Date(data[4]);
if (
(min === null && max === null) ||
(min === null && date <= max) ||
(min <= date && max === null) ||
(min <= date && date <= max)
) {
return true;
}
return false;
});
$(document).ready(function () {
// Create date inputs
minDate = new DateTime($('#min'), {
//format: 'MMMM Do YYYY'
format: 'YYYY-MM-DD',
});
maxDate = new DateTime($('#max'), {
//format: 'MMMM Do YYYY'
format: 'YYYY-MM-DD',
});
// DataTables initialisation
var table = $('#mainTable').DataTable();
// Refilter the table
$('#min, #max').on('change', function () {
table.draw();
});
});
Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
Replies
I'm happy to look at a test page showing the issue. Please see here if you can't provide a link to your current page.
Allan
Below is the code for the Django application.Date selection is not rendered with the below code. Is it also possible to have the search box and the date range inputs all in one line with a reset button to clear the filtered result?
Attaching a screenshot of the rendered output.
base.html
Test.html
This example shows how to add custom elements in with the Datatables elements.
Kevin
I'd really need a link to a running test case showing the issue so I can debug it please. I'm not at all well versed in Django - not enough to make a running test case from the above anyway.
Also it looks like some styling is missing - I wonder if just that is enough to make the date box not appear (it actually probably does somewhere on the page if that is indeed the issue). Try adding the DataTables and DateTime styling to your page (I see you do have the two stylesheets in the code - but the DataTables one certainly isn't being applied). A running test case would let me test and debug these things.
Allan
Hi Allan,
Since my requirement involves multiple files, can i upload a running project on surge.sh?
Sure - that should allow be to debug the front end.
Allan
I have managed to create a CodePen with the incorrect output. Output desired is to have the number of entries selection appear together with the date range to filter the table records accordingly in addition to the Sidebar navigation menu and searching the table records. Link to CodePen is as below:
https://codepen.io/Chlorine-Lemon/pen/abKegor
Thanks for the link!
Reading their documentation for Select it says:
Which would explain why the
select
for the "Show entries" isdisplay: none
. Likewise the hidden "Search"input
element.So... I tried to follow the Materialize documentation:
Neither work. I saw you were including 0.98.2, so I tried 1.0.0 - same issue. The first reports that
M
is undefined, the second thatformSelect
is undefined.There doesn't appear to be a general initialisation for Materialize, so I'm at a bit of a loss. I'm afraid that this would be one for the Materialize folk. Although the project looks like it hasn't been updated in 5 years, so I'm not sure how much support you'll be able to get from them?
Can you see something that I've missed?
Allan
Thanks Allan. I tried different ways too. Once materialize.css is included, the layout and display gets affected. Guess I will have to change the CSS framework for the entire project altogether. Trying with UIKit now. Hope it works. Thanks anyway.
The Materialize core CSS has
select { display: none }
which is why the select input for the page length disappears. Theinput
for the filter is the same. Imho that's not the best for accessibility and progressive enhancement would be better, but what really has me confused is why we can't get the Materialize JS to then operate like it says in their docs. There isn't even anM
object on the global space. So I'm clearly missing something, I'm just not sure what!I've just found that there is an active fork on Github that you might want to give a go before throwing in the towel?
Allan