Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,
I have following Json Object
"lstTimesheet":[{"ConventionID":1,"PhaseID":139,"ActivityID":259,"TaskID":10,"Day1":8.00,"Day2":8.00,"Day3":0.00,"Day4":0.00,"Day5":0.00,"Day6":0.00,"Day7":0.00,"MSPFlag":0,"TimesheetFormat":"MH ","Freeze":0}]}


What I'm trying is, I want only iterate only through keys which has "Day"

I was trying following:
$.each(data.obj.lstTimesheet, function (i, obj) {
                                var txtID = 'txtTime' + (i + 1);
                                var Day = 'Day' + (i + 1);

                                $('#Grid tbody tr').eq(i).each(function () {
                                    $(this).find('td').eq(1).find(txtID).append(obj[Day]);
                                });
                            });


But it is not doing.

Any idea where I'm going wrong??

Thanks in Advance
Posted
Updated 15-Nov-13 0:10am
v4
Comments
What it is doing currently? Did you put a debugger and check?

If you can create a jsfiddle, it will be easier for us to check the issue. Just create a fiddle jsfiddle.

Hello prashant,

The listItemsSheet is a JSON Array and in this case contains only a single element. Your loop essentially is going to return the zeroth element which itself is a Json Object. So the loop is going to get called only once, and hence your code fails. The more appropriate way is to re-write this loop as shown below.
JavsScript
$.each(data.obj.lstTimesheet, function (i, obj) {
    if (isJSON(obj)) parseMe(obj);
});

function parseMe(data) {
    var i = 0;

    $.each(obj, function(k, val) {
        if (/^Day/.test(k) {
            var txtID = 'txtTime' + k.substring(;
            $('#Grid tbody tr').eq(i).each(function () {
                $(this).find('td').eq(1).find(txtID).append(val);
            });
            i++;
        }
    });
});

function isJSON(data) {
    var isJson = false
    try {
       var json = JSON.stringify(data);
       isJson = (json != null);
    } catch (ex) {
        console.error('data is not JSON');
    }
    return isJson;
}

Please have a look at a sample demo here[^].

Regards,
 
Share this answer
 
Comments
dhage.prashant01 11-Nov-13 7:26am    
Thanks Prasad..!!
Prasad Khandekar 11-Nov-13 7:31am    
You are welcome Prashant.
Hi friends,
I came up with solution, might it helps others searching for the similar thing

var txtID;
                            var i = 1;
                            for (key in data.obj.lstTimesheet[0]) {

                                if (key.indexOf('Day') != -1) {
                                    var value = data.obj.lstTimesheet[0][key].toFixed(2);
                                    txtID = '#txtTime' + i;

                                    if (key.trim() == 'Day1') 
                                        $('#Grid tbody tr').eq(0).find('td').eq(1).find(txtID).val(value);
                                    else if (key.trim() == 'Day2') 
                                        $('#Grid tbody tr').eq(1).find('td').eq(1).find(txtID).val(value);
                                    else if (key.trim() == 'Day3') 
                                        $('#Grid tbody tr').eq(2).find('td').eq(1).find(txtID).val(value);
                                    else if (key.trim() == 'Day4') 
                                        $('#Grid tbody tr').eq(3).find('td').eq(1).find(txtID).val(value);
                                    else if (key.trim() == 'Day5') 
                                        $('#Grid tbody tr').eq(4).find('td').eq(1).find(txtID).val(value);
                                    else if (key.trim() == 'Day6') 
                                        $('#Grid tbody tr').eq(5).find('td').eq(1).find(txtID).val(value);
                                    else 
                                        $('#Grid tbody tr').eq(6).find('td').eq(1).find(txtID).val(value);

                                    i = i + 1;
                                }
                            }
 
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