1+ <?php
2+ /*this a addon for grocery_crud modded
3+ * just do $this->mInjectAfterPageTitle = $this->load_inject('gcplug/between2date', array('column' => 'tot', 'title' => 'Search by date', 'search_button' => 'Search', 'lang' => 'en', 'auto' => true));
4+ or $this->mInjectBeforeFooter = $this->load_inject('gcplug/between2date', array('column' => 'tot', 'title' => 'Search by date', 'search_button' => 'Search', 'lang' => 'en', 'auto' => true));
5+ you could use $colum name or number, mind display_as is a traslated element, by now...
6+ */
7+ $ column = isset ($ column ) ? $ column : 0 ; //<-- you could use $colum name or number, mind display_as is a traslated element, by now...
8+ $ title = isset ($ title ) ? $ title : 'Search by date ' ;
9+ $ search_button = isset ($ search_button ) ? $ search_button : 'Search ' ;
10+ $ lang = isset ($ lang ) ? $ lang : 'en ' ; //<-- choose lang
11+ $ auto = isset ($ auto ) ? $ auto : true ; //<-- choose if you want automatic or manual search date
12+ ?>
13+
14+ <section class="content">
15+ <div class="container-fluid">
16+ <div class="card">
17+ <div class="card-header"><?php echo $ title ; ?> </div>
18+ <div class="card-body">
19+ <div class="row">
20+ <div class="col-md-4">
21+ <div id="min"></div>
22+ </div>
23+
24+ <?php if (!$ auto ) : ?>
25+ <div class="col-md-2"></div>
26+ <div class="col-md-4">
27+ <div id="max"></div>
28+ </div>
29+ <div class="col-md-2">
30+ <button class="btn btn-default" onclick="doDateSearch()"><?php echo $ search_button ; ?> </button>
31+ </div>
32+ <?php else : ?>
33+ <div class="col-md-4"></div>
34+ <div class="col-md-4">
35+ <div id="max"></div>
36+ </div>
37+ <?php endif ; ?>
38+
39+ </div>
40+ </div>
41+ </div>
42+ </div>
43+ </section>
44+ <script>
45+ $dateSearchStart = false;
46+
47+ function indexFromName(searchindex) {
48+ let ret = 0;
49+ let val = parseInt(searchindex.charAt(0));
50+ console.log(val);
51+ if (isNaN(val)) {
52+ $("table.groceryCrudTable > thead > tr[id!=multiSearchToggle] > th").each(function(index) {
53+ if (searchindex.toLowerCase() === $(this).text().toLowerCase()) {
54+ ret = index + 1
55+ };
56+ });
57+ } else {
58+ ret = searchindex;
59+ }
60+ return parseInt(ret);
61+ }
62+
63+ function initdatetime() {
64+ $('#min').datetimepicker({
65+ format: 'L',
66+ inline: true,
67+ locale: '<?php echo $ lang ; ?> ',
68+ sideBySide: true,
69+ defaultDate: '<?php echo date ('Y ' ) ?> -01-01'
70+ });
71+
72+ $('#max').datetimepicker({
73+ format: 'L',
74+ inline: true,
75+ locale: '<?php echo $ lang ; ?> ',
76+ sideBySide: true
77+ });
78+
79+ $.fn.dataTable.ext.search.push(
80+ function(settings, data, dataIndex) {
81+ if ($dateSearchStart) {
82+ let min = $('#min').datetimepicker('date').unix();
83+ let max = $('#max').datetimepicker('date').unix();
84+ let datain = data[indexFromName('<?php echo $ column ; ?> ')];
85+ let datafix = datain.slice(6, 10) + '-' + datain.slice(3, 5) + '-' + datain.slice(0, 2);
86+ let age = Date.parse(datafix) / 1000;
87+ if ((isNaN(min) && isNaN(max)) ||
88+ (isNaN(min) && age <= max) ||
89+ (min <= age && isNaN(max)) ||
90+ (min <= age && age <= max)) {
91+ return true;
92+ }
93+ return false;
94+ } else {
95+ return true;
96+ }
97+ }
98+ );
99+
100+
101+ <?php if ($ auto ) : ?>
102+ $('#min, #max').on("change.datetimepicker", function() {
103+ doDateSearch();
104+ });
105+ <?php endif ; ?>
106+ }
107+
108+ async function doDateSearch() {
109+ $dateSearchStart = true;
110+ await oTable.fnDraw();
111+ $dateSearchStart = false;
112+ }
113+
114+ $(document).ready(function() {
115+ initdatetime();
116+ });
117+ </script>
0 commit comments