1

I have an ajax function in jQuery that returns me a list of JSON object. From the data, I want to populate a timeline which is defined as follows:

Timeline

$('#timeline-container-basic').timelineMe({
  items: [
    {
      type: 'smallItem',
      label: 'Name',
      shortContent: 'Address',
      forcePosition: 'right'
    },
    {
      type: 'smallItem',
      label: 'Name2',
      shortContent: 'Address2',
      forcePosition: 'right'
    }
  ]
});

The Ajax Function:

$.ajax({
            url: 'GetCustomers',
            type: 'GET',
            dataType: 'json',
            success: function (data) {

            for (var i = 0; i < data.length; i++) {
                console.log(data[i].Name);
            }

What I want to achieve is that I want the items object to be populated from the ajax data. Could someone advise how can I achieve this ?

1
  • you just need to assign one wariable to your items which is just a array of objects. Commented Mar 1, 2018 at 7:04

2 Answers 2

1

As per my understanding your json would be.

 $.ajax({
        url: 'GetCustomers',
        type: 'GET',
        dataType: 'json',
        success: function (data) { 
                var AssignArray=[];
                for (var i = 0; i < data.length; i++) {
                var obj= {
                type: data[i].smallItem,
                label: data[i].Name,
                shortContent: data[i].Address,
                forcePosition: data[i].right
                }
             AssignArray.push(obj);   
            } 
            $('#timeline-container-basic').timelineMe({  items:AssignArray});
     }
});
Sign up to request clarification or add additional context in comments.

Comments

1

Use like this.

timelinejson = []
$.ajax({
    url: 'GetCustomers',
    type: 'GET',
    dataType: 'json',
    success: function (data) {
        $.each(data,function(){
            timelinejson.push({
                type: this.smallItem,
                label: this.Name,
                shortContent: this.Address,
                forcePosition: this.right
            });
        });
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.