[ 17 posts ] Go to page Previous1, 2

Marco Asbreuk

YUI Contributor

  • Username: itsasbreuk
  • Joined: Mon Nov 16, 2009 5:14 am
  • Posts: 459
  • Location: Netherlands
  • Twitter: itsasbreuk
  • GitHub: ItsAsbreuk
  • Gists: ItsAsbreuk
  • IRC: Marco Asbreuk
  • Offline
  • Profile

Re: How to get yui datatable recordset in json format

Post Posted: Fri Jul 27, 2012 12:48 am
+0-
@Sean
Even though it works, better choose a native solution. Iterating comes at the price of performance, especially when you have a bunch of items.

Kind Regards,
Marco.

Bhuvana Pathsamatla

  • Username: Bhuvana
  • Joined: Mon Jun 18, 2012 2:09 am
  • Posts: 15
  • Location: India
  • IRC: Bhuvana
  • Offline
  • Profile

Re: How to get yui datatable recordset in json format

Post Posted: Fri Jul 27, 2012 3:20 am
+0-
Thanks Rohit! It worked as expected.

-Bhuvana

Sean Fitzsimmons

  • Username: seanf
  • Joined: Wed Sep 16, 2009 6:16 am
  • Posts: 78
  • Location: Baltimore, Maryland, USA
  • Offline
  • Profile

Re: How to get yui datatable recordset in json format

Post Posted: Mon Jul 30, 2012 6:16 am
+0-
@Marco

You are correct. Will work on it today. I think I can use JSON.stringify() as satyam suggested, but only use the _oData part and ignore the rest. My RecordSets can be large (1-2K rows), so this should speed things up a bit.

Sean

Sean Fitzsimmons

  • Username: seanf
  • Joined: Wed Sep 16, 2009 6:16 am
  • Posts: 78
  • Location: Baltimore, Maryland, USA
  • Offline
  • Profile

Re: How to get yui datatable recordset in json format

Post Posted: Tue Jul 31, 2012 4:28 am
+0-
@Marco,

I was able to use native methods by doing the following,

Code:
YAHOO.widget.Record.prototype.toJSON = function() {
  return this._oData;
}


and then to convert the RecordSet to a JSON string,
Code:
YAHOO.lang.JSON.stringify(myRecordSet.getRecords(), myWhiteList);


Seems to work fine, but I'm not 100% comfortable adding the toJSON prototype method to Record. All Record instances on my page would be modified, but should be ok since the only thing I care about when converting a Record to JSON is _oData.

Sean

Sean Fitzsimmons

  • Username: seanf
  • Joined: Wed Sep 16, 2009 6:16 am
  • Posts: 78
  • Location: Baltimore, Maryland, USA
  • Offline
  • Profile

Re: How to get yui datatable recordset in json format

Post Posted: Thu Aug 02, 2012 8:11 am
+0-
For anyone still interested,

Code:
YAHOO.widget.Record.prototype.toJSON = function() {
  return this._oData;
}


should have been,

Code:
YAHOO.widget.Record.prototype.toJSON = function() {
  return this.getData();
}

Rohit Shah

  • Username: masterrohitshah
  • Joined: Sat Jul 21, 2012 9:47 pm
  • Posts: 7
  • Offline
  • Profile

Re: How to get yui datatable recordset in json format

Post Posted: Sat Aug 04, 2012 8:58 am
+0-
i didn't understand your method....

it is showing the error 'Deserilize is not defined'.

Can you please post your code....
Please reply....

Todd Smith

YUI Contributor

  • Username: stlsmiths
  • Joined: Thu Nov 05, 2009 10:03 am
  • Posts: 675
  • GitHub: stlsmiths
  • Gists: stlsmiths
  • IRC: t_smith
  • Offline
  • Profile

Re: How to get yui datatable recordset in json format

Post Posted: Sat Aug 04, 2012 10:17 am
+0-
@masterrohitshah: You are getting that error because you are probably using YUI 3 and seanf's method was for YUI 2!

This thread is getting unnecessarily confused. Some of the responses are for DataTable YUI 2.x, and some are for YUI 3. I think this was originally posted on the YUI 3 forums, and then changed to YUI 2 ... :-|

There are differences between the versions and in how to retrieve the recordset, and data for an individual record. If you have a DataTable instance named "myDT", you can do the following;

In YUI 2.x

Code:
var myDT = new YAHOO.widget.DataTable(....);

// to get a single record's data as a JS Object { foo:'bar', baz:12345 }
var recObj = myDT.getRecordSet().getRecord(7).getData();  // returns a JS Object

// to return the recordset as a JS Array of JS Objects, do as seanf's method ...
var RS = myDT.getRecordSet();   // returns a YAHOO RecordSet widget
var jsArray = [];
for (var i=0, i<RS.getLength(); i++) {
    jsArray.push( RS.getRecord(i).getData() );
}

// to convert a JS Object into a JSON Object (i.e. JSON formatted string)
    JSON.stringify( recObj );     // via modern ECMAScript 5 browsers
// OR, if unsure and to be safe, use YUI 2's JSON utility (normalizes across browsers)
    YAHOO.lang.JSON.stringify( recObj );

// both above return as follows;  Example:  JSON.stringify( { foo:'bar', baz:12345 });
//  ... returns "{"foo":"bar","baz":12345}"

// Likewise, you can use the same "stringify" on a whole JS Array ...
//  JSON.stringify( {mydata:jsArray}  )
// ... returns "{"mydata":[ {"foo":"bar","baz":12345}, ... {"foo":"bar","baz":12345} ] }"

To use the YUI 2 JSON library within your code see http://developer.yahoo.com/yui/json/. Seanf's method works in YUI 2, but you can use .stringify on the whole array, and don't have to do each row at a time.


In YUI 3 (say 3.5 forwards)
YUI 3 has some deprecated methods similar to YUI 2 (i.e. recordset, etc..), but they are not recommended and not fully featured. If you are using YUI 3 DT directly, you should adopt the newer non-deprecated methods.
Code:
var myDT = new Y.DataTable(....);

// to get a single record's data as a JS Object { foo:'bar', baz:12345 }
var recObj = myDT.getRecord(7).toJSON();  // returns a JS Object

// to return the recordset as a JS Array of JS Objects, use ModelList's toJSON method
var RS = myDT.data.toJSON();   // returns a JS Array of Objects
// or var RS = myDT.get('data').toJSON(); ... identical, .data is an alias for .get('data')

// to convert a JS Object into a JSON Object (i.e. JSON formatted string)
    JSON.stringify( recObj );     // via modern ECMAScript 5 browsers
// OR, if unsure and to be safe, use YUI 3' JSON module (normalizes across browsers)
    Y.JSON.stringify( recObj );

// the results are identical to the code block for YUI 2 above.

To use the YUI 3 JSON module include "json-stringify" in your YUI().use() loader statement.

If you want to prepend something on your JSON string, you can either do the string concatenation stuff or simply defined a nested object and use .stringify to output it.

Sorry this is a little long, seems like some answers and questions were getting confused regarding which version was asked about. If somebody is still unclear, a new posting might be helpful stating which version is the target 2 or 3.

Todd
  [ 17 posts ] Go to page Previous1, 2
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum