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

Use Custom JSON Object in resource

Asked by zura
9 years ago.

Hello,

May i use custom JSON object created for me in resource parameter ?

I don't want to json same this

[
{
"id":"group_1",
"name":"People",
"expanded":true,
"children":[
{"id":"1","name":"Person 1"},
{"id":"2","name":"Person 2"},
{"id":"3","name":"Person 3"},
{"id":"4","name":"Person 4"},
{"id":"5","name":"Person 5"},
{"id":"6","name":"Person 6"},
{"id":"7","name":"Person 7"},
{"id":"8","name":"Person 8"},
{"id":"9","name":"Person 9"}
]
},
{
"id":"group_2",
"name":"Tools",
"expanded":true,
"children":[
{"id":"11","name":"Tool 1"},
{"id":"12","name":"Tool 2"},
{"id":"13","name":"Tool 3"},
{"id":"14","name":"Tool 4"},
{"id":"15","name":"Tool 5"},
{"id":"16","name":"Tool 6"},
{"id":"17","name":"Tool 7"},
{"id":"18","name":"Tool 8"},
{"id":"19","name":"Tool 9"}
]
}
]
if yes please tell me how to do this

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

The scheduler .resources property requires this exact structure. See also:

http://api.daypilot.org/daypilot-scheduler-resources/

If your server returns JSON array that has items with a different structure you will need to transform the items before assigning it to the .resources property.

Example:

1. Your server returns

[
 { "personName": "John", personId: "2"}
]

2. The jQuery POST request could look like this:

$.post("get_resources.php", function(data) {
  var resources = [];
  for (var i = 0; i < data.length; i++) {
    var source = data[i];
    var item = {
      name: source.personName,
      id: source.personId
    };
    resources.push(item);
  }
  dp.resources = resources;
  dp.update();
});

3. In modern browsers (IE9+) you can use .map() instead:

$.post("get_resources.php", function(data) {
  dp.resources = data.map(function(source) { 
    return {
      name: source.personName,
      id: source.personId
    };
  });
  dp.update();
});
This question is more than 1 months old and has been closed. Please create a new question if you have anything to add.