| Page 1 of 1 | [ 6 posts ] |
|
I'm using DataTable from YUI 3.5 backed by a JSON DataSource.
Is there a way to preprocess the JSON results before it's used? I need to consolidate a few columns and potentially insert additional rows. I've tried providing a custom callback to datasource.load() but that doesn't do the trick. Or at least, I'm not sure how to pass my changes up the stack. Any suggestions? |
|
Hi Mark,
It's pretty straightforward to access the response object from within the .load callback. You'd do something like; Code: myDataTable.datasource.load({ request: 'your-request-parameters-here', callback : { success: function(e){ // this is the parsed Array of Objects var results = e.response.results; // For example, add a row of data ... as an object hash results.push({ your data hash here }); // this sets the 'data' to results ... myDataTable.set('data',results); } } }); The details of the DataTable.DataSource.load() command are at http://yuilibrary.com/yui/docs/api/files/datatable_js_datatable-datasource.js.html#l126, a little hard to find! Also, the "onDataReturnInitializeTable" is immediately below the load command. You are free to add / delete whatever rows you like in the success callback function, but you need to handle the myDataTable.set('data',your_data) yourself. As for "combining columns" as you said in your posting ... that could be a little more involved, because the DataTable is constructed assuming the columns you defined in the "columns" config, so hopefully you aren't planning to change that. Todd |
|
Todd, thanks for the response. That was exactly what I was looking for.
A couple more questions: If I'm setting the data into the table myself, will I still be able to take advantage of the caching capabilities of DataSource? At this point, I can just as easily use Y.io to get the data, transform it and set it into the DataTable. Is there really any advantage to using DataSource? |
|
Mark,
Clearly your modifications to the DS response results won't be available in the cache, although the original pristine response results should be accessible. You're correct that you don't necessarily need to use the DS, you can simply do your Y.io call and any parsing and/or other mods and feed them right into DT with set('data'). A real advantage is with DataSource you can hook-up any of several DataSchemas (JSON, XML, etc...) and can also handle conversions with DataSchema parsers on individual fields. On a related note, amongst the many changes in the recent 3.5 version of DataTable, you can now define a Y.ModelList that has it's own sync layer built-in, essentially freeing you from using DS or Y.io on your own. I recently posted a forum item regarding some work using a RESTful sync layer at http://yuilibrary.com/forum/viewtopic.php?t=10217. This ModelList sync approach is pretty cool IMHO, so that is potentially another alternative for you. Regards, Todd |
|
Is there a way to replace DataSource's cache for a request with my transformed variant? Or a way to intercept the cache lookup so I can repeat the transformation?
Thanks for the pointer to ModelList. I'll have to spend a bit more time looking into it. |
|
I don't really use the caching frequently. Looks like you can push your changed response back on the DS cache using the DataSourceCache.add method. You'd have to experiment with that if that's a requirement.
|
| Page 1 of 1 | [ 6 posts ] |
| 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 |
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info
Powered by phpBB® Forum Software © phpBB Group