Click here to Skip to main content
15,909,656 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have data in Array format as below... (sample one) this is an array..Based on data , I need to generate a html table by custom condition... say, table header will be day1,2,3,4,5,6 and below array has to be based for each header to add row based on the conditions... I have given the details for reference

var array= [{"day":1,"summary":"proge1"},
{"day":1,"summary":"proge2"},
{"day":1,"summary":"proge3"},
{"day":2,"summary":"Tues1"},
{"day":2,"summary":"Tues2"},
{"day":2,"summary":"Tues3"},
{"day":3,"summary":"Wed1"},
{"day":3,"summary":"Wed2"},
{"day":3,"summary":"Wed3 "},
{"day":4,"summary":"Thur1"},
{"day":4,"summary":"Thur2"},
{"day":5,"summary":"Fri1"},
{"day":6,"summary":"Sat1"}]
I want to generate a html table based on this data such that it should display as below..can someone please suggest me how to accomplish this..










Day1Day2Day3Day4Day5 Day6

output:

----------------------------------------------
Day1 |Day2 Day3 Day4 Day5 Day6
-----------------------------------------------
proge1 Tue1 Wed1 Thur1 Fri1 Sat1

proge2 Tue2 Wed2 Thur2
proge3 Tue3 Wed3

What I have tried:

here is wat i did....


script

$(function () {
var json = [];
var tr,start=0;
console.log('here json check here',json);
for (var i = 0; i < json.length; i++) {
tr = $('');
tr.append("" + json[i].summary + "");
$('table').append(tr);
}
}},


But I am thinking how to call this function for each td....say... assign some value for each td like 1,2,3,4 and pass that value to function and get required data for each td.....Experts... I am new to html please advise if you have any idea
Posted
Updated 30-Jan-17 19:46pm

try this

HTML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="jquery.js"></script>

    <script>
        var array = [{ "day": 1, "summary": "proge1" },
{ "day": 1, "summary": "proge2" },
{ "day": 1, "summary": "proge3" },
{ "day": 2, "summary": "Tues1" },
{ "day": 2, "summary": "Tues2" },
{ "day": 2, "summary": "Tues3" },
{ "day": 3, "summary": "Wed1" },
{ "day": 3, "summary": "Wed2" },
{ "day": 3, "summary": "Wed3 " },
{ "day": 4, "summary": "Thur1" },
{ "day": 4, "summary": "Thur2" },
{ "day": 5, "summary": "Fri1" },
{ "day": 6, "summary": "Sat1" }];

        $(function () {
            var temp = array;
            var uniqdays = [];
            for (var i = 0; i < array.length; i++)  
                if (uniqdays.indexOf(array[i].day) == -1) uniqdays.push(array[i].day);
            var maxAry = [];
            for (var i = 0; i < uniqdays.length; i++) {
                var tempary = [];
                for (var j = 0; j < array.length; j++) {
                    if(array[j].day == uniqdays[i])
                        tempary.push(array[j].summary)
                }
                maxAry.push(tempary.length);
            }           
            var maxValue = maxAry.sort().pop();

            var $tbl = $('#tbl');
            var $trhead = $('<tr>').appendTo($tbl);
            for (var i = 0; i < uniqdays.length; i++) {
                var day = uniqdays[i]; 
                var $td = $('<td>').appendTo($trhead);
                $td.text('day' + day); 
            }
           
            for (var i = 0; i < maxValue; i++) {
                var $tr = $('<tr>').appendTo($tbl);
                for (var j = 0; j < uniqdays.length; j++) {
                    var day = uniqdays[j];

                    for (var k = 0; k < temp.length; k++) {
                        if (temp[k].day == day && temp[k].summary != '') {
                            var $td = $('<td>').appendTo($tr);
                            $td.text(temp[k].summary);
                            temp[k].summary = '';
                            break;
                        }
                    }
                }
            }
        });

    </script>
</head>
<body>
    <table cellspacing="1" cellpadding="1" border="1" id="tbl">        
    </table>

</body>
</html>


Demo: JSFiddle[^]
 
Share this answer
 
v2
Comments
Member 12974741 31-Jan-17 14:01pm    
you are awsm!! Thanks for that Karthik....I am trying to enhance the code with real data... but when i execute it shows some incorrect o/p... I think its based on td value passed....??? please suggest on this..

For more reference:
here is real array data :
Real Array : https://gist.github.com/mahakT/df9632be8b8929197041460efd335ac8
get : the sequecnce of data based on timing
Display in table format: this link shows data as per the code suggested by you.....https://firstappth.mybluemix.net/ ( but if you view.. column 1 has fetched colum 6 data as well.... not sure if that bec of td?)

This is the expected output in table....

Monday |Tuesday | Wednesday.....
-----------------------------------
Summary | Summary | Summary
4.00-4.45| 4.00-4.45 |4.00-4.45
---------------------------------
Summary | Summary |Summary
4.45-5.30 | 4.00-4.45 |4.00-4.45



table should show exact data as ics events(you can view the ics in calendar to see how exactly the output should luk like)... Array has the data from this ics....

https://calendar.google.com/calendar/ical/o8mfhn5drq7t875vosh3b5kdao%40group.calendar.google.com/public/basic.ics


Appreciate if you could provide your suggestion/thoughts...Thanks!!
Karthik_Mahalingam 31-Jan-17 23:19pm    
could you post the expected output as screenshot
Member 12974741 31-Jan-17 21:41pm    
please view this url in IE.....https://firstappth.mybluemix.net/

I used the above code... and just updated my array and you can see the output in above url.... Am i missing something here?
Karthik_Mahalingam 31-Jan-17 23:19pm    
the url is not responding
Member 12974741 31-Jan-17 23:42pm    
Hi.... How to send a screenshot... as i couldnt paste in the question/comment section...
To simplify you HTML generation you will have to transform your array to different grouping, I would suggest transforming data to array based on Day, Something like

[{"day":1, summary : ["proge1","proge2","proge3"],
{"day":2,"summary":["Tues1", "Tues2", "Tues3"],
{"day":3,"summary":["Wed1","Wed2","Wed3 "],
{"day":4,"summary":["Thur1","Thur2"],
{"day":5,"summary":["Fri1"],
{"day":6,"summary":["Sat1"]}]

Once you transform this, get the max array length out of the summary array for all day's. In the above case, it will be 3. Put a loop on top of max length you get, an internal loop on no of days. Before adding data from the day's record check if the array day pointer does have data for that index.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900