2012-09-12 51 views
1

我正在嘗試使用YUI來顯示錶。使用但使用bookorders數據,而不是將數據推入json數組

YAHOO.example.Data = { 
     bookorders: [ 
      {id:"po-0167", date:new Date(1980, 2, 24), quantity:1, amount:4, title:"A Book About Nothing"}, 
      {id:"po-0783", date:new Date("January 3, 1983"), quantity:null, amount:12.12345, title:"The Meaning of Life"}, 
      {id:"po-0297", date:new Date(1978, 11, 12), quantity:12, amount:1.25, title:"This Book Was Meant to Be Read Aloud"}, 
      {id:"po-1482", date:new Date("March 11, 1985"), quantity:6, amount:3.5, title:"Read Me Twice"} 
     ] 
    } 

    YAHOO.example.Basic = function() { 
     var myColumnDefs = [ 
      {key:"id", sortable:true, resizeable:true}, 
      {key:"date", formatter:YAHOO.widget.DataTable.formatDate, sortable:true, sortOptions:{defaultDir:YAHOO.widget.DataTable.CLASS_DESC},resizeable:true}, 
      {key:"quantity", formatter:YAHOO.widget.DataTable.formatNumber, sortable:true, resizeable:true}, 
      {key:"amount", formatter:YAHOO.widget.DataTable.formatCurrency, sortable:true, resizeable:true}, 
      {key:"title", sortable:true, resizeable:true} 
     ]; 

     var myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.bookorders); 
     myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY; 
     myDataSource.responseSchema = { 
      fields: ["id","date","quantity","amount","title"] 
     }; 

     var myDataTable = new YAHOO.widget.DataTable("basic", 
       myColumnDefs, myDataSource, {caption:"DataTable Caption"}); 

     return { 
      oDS: myDataSource, 
      oDT: myDataTable 
     }; 
    }(); 

下面的例子中,我從查詢使用esri.tasks.QueryTask

數據庫中的結果,所以我得到了通過數據迭代我得到填充書籍json數組。

for (var i=0, il=results_books.features.length; i<il; i++) { 
    var featureAttributes = results_books.features[i].attributes; 
     var string = " id : \"" + results_books.features[i].attributes[0] + "\","; 
     var string = string + " date : \"" + results_books.features[i].attributes[1] + "\","; 
     var string = string + " quantity: \"" + results_books.features[i].attributes[2] + "\","; 
     var string = string + " amount: \"" + results_books.features[i].attributes[3] + "\","; 
     var string = string + " title: \"" + results_books.features[i].attributes[4] + "\""; 

} 

但我怎麼推串入JSON陣列是讀正確的屬性呢?

編輯:加逗號字符串

+0

可以安全地說,我可以實例化書籍數組,然後使用連接的字符串,並執行此'bookorders.push(s)' – jonleech

回答

1

只需使用對象文本,而不是建立一個字符串:

for (var i=0, il=results_books.features.length; i<il; i++) { 
    var featureAttributes = results_books.features[i].attributes; 
    var book = { 
     "id" : featureAttributes[0], 
     "date" : featureAttributes[1], 
     "quantity" : featureAttributes[2], 
     "amount" : featureAttributes[3], 
     "title" : featureAttributes[4] 
     }; 
     // push into array 
} 

無需使用中介字符串,創建一個完整的out對象馬上。

+0

感謝您的及時響應 – jonleech

+0

@jonleech很高興我可以幫助 –