Dynamic nested collapsible set based on dynamic section in jquery mobile - jquery-mobile

I follow this answer for creating collapsible set. now I want to create nested collapsible set. I have a json like:
[
{
"sid": "26",
"tour_name": "4",
"day": "2",
"time": "16:00",
"main_location": "Madurai",
"sub_location": "Mahal",
"site_link": "www.maduraimahal.com",
"description": "test"
},
{
"sid": "25",
"tour_name": "4",
"day": "2",
"time": "13:00",
"main_location": "Trichy",
"sub_location": "Rockport",
"site_link": "www.rockport.com",
"description": "Test"
},
{
"sid": "24",
"tour_name": "4",
"day": "2",
"time": "12:00",
"main_location": "Madurai",
"sub_location": "Temple",
"site_link": "www.madurai.com",
"description": "Test "
},
{
"sid": "17",
"tour_name": "4",
"day": "1",
"time": "09:00",
"main_location": "Chennai",
"sub_location": "City Centre",
"site_link": "www.wikipedia.com",
"description": "test"
},
{
"sid": "16",
"tour_name": "4",
"day": "1",
"time": "08:00",
"main_location": "Chennai",
"sub_location": "Spencer Plaza",
"site_link": "www.spencer.com",
"description": "test"
},
{
"sid": "15",
"tour_name": "4",
"day": "1",
"time": "07:00",
"main_location": "Chennai",
"sub_location": "Express Avenue",
"site_link": "www.wikipedia.com",
"description": "test"
}]
From this json I want to create collapsible set like following type
day-->First collapsiple
---main_location-- >second collapsiple
---sublocation-->listview
Exmaple
day 1
--Chennai
---expressavenue
---Spencer Plaza
---City Centre
day 2
--Trichy
---Rockfort
-- Madurai
---Mahal
---Temple
I am trying following method but can't group like this
$(document).on('pageshow', '#itienary', function (event, ui) {
$.getJSON('url', function (data) {
var collapsible = [];
var days = [];
var collapsible1 = [];
var mainlocation = [];
$.each(data, function (i, v) {
collapsible.push(v.day);
$.each(collapsible, function (i, v) {
if ($.inArray(v, days) === -1) {
days.push(v);
days.sort();
}
});
});
$.each(days, function (j, main) {
var day = main;
var locations = '';
$.each(data, function (k, val) {
var mainloc = val.main_location;
if (val.day == day) {
collapsible1.push(mainloc);
}
$.each(collapsible1, function (i, v) {
if ($.inArray(v, mainlocation) === -1) {
mainlocation.push(v);
mainlocation.sort();
}
});
});
$.each(mainlocation, function (i, loc) {
var parent = loc;
var elements = '';
$.each(data, function (x, sub) {
var subLoc = sub.sub_location;
var time = sub.time;
var sitelink = sub.site_link;
if (sub.main_location == parent) {
elements += '<li><h2>' + subLoc + '</h2><p><strong>' + time + '</strong></p><p class="ui-li-aside"><strong>Sitelink: ' + sitelink + '</strong></p></a></li>';
}
});
$("#itinerary-list").append($("<div/>", {
"data-role": "collapsible",
"data-iconpos": "left",
"data-collapsed-icon": "arrow-d",
"data-expanded-icon": "arrow-u",
"class": day
}).append($("<h3/>").text("Day " + day)).appendTo($("<div/>"), {
"data-role": "collapsible",
"data-iconpos": "left",
"class": parent
}).append($("<h3/>").text(parent)).append($("<ul/>", {
"data-role": "listview",
"data-theme": "b"
}).append(elements).listview()));
$('#itinerary-list').collapsibleset('refresh');
});
});
});
});
How can I done this?

Related

Unable to visualize series correctly on custom timeline (Error, unable to access property 0, c.points is undefined)

I am attempting to cluster a rather large amount of data segments into Series on a timeline chart. Initially, enabling the sections on the legend plotted each data point as a filterable section, but I am unable to correctly get it working with the individual series.
Here is the code being used at present:
$.ajax({
type: "GET",
url: '/newestccoi/api/ccoi_visualization_engine.php',
dataType: "json",
data: {
sid: <?php echo $_GET['sid'] ?>
},
success: function(res) {
const time = new Highcharts.Time();
var data = [];
console.log(res);
let constructedSeries = [];
function toDateTime(secs) {
var t = new Date(1970, 0, 1);
t.setSeconds(secs);
return t;
};
let distance = 50;
for (value of Object.values(res)) {
let seriesEntry = {};
const arr_start = value[0];
const arr_end = value[value.length - 1];
//Construct our Data array with ALL entries in a given subsession
for (let i = 0; i < value.length; i++) {
let tempObj = value[i];
let obsTime = toDateTime(tempObj.seconds);
temp_data = {
name: tempObj.notes,
label: tempObj.name,
description: arr_start.sublabel,
x: obsTime.getTime(),
};
data.push(temp_data);
}
//Craft individual series entry (object) with the data object.
console.log(distance);
seriesEntry = {
dataLabels: {
allowOverlap: true,
distance: distance,
format: '<span style="color:{point.color}">● </span><span style="font-weight: bold;" > ' +
'{point.x:%M:%S}</span><br/>{point.label}'
},
marker: {
symbol: 'circle'
},
data: data
};
constructedSeries.push(seriesEntry);
//Clear data object;
data = [];
distance += 50;
}
Highcharts.chart('container', {
chart: {
type: 'timeline',
zoomType: 'x'
},
accessibility: {
screenReaderSection: {
beforeChartFormat: '<h5>{chartTitle}</h5>' +
'<div>{typeDescription}</div>' +
'<div>{chartSubtitle}</div>' +
'<div>{chartLongdesc}</div>' +
'<div>{viewTableButton}</div>'
},
point: {
valueDescriptionFormat: '{index}. {point.label}. {point.description}.'
}
},
xAxis: {
type: 'datetime',
labels: {
format: '{value:%M:%S}'
},
visible: true
},
yAxis: {
visible: false
},
legend: {
enabled: true
},
title: {
text: 'Session path timeline visualization'
},
subtitle: {
text: 'CCOI</a>'
},
colors: [
'#4185F3',
'#427CDD',
'#406AB2',
'#3E5A8E',
'#3B4A68',
'#363C46'
],
plotOptions: {
timeline:{
legendType: '',
colorByPoint: false,
showInLegend: true,
}
},
series: constructedSeries
});
},
error: function(xhr) {
//Do something to handle our error.
console.log(xhr);
}
})
Introducing the code necessary to properly display the individual Series on the legend, legendType: '', broke the timeline and returned the following Javascript Error:
Uncaught TypeError: can't access property 0, c.points is undefined
Any help on this matter would be greatly appreciated! I am including a sample piece of JSON to rid any confusion on what exactly this data looks like. If believes there's a better way of doing this in Highchart, I'm open to completely redoing it as well! I have a feeling this may have to do with the fact I am using a hacky method of changing the time scale on the timeline to Minutes and Seconds, but I am not absolutely sure.
{
"1": [
{
"sid": "1",
"sublabel": "Problem 1",
"subsession": "1",
"notes": "Student is trying to draw star in Scratch.",
"seconds": "0",
"name": "start"
},
{
"sid": "1",
"sublabel": "Problem 1",
"subsession": "1",
"notes": null,
"seconds": "30",
"name": "independent"
}
],
"2": [
{
"sid": "1",
"sublabel": "Problem 1",
"subsession": "2",
"notes": "Student asks for peer's help with problem.",
"seconds": "30",
"name": "start"
},
{
"sid": "1",
"sublabel": "Problem 1",
"subsession": "2",
"notes": null,
"seconds": "30",
"name": "interactive"
},
{
"sid": "1",
"sublabel": "Problem 1",
"subsession": "2",
"notes": null,
"seconds": "30",
"name": "problem_solving"
},
{
"sid": "1",
"sublabel": "Problem 1",
"subsession": "2",
"notes": null,
"seconds": "90",
"name": "problem_solving"
},
{
"sid": "1",
"sublabel": "Problem 1",
"subsession": "2",
"notes": null,
"seconds": "90",
"name": "problem_solving"
},
{
"sid": "1",
"sublabel": "Problem 1",
"subsession": "2",
"notes": null,
"seconds": "90",
"name": "interactive"
}
],
"3": [
{
"sid": "1",
"sublabel": "Problem 1",
"subsession": "3",
"notes": "Student opens Google Maps.",
"seconds": "90",
"name": "start"
},
{
"sid": "1",
"sublabel": "Problem 1",
"subsession": "3",
"notes": null,
"seconds": "135",
"name": "independent"
}
],
"4": [
{
"sid": "1",
"sublabel": null,
"subsession": "4",
"notes": null,
"seconds": "135",
"name": "start"
},
{
"sid": "1",
"sublabel": null,
"subsession": "4",
"notes": null,
"seconds": "135",
"name": "interactive"
},
{
"sid": "1",
"sublabel": null,
"subsession": "4",
"notes": null,
"seconds": "165",
"name": "non_computing_communication"
},
{
"sid": "1",
"sublabel": null,
"subsession": "4",
"notes": null,
"seconds": "170",
"name": "interactive"
},
{
"sid": "1",
"sublabel": null,
"subsession": "4",
"notes": null,
"seconds": "170",
"name": "problem_solving"
},
{
"sid": "1",
"sublabel": null,
"subsession": "4",
"notes": null,
"seconds": "170",
"name": "problem_solving"
},
{
"sid": "1",
"sublabel": null,
"subsession": "4",
"notes": null,
"seconds": "180",
"name": "problem_solving"
},
{
"sid": "1",
"sublabel": null,
"subsession": "4",
"notes": null,
"seconds": "180",
"name": "interactive"
}
]
}
I attempted to follow the help of this a separate stackoveflow post that goes over the method for using multiple series within a timeline, but nothing there seemed to be the cause of why I am getting this error.
Edit
Chart when legendType is default
Chart when legendType is unset to ''
My goal is to actually have the legend correctly display 1-10 like in the second image, while actually displaying the data. When legendType is set, for some reason or another it treats every individual data object in the data array as a series, when it should actually be display the 10 series objects that are present in the array.
If this still doesn't clear things up, let me know and I'll create a codepen!

How to Insert Options with Ajax while Having Option Group in select2?

I have a multiselect dropdown with below data:
var fruitArray = [];
var fruitGreen = [];
var fruitNotGreen = [];
fruitGreen = {
"text": "Green",
"children": [
{ "id": "Watermelon", "text": "Watermelon" },
{ "id": "Lime", "text": "Lime" },
{ "id": "Guava", "text": "Guava" },
{ "id": "Avocado", "text": "Avocado" },
{ "id": "Kiwi", "text": "Kiwi" },
]};
fruitNotGreen = {
"text": "Not Green",
"children": [
{ "id": "Apple", "text": "Apple" },
{ "id": "Orange", "text": "Orange" },
{ "id": "Berries", "text": "Berries" },
{ "id": "Grape", "text": "Grape" },
{ "id": "Pineapple", "text": "Pineapple" },
]};
fruitArray.push(fruitGreen, fruitNotGreen);
$(".select2").select2({
placeholder: "-- Select Fruits --",
allowClear: true,
maximumSelectionLength: 50,
dropdownAutoWidth: true,
data: fruitArray,
multiple: true,
closeOnSelect: false
});
How to Insert Options with Ajax while Having Option Group? Please help
I'm using select2 multiple dropdown from https://select2.org

How do take the values from a large JSON response with multiple arrays and brackets?

I have a JSON response (over 500lines) , that has several arrays and brackets inside. Basically an overly nested response.
I am using Alamofire for fetching and i get a JSONData as response. But I don't know how to take values from the response and print it on a custom cell .
I am using Alamofire 5 to get the JSON response.
Please excuse if this sounds too newbieish , I am new to iOS developing.
{
"availabilityResultList": [
{
"availabilityRouteList": [
{
"availabilityByDateList": [
{
"originDestinationOptionList": [
{
"fareComponentGroupList": [
{
"boundList": [
{
"availFlightSegmentList": [
{
"addOnSegment": false,
"flightSegment": {
"journeyDuration": "PT1H15M",
"distance": 1,
"departureAirport": {
"locationName": "LOC",
"cityInfo": {
"country": {
"locationName": "LOC",
"currency": {
"code": "USD"
},
"locationCode": "LOC",
"locationNameLanguage": "EN"
},
"city": {
"locationName": "LOC",
"locationCode": "LOC",
"locationNameLanguage": "EN"
}
},
"codeContext": "ASD",
"language": "EN",
"locationCode": "LOC"
},
"equipment": {
"airEquipType": "ABC",
"changeofGauge": false
},
"remark": "NO",
"ticketType": "PP",
"onTimeRate": 0,
"flightNotes": [
{
"deiCode": 0
}
],
"flightSegmentID": "",
"flownMileageQty": 0,
"flightNumber": "106",
"groundDuration": "",
"trafficRestriction": {
"code": "",
"explanation": {}
},
"stopQuantity": 0,
"codeshare": false,
"secureFlightDataRequired": false,
"departureDateTime": {
"month": 5,
"hour": 6,
"year": 2019,
"timezone": 270,
"day": 6,
"minute": 45,
"second": 0
},
"ondControlled": false,
"arrivalDateTime": {
"month": 5,
"hour": 8,
"year": 2019,
"timezone": 270,
"day": 6,
"minute": 0,
"second": 0
},
"airline": {
"code": "RQ"
},
"arrivalAirport": {
"locationName": "LOC",
"cityInfo": {
"country": {
"locationName": "AA",
"currency": {
"code": "USD"
},
"locationCode": "AA",
"locationNameLanguage": "EN"
},
"city": {
"locationName": "loc",
"locationCode": "LOC",
"locationNameLanguage": "EN"
}
},
"codeContext": "ASD",
"language": "EN",
"locationCode": "LOC"
},
"sector": "ASD",
"accumulatedDuration": ""
},
"marriageGroup": "-1"
}
],
"boundCode": "Outbound"
}
]
}
]
},
JSON is a very simple format and easy to read. There are only two different collection types and four value types.
{} is a dictionary and is subscripted by key
[] is an array and is subscripted by index (zero-based)
Everything in double quotes is String, even "123" and "false"
false and true is Bool
Numeric values are Double, without fractional digits Int
<null> is NSNull
With SwiftyJSON you get distance in flightSegment with (root represents the top level object`)
root["availabilityResultList"][0]["availabilityRouteList"][0]["availabilityByDateList"][0]["originDestinationOptionList"][0]["fareComponentGroupList"][0]["boundList"][0]["availFlightSegmentList"][0]["flightSegment"]["distance"]

Send ajax request in PeriodSelector AMChart

I have this javascript code to draw an amchart stock, just the PeriodSelector part:
var periodSelectorAjax = new AmCharts.PeriodSelector();
periodSelectorAjax.periods = [
{period: "F1", label: "Phase 1"},
{period: "F2", label: "Phase 2"},
{period: "F3", label: "Phase 3"},
{period: "MAX", selected: true, label: "MAX"}
];
I need to intercept when user click on period to reload the graph, I see that I can do it with this, from here:
var chart = AmCharts.makeChart( "chartdiv", {
"type": "stock",
// ...
"periodSelector": {
"position": "left",
"periods": [ {
"period": "MM",
"selected": true,
"count": 1,
"label": "1 month"
}, {
"period": "YYYY",
"count": 1,
"label": "1 year"
}, {
"period": "YTD",
"label": "YTD"
}, {
"period": "MAX",
"label": "MAX"
} ],
"listeners": [ {
"event": "changed",
"method": function( event ) {
if ( event.predefinedPeriod !== undefined ) {
console.log( event.predefinedPeriod, event.count );
}
}
} ]
},
// ...
} );
But I don't know how to put the last listeners part and use it with my code.
thanks
You can either use the addListener method or just set the listeners array directly on your periodSelectorAjax variable.
addListener example:
periodSelectorAjax.addListener("changed", function(event) {
// your code here
});
listeners property example:
periodSelectorAjax.listeners = [{
"event": "changed",
"method": function(event) {
// ...
}
}]

How to parse a nested json array in CI

i am trying to parse the following json
{
"offerinfo": {
"offername": "chaos",
"offertype": "3",
"brand": "1",
"subbrand": "0",
"categories": "1,2",
"Tags": "4,6",
"promotiontype": "1",
"currency": "1",
"promotioncode": "1",
"offertitle": "1",
"offerdescription": "ad",
"addtionalterms": "asdasd",
"retaildescription": "asdasd"
},
"media": {
"video": "",
"images": []
},
"availability": [
{
"airports": "1,4,6",
"stores": "3,4,5",
"startdate": "2014",
"enddate": "3434"
}],
"featured": {
"categories": "",
"slot": ""
}
}
COde for parsing is
$json = json_decode(trim(file_get_contents('php://input')),true);
//print_r($json)."<br/>";
//var_dump($json);
foreach($json as $key=>$value)
{
var_dump($value);
$offername = $value['offername']."<br/>";
//echo $media = $value['video'];
$mediaimage = $value['images'];
foreach($mediaimage as $a)
{
//echo $a['MediaFile'];
}
//echo $avail = $value['airports'];
echo $key->{'availability'};
//var_dump($avail);
// foreach($avail as $b)
// {
// echo $b['airports'];
//}
//var_dump($mediaimage);
}
i got the offername etc those in ogger info dictionary, but i am unable to parse the information from availability
can some one kindly help me
$json['availability'] is an array so you can access it like this
foreach ($json['availability'] as $availability) {
echo $availability["airports"];
// etc
}

Resources