Fullcalendar调度程序使用JSON提要检索资源

时间:2017-06-12 11:21:22

标签: json fullcalendar

我刚刚开始使用Jquery完整日历的Scheduler插件,但是在将资源作为JSON提要检索时遇到了问题,这在从数据库中获取时是必需的。这是我想要实现的一个例子:

https://fullcalendar.io/js/fullcalendar-scheduler-1.6.2/demos/scale.html

基本上,资源是左侧的名称,但对于我的日历,我将始终需要父级 - >孩子们查看,因为我有属性,其中包含单位。

目前,我正在根据需要从数据库中获取数据,这是我得到的数组(请记住,这些只是我测试的完全随机的示例):

Array
(
[0] => Array
    (
        [id] => 3
        [title] => Emerson Buildings
        [children] => Array
            (
                [0] => Array
                    (
                        [id] => 7
                        [title] => Michael
                    )

                [1] => Array
                    (
                        [id] => 10
                        [title] => Unit 2
                    )

            )

    )

[1] => Array
    (
        [id] => 2
        [title] => Morb House
        [children] => Array
            (
                [2] => Array
                    (
                        [id] => 5
                        [title] => Apartment Number 1
                    )

                [3] => Array
                    (
                        [id] => 6
                        [title] => 2 Bed Apartment
                    )

                [4] => Array
                    (
                        [id] => 11
                        [title] => Jamie
                    )

            )

    )

)

当我在检索这些资源后转换为JSON时,我将其保存到我使用get-resources.php获取的文件中:

<?php
// Require our Event class and datetime utilities
require dirname(__FILE__) . '/utils.php';

// Read and parse our events JSON file into an array of event data arrays.
$json = file_get_contents(dirname(__FILE__) . '/booking_json/resources.json');

// Send JSON to the client.
echo $json;

resources.json文件中的JSON:

[{"id":3,"title":"Emerson Buildings","children":[{"id":7,"title":"Michael"},{"id":10,"title":"Unit 2"}]},{"id":2,"title":"Morb House","children":{"2":{"id":5,"title":"Apartment Number 1"},"3":{"id":6,"title":"2 Bed Apartment"},"4":{"id":11,"title":"Jamie"}}}]

但是,在尝试在javascript初始化中检索它时,这总是会引发异常错误:

$('#availability-calendar').fullCalendar( {
    schedulerLicenseKey: '0409344942-fcs-1497004132',
    header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,agendaDay,agendaWeek'
    },
    defaultDate: TODAY,
    defaultView: 'timelineMonth',
    editable: true,
    navLinks: true,
    eventLimit: true,
    timeFormat: 'hh:mma',
    resourceAreaWidth: '25%',
    resourceLabelText: 'Property / Units',
    resources: {
        url: '/get-resources.php',
        error: function() {
            $('#resource-warning').show();
        }
    },
    events: {
        url: '/get-bookings.php',
        error: function() {
            $('#script-warning').show();
        }
    },
    loading: function(bool) {
        $('#loading').toggle(bool);
    },
    eventRender: function(event, element) {
    }
});

(这里特别指资源部分)。只是为了澄清,我得到的预订方式几乎相同,但由于没有像资源这样的嵌套值,所以它们都有效。

对此的任何帮助都表示赞赏,因为完整日历网站上的文档并未真正详细介绍此方法。

编辑:我已设法修复它。基本上这是孩子们在字符串中格式化的方式,所以我现在这样做:

foreach ($prop_units as $v) {
    $id = $v['prop_id'];
    $record = array_diff_key($v, $exclude);

    if (!isset($result[$id])) {
        $result[$id] = $record;
        $result[$id]['units'] = [];
    }

    $unit = array_diff_key($v, $record);
    $result[$id]['units'][$x]['id'] = $unit['unit_id'];
    $result[$id]['units'][$x]['title'] = $unit['unitName'];
    $x ++;
}


if (!empty($result)) {

    $resources = array();
    foreach ($result as $propId => $row) {
        $resource = array();
        $resource['id'] = $propId;
        $resource['title'] = $row['propertyName'];
        $resource['children'] = [ ];

        foreach($row['units'] as $unit_row) {
            array_push($resource['children'],(object) $unit_row);
        }

        $obj = (object) $resource;

        array_push($resources, $obj);

        $obj = false;
    }
}

主要的变化是,不是将单元数组分配给$resource对象的子部分,而是现在遍历每个部分并array_push()进入。然后返回正确的JSON格式允许它正确呈现。感谢@ADyson帮助解决这个问题。

0 个答案:

没有答案