search envelope-o feed check
Home Unanswered Active Tags New Question
user comment-o

Load Resource Tree from Database

Asked by Daniel
10 years ago.

Hi!

I want to load my Resources from my database. Is there a function or method like "loadResources" similiar to "loadEvents"??

I work with Daypilot Scheduler Javascript.

Thanks!
DAniel

Answer posted by Dan Letecky [DayPilot]
10 years ago.

You can use something like this:

An example using DayPilot.request():

function loadResources() {
  DayPilot.request("backend_resources.php", function(result) {
      var data = eval("(" + result.responseText + ")");
      dp.resources = data;
      dp.update();
  });
}

An example using jQuery:

function loadResources() {
  $.post("backend_resources.php", 
  function(data) {
      dp.resources = data;
      dp.update();
  });
}

The backend_resources.php should return an array of resources.

Flat resource structure:

[
 { name: "Room A", id: "A" },
 { name: "Room B", id: "B" },
 { name: "Room C", id: "C" }
]

Resource tree:

[
  { name: "Room A", id: "A", expanded: true, children:
    [
      { name : "Room A.1", id : "A.1" },
      { name : "Room A.2", id : "A.2" }
    ] 
  },
  { name: "Room B", id: "B" },
  { name: "Room C", id: "C" }
]
Comment posted by Dan Letecky [DayPilot]
10 years ago.

See also the HTML5 Scheduler tutorial:

http://code.daypilot.org/87166/html5-scheduler

It shows how to load the scheduler resources from the dabatase using an AJAX call. It uses the jQuery method:

function loadResources() {
  $.post("backend_resources.php", function(data) {
    dp.resources = data;
    dp.update();
  });
}

PHP backend example:

<?php
require_once '_db.php';
    
$scheduler_groups = $db->query('SELECT * FROM [groups] ORDER BY [name]');

class Group {}
class Resource {}

$groups = array();

foreach($scheduler_groups as $group) {
  $g = new Group();
  $g->id = "group_".$group['id'];
  $g->name = $group['name'];
  $g->expanded = true;
  $g->children = array();
  $groups[] = $g;
  
  $stmt = $db->prepare('SELECT * FROM [resources] WHERE [group_id] = :group ORDER BY [name]');
  $stmt->bindParam(':group', $group['id']);
  $stmt->execute();
  $scheduler_resources = $stmt->fetchAll();  
  
  foreach($scheduler_resources as $resource) {
    $r = new Resource();
    $r->id = $resource['id'];
    $r->name = $resource['name'];
    $g->children[] = $r;
  }
}

header('Content-Type: application/json');
echo json_encode($groups);

?>

This PHP example loads the resources grouped by categories (stored in [groups] table).

This question is more than 1 months old and has been closed. Please create a new question if you have anything to add.