0

I have Following working code:

var transforms = {
   'renderTimeline':[  {
       tag: "a",  
       class: "btn btn-warning btn-circle", 
       style: "float: right;", 
       html: "<i class=\"icon-remove\"></i>",
       "onclick": function(e) {
       delSchedule(e);
     }}
  }]
}

If I am passing the following json :

{ monday:[ { startTime:10:00, endTime: 12:00, room_id:cse124 }, { startTime:13:00, endTime: 15:00, room_id:lotus } ] }

And I invoke the transform like this :

$('#someplace').json2html(data.monday, transforms.renderTimeline, {'events': true});

I want to be able to access "monday" in the function delSchedule(). How do i do this? Please help.

pacific ray
  • 55
  • 2
  • 8

3 Answers3

0

That's not json. It's native Object

"onclick": function(e) {
    var obj = {
        monday: [{
            startTime: 10: 00,
            endTime: 12: 00,
            room_id: cse124
        }, {
            startTime: 13: 00,
            endTime: 15: 00,
            room_id: lotus
        }]
    };
    delSchedule(obj.monday); // pass it here
}

If it is in JSON, then it's no big deal. Just do delSchedule(JSON.parse(jsonStr).monday)

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
0

I think first you need to parse the Json by using jQuery.parseJSON(). Then it will return an object.

var jObject = jQuery.parseJSON(jsonData);

var monday = jObject.monday;

Debabrata Patra
  • 498
  • 3
  • 15
  • Are you sure your data is ready to be accessed in delSchedule function? Are you making any asynchronous call to get data? – Debabrata Patra Dec 18 '14 at 07:36
  • yes. im sure. data is being rendered as html and all other things work. I print out the event using Console.log() in delSchedule(). – pacific ray Dec 18 '14 at 07:59
  • console.log(myevent.obj) inside delSchedule() gives me : Object { startTime:10:00, endTime: 12:00, room_id:cse124 } – pacific ray Dec 18 '14 at 08:10
0

Have a look at json2html.com it describes the available properties of the "e" parameter you're passing to delSchedule(e) in Attributes->jquery->Event Attributes

enter image description here

And to answer your question your delSchedule should look like so

function delSchedule(e) {
     console.log( e.obj );
}

What will e.obj return? That depends on what JSON object you're transforming. For example

$('#someplace').json2html(data, transforms.renderTimeline);

would create one element and

e.obj === { monday:[ { startTime:10:00, endTime: 12:00, room_id:cse124 }, { startTime:13:00, endTime: 15:00, room_id:lotus } ] }

with

$('#someplace').json2html(data.monday, transforms.renderTimeline);

would create two elements and each one would have

e.obj === { startTime:10:00, endTime: 12:00, room_id:cse124 }
e.obj === { startTime:13:00, endTime: 15:00, room_id:lotus }

FYI you're transform doesn't need the events:true option .. that's only if your using the json2html.transform function directly

Chad Brown
  • 1,627
  • 1
  • 13
  • 22