如何遍历json数据并在flatpickr上实现(How to loop through json data and implement on flatpickr)

编程入门 行业动态 更新时间:2024-10-27 12:24:46
如何遍历json数据并在flatpickr上实现(How to loop through json data and implement on flatpickr)

首先,我知道这个问题已被提出,但我已尝试过大多数(如果不是所有)提供的解决方案并且无法使其正常工作。

我在我的Symfony 3项目中使用flatpickr,我需要能够根据预订的假期禁用某些日期。 当从下拉列表中选择某个人时,系统会查询数据库以查明他们是否在任何时候都在度假,如果是,则将这些日期作为“从”和“到”返回。 然后结果是JSON编码并通过Ajax响应返回。

这是来自Ajax调用的示例响应:

[{"from":"2017-06-05","to":"2017-06-09"},{"from":"2017-06-27","to":"2017-06-28"}]

而flatpickr需要一组日期才能禁用,如下所示:

{ disable: [ { from: "2017-04-01", to: "2017-06-01" }, { from: "2017-09-01", to: "2017-12-01" } ] }

所以我使用以下jQuery来尝试获得所需的结果:

$.each(response, function(i, dateitem){ console.log(dateitem.from + " - " + dateitem.to); });

出于测试目的,但没有任何显示。 response是Controller中数据库的返回结果。

任何人都可以帮助我将这些数据转换为与Flatpickr一起使用的正确格式,因为我正在努力奋斗。

Firstly, I know that this question has been asked but I've tried most if not all of the solutions that were offered and cannot get it to work correctly.

I'm using flatpickr with my Symfony 3 project, and I need to be able to disable certain dates, based on holidays booked. When a certain person is selected from a drop down, the system queries the database to find out if they are on holiday at any point and, if so, returns those dates as a 'from' and 'to'. The result is then JSON encoded and returned via the Ajax response.

This is an example response from the Ajax call:

[{"from":"2017-06-05","to":"2017-06-09"},{"from":"2017-06-27","to":"2017-06-28"}]

And flatpickr requires a set of dates to disable, like so:

{ disable: [ { from: "2017-04-01", to: "2017-06-01" }, { from: "2017-09-01", to: "2017-12-01" } ] }

So I used the following jQuery to try and get the desired result:

$.each(response, function(i, dateitem){ console.log(dateitem.from + " - " + dateitem.to); });

For testing purposes, but nothing shows. response being the return result from the database in the Controller.

Can anyone help me to convert this data into the correct format for use with Flatpickr as I'm struggling quite a lot.

最满意答案

最后,我做了很长一段路,在那里你按顺序说明每个日期都被禁用。

我使用DatePeriod()获取from和to之间的所有日期,然后再次在结束日期添加(因为DatePeriod()不执行此操作),如下所示:

foreach ($holidays as $row) { $start_date = new \DateTime($row->getStartDate()->format('Y-m-d')); $end_date = new \DateTime($row->getEndDate()->format('Y-m-d')); $date_range = new \DatePeriod($start_date, new \DateInterval('P1D'), $end_date); foreach($date_range as $date){ $date_array[] = $date->format('Y-m-d'); } // Add the end date as this is not included in the date period. $date_array[] = $row->getEndDate()->format('Y-m-d'); }

我给ajax一个json编码结果,然后ajax成功:

var parsed = JSON.parse(response); var optional_config = { enableTime : true, disable: parsed };

这就是诀窍:)

In the end I did it the long way round, where you state each date in sequence to be disabled.

I used DatePeriod() to get all the dates between the from and to, and then tacked on the end date again (as DatePeriod() doesn't do this) as follows:

foreach ($holidays as $row) { $start_date = new \DateTime($row->getStartDate()->format('Y-m-d')); $end_date = new \DateTime($row->getEndDate()->format('Y-m-d')); $date_range = new \DatePeriod($start_date, new \DateInterval('P1D'), $end_date); foreach($date_range as $date){ $date_array[] = $date->format('Y-m-d'); } // Add the end date as this is not included in the date period. $date_array[] = $row->getEndDate()->format('Y-m-d'); }

I fed the ajax a json encoded result, and then on the ajax success:

var parsed = JSON.parse(response); var optional_config = { enableTime : true, disable: parsed };

and that did the trick :)

更多推荐

response,flatpickr,电脑培训,计算机培训,IT培训"/> <meta name="descriptio

本文发布于:2023-07-20 11:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1198687.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:遍历   并在   数据   json   implement

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!