【问题标题】:Loop dates and add object if not excist in JSON data如果 JSON 数据中不存在,则循环日期并添加对象
【发布时间】:2016-02-06 20:00:27
【问题描述】:

我需要使用 fullcalendar.io 创建日历视图。要使用日历,我需要像这样创建一个 JSON:

var db_data = [
  {
    "id": 5,
    "user_id": 1,
    "article_id": 5,
    "title": "",
    "start": "2016-03-25 15:18:46"
  },
  {
    "id": 4,
    "user_id": 1,
    "article_id": 5,
    "price": 55,
    "title": "",
    "start": "2016-03-15 15:18:46"
  } etc.

我需要从 period_start 到 period_end 的每个日期在日历上输入价格,但对于某些价格我在数据库中有值,而对于其他我没有,所以我需要使用标准价格(等 50 美元)...

所以我有 period_start 和 period_end,并且我从数据库中获取了一些数据,但现在我需要创建一个 JSON,其中包含不在数据库中的日期对象,因此我创建了代码:

function fulljson (){
    var db_data;
    $.ajax({
                url: "http://localhost:8888/diving/{{$article->id}}", 
                type: "GET",
                async: true, 
                dataType: "json",
                success: function(data) {
                db_data = data;
                console.log(db_data);

    // declare variables
    var period_start = new Date('{{ date('Y-m-d', strtotime($article->from)) }}'),
        period_end = new Date('{{ date('Y-m-d', strtotime($article->to)) }}'),
        current_date = period_start,
        array_of_all_dates = [];

    // Create a populated array of dates
    while (current_date.getTime() <= period_end.getTime()) {
      array_of_all_dates.push(current_date);
      current_date = new Date(+current_date);
      current_date.setDate(current_date.getDate() + 1);
    }

    // Now loop over the array of populated dates and mutate, so something like
    array_of_all_dates = array_of_all_dates.map(function (date) {
      var found_in_db = db_data.filter(function (db_data) {
        return new Date(db_data.start.replace(" ", "T")).getTime() === date.getTime(); // I need to do this comparison better!
      });
      if (found_in_db.length > 0) {
        return found_in_db[0];
      }
      var new_object = {
        title: '',
        start: date,
        price: '{{$article->price}}'
      };
      console.log(new_object);
      return new_object;

    });
    console.log('result'+array_of_all_dates);
    drawCalendar(array_of_all_dates);
                }, 
                error: function (data) {
                console.log(data);
                console.log('ERROR');
                }      
    });

};

    $(document).ready(function() {
    fulljson();

    });

    function drawCalendar(data) {
             $('#calendar').fullCalendar({
            header: {
                left: 'prev today',
                center: 'title',
                right: 'next'
            },
            defaultDate: Date.now(),

            eventRender: function(event, element) {
            element.find("fc-event-container").remove();    
    element.find(".fc-content").remove();
    element.find(".fc-event").remove();
    var new_description =   
         '<div style="display:inline-flex; float:right;"><div class="col-md-6"style="margin-top:5px";><p class="price">' + event.price + 'e</p></div>';

    element.append(new_description);
} ,// allow "more" link when too many events
            events: data,
            loading: function(bool) {
                $('#loading').toggle(bool);
            }
    }

        });
};

现在这段代码可以在 Chrome 中运行:

但在 FireFox 中我得到了这个:

请看 1.2.3。二月,我有来自数据库的数据。您将看到更改。

那么为什么这段代码在 Chrome 中有效,而在 Firefox 中无效?什么是问题?有没有更好的办法来解决这个问题?

【问题讨论】:

  • 我怎样才能更好地解决这个比较:while (current_date.getTime() 0) { return found_in_db[0]; } ...
  • 我很难看出这两个屏幕截图之间的区别。什么是 1.2.3?
  • 请查看日期 1. 二月, 2. 2 月 3 日 ... 价格 ...

标签: javascript jquery arrays json object


【解决方案1】:

那么为什么这段代码在 Chrome 中有效,而在 Firefox 中无效?

因为提供的日期字符串无效,当传递给new Date() 时,Firefox 会将它们转换为invalid date。然而,Chrome 会解析它们,但这不是跨浏览器的预期行为

从服务器发送有效的 ISO 字符串或插件文档中推荐的任何格式

来自 fullcalender 文档:

当为事件或事件源指定事件对象时,您可以 指定 IETF 格式的字符串(例如:“Wed, 18 Oct 2009 13:00:00 EST”), ISO8601 格式的字符串(例如:“2009-11-05T13:15:30Z”)或 UNIX 时间戳。

【讨论】:

  • 需要在服务器上修复,我不知道你使用的是什么服务器语言。您是否按照建议查看了文档?
  • 这是 mysql 数据库的屏幕截图:i.imgur.com/OPyc8Rp.png 并且列开始是 TIMESTAMP ...
  • 因此,当从数据库中提取日期时,您需要正确格式化日期以供 javascript 读取。数据库服务器知道它所在的时区并可以使用该格式,但浏览器不知道该时区或您发送的日期参考,而不对其进行格式化
  • 但我在这里用 Laravel 刀片格式化:var period_start = new Date('{{ date('Ym-d', strtotime($article->from)) }}'),所以格式是ymd
  • 您是否阅读了完整的日历参考文档?你需要的一切都在那里解释。
猜你喜欢
  • 1970-01-01
  • 2012-04-24
  • 1970-01-01
  • 1970-01-01
  • 2011-10-30
  • 2016-05-14
  • 1970-01-01
  • 2021-02-07
  • 2016-03-17
相关资源
最近更新 更多