0

私は次のJSONを持っています:(あなたのために少し単純化されています)

{ returnJSON = {
    studentDataVOs = {
        finalGrades = (
            {
                grade = A;
                percent = 100;
                sectionid = 7744;
                reportingTermId = 801;
            },
            {
                grade = B+;
                percent = 89;
                sectionid = 7745;
                reportingTermID = 801;
            });
        reportingTerms = (
            {
                id = 801;
                title = S1;
            },
            {
                id = 802;
                title = S2;
            });
        sections = (
            {
                id = 7744;
                termID = 801;
                courseTitle = Physics;
                courseCode = 88A;
            },
            {
                id = 7745;
                termID = 801;
                courseTitle = Government;
                courseCode = 90B;
            });
        };
    };
}

Appcelerator Titaniumを使用してアプリを作成しています。このアプリは、次のことを示すデータを含むテーブルビューを表示します。


物理学(88A)-S1(グレード:A、100%)


政府(90B)-S1(グレード:B +、89%)


...等々...

テーブルビューを設定しました。次のコードは、セクションからデータを抽出し、テーブルビューのラベルに配置します。

var response = JSON.parse(response);
var sections = response.returnJSON.studentDataVOs.sections;
for (i=0;i<sections.length;i++) {
    var courseName = sections[i].courseTitle;
    var courseCode = sections[i].courseCode;
}

私が理解できないのは、個々のクラスの成績と学期のタイトルを取得する方法です。ご覧のとおり、各セクションのデータにはIDとtermIDが含まれており、finalGradesのセクションとIDまたはtermIDを含むreportingTermsに移動します。ここで、最終的な成績、パーセント、用語のタイトルを取得する必要があります。

誰かがこれを手伝ってくれますか?私はこれを理解しようとして2日間オンとオフを試してきました...

4

2 に答える 2

0

セクション->最終成績

セクションリストでは、次のデータセットのクエリに使用できる変数をテーブル行に渡します。データを最終グレードのデータに関連付けるにはセクションIDが必要なので、テーブルの行に追加します。テーブルを作成するとき:

// This loop to loop through the data.
function getSections(_args){
   var response = JSON.parse(response);
   var sections = response.returnJSON.studentDataVOs.sections;
    for (i=0;i<sections.length;i++) {
       createMyRow({
         courseName: sections[i].courseTitle,
         courseCode: sections[i].courseCode,
         sectionId: sections[i].id
       });
    }
}

// This function creates the rows for the table 
function createMyRow(_args){
   // the sectionId is now included in the tableRow and can be used later for your 
   // next query.
   // allows the sectionId value is now can be queried when a user clicks it 
   // through e.rowData.
   var tableRow = Ti.UI.createTableViewRow({
      sectionId: _args.sectionId
   });

   var title = Ti.UI.createLabel({
      text: _args.courseName
   });
   tableRow.add(title);

    return tableRow;
    }

    var tableView = Ti.UI.createTableView();
var data = [];
function refreshTable()

   data = getSections();
   var rows = [];
   for( var i = 0; i<data.length; i++){
      rows.push(createMyRow(data[i]);
   }
   tableView.setData(rows);
}

// this passes the value of the sectionId so you can use it to look up your next section.
tableView.addEventListener('click', function(e){
   getFinalGrades({
      sectionId: e.rowData.sectionId
   })
});
function getFinalGrades(_args){
   var finalGrades = response.returnJSON.studentDataVOs.finalGrades
   var data = [];
   for (i=0;i<finalGrades.length;i++) {
     if(finalGrades[i].sectionId == _args.sectionId){
       data.push({
         grade: finalGrades[i].grade,
         percent: finalGrades[i].percent
       });
     }
   }
   return data;
}

私はこれを一緒にハッキングして、コード例のいくつかをすばやく調べました。getFinalGrades関数の最後に、クリックしたsectionIdからのみのfinalGradesの配列があります。

于 2012-08-27T13:47:32.187 に答える
0

リストしたフィールドごとにインデックスを作成する必要があります。それはとても簡単です:

//for example you have user list
var users = [{
    id : 1, email : 'user@gmail.com',
    nickname : 'John'
}, {
    id : 2, email : 'user@stackoverflow.com',
    nickname : 'Peter'
}];

//and you need to query user by his email, id and nickname
//first, create 3 index list

var usersIdIndex = {}, usersEmailIndex = {},
    usersNicknameIndex = {};

//then fill them

users.forEach(function(user){
    usersIdIndex[user.id] = user;
    usersEmailIndex[user.email] = user;
    usersNicknameIndex[user.nickname] = user;
});

//now you can get users by any of this fields
//for example

console.log(usersIdIndex[2].nickname== 'Peter');
console.log(usersNicknameIndex['John'].email == 'user@gmail.com');
于 2012-08-27T04:40:14.343 に答える