4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
15 * Shortcut class to create a JSON + {@link Roo.UpdateManager} template view. Usage:
17 var view = new Roo.JsonView({
18 container: "my-element",
19 tpl: '<div id="{id}">{foo} - {bar}</div>', // auto create template
24 // listen for node click?
25 view.on("click", function(vw, index, node, e){
26 alert('Node "' + node.id + '" at index: ' + index + " was clicked.");
29 // direct load of JSON data
30 view.load("foobar.php");
32 // Example from my blog list
33 var tpl = new Roo.Template(
34 '<div class="entry">' +
35 '<a class="entry-title" href="{link}">{title}</a>' +
36 "<h4>{date} by {author} | {comments} Comments</h4>{description}" +
37 "</div><hr />"
40 var moreView = new Roo.JsonView({
41 container : "entry-list",
45 moreView.on("beforerender", this.sortEntries, this);
47 url: "/blog/get-posts.php",
48 params: "allposts=true",
49 text: "Loading Blog Entries..."
53 * Note: old code is supported with arguments : (container, template, config)
57 * Create a new JsonView
59 * @param {Object} config The config object
62 Roo.JsonView = function(config, depreciated_tpl, depreciated_config){
65 Roo.JsonView.superclass.constructor.call(this, config, depreciated_tpl, depreciated_config);
67 var um = this.el.getUpdateManager();
69 um.on("update", this.onLoad, this);
70 um.on("failure", this.onLoadException, this);
74 * Fires before rendering of the downloaded JSON data.
75 * @param {Roo.JsonView} this
76 * @param {Object} data The JSON data loaded
80 * Fires when data is loaded.
81 * @param {Roo.JsonView} this
82 * @param {Object} data The JSON data loaded
83 * @param {Object} response The raw Connect response object
86 * @event loadexception
87 * Fires when loading fails.
88 * @param {Roo.JsonView} this
89 * @param {Object} response The raw Connect response object
92 'beforerender' : true,
94 'loadexception' : true
97 Roo.extend(Roo.JsonView, Roo.View, {
99 * @type {String} The root property in the loaded JSON object that contains the data
104 * Refreshes the view.
106 refresh : function(){
107 this.clearSelections();
110 var o = this.jsonData;
111 if(o && o.length > 0){
112 for(var i = 0, len = o.length; i < len; i++){
113 var data = this.prepareData(o[i], i, o);
114 html[html.length] = this.tpl.apply(data);
117 html.push(this.emptyText);
119 this.el.update(html.join(""));
120 this.nodes = this.el.dom.childNodes;
121 this.updateIndexes(0);
125 * Performs an async HTTP request, and loads the JSON from the response. If <i>params</i> are specified it uses POST, otherwise it uses GET.
126 * @param {Object/String/Function} url The URL for this request, or a function to call to get the URL, or a config object containing any of the following options:
130 params: {param1: "foo", param2: "bar"}, // or a URL encoded string
131 callback: yourFunction,
132 scope: yourObject, //(optional scope)
140 * The only required property is <i>url</i>. The optional properties <i>nocache</i>, <i>text</i> and <i>scripts</i>
141 * are respectively shorthand for <i>disableCaching</i>, <i>indicatorText</i>, and <i>loadScripts</i> and are used to set their associated property on this UpdateManager instance.
142 * @param {String/Object} params (optional) The parameters to pass, as either a URL encoded string "param1=1&param2=2" or an object {param1: 1, param2: 2}
143 * @param {Function} callback (optional) Callback when transaction is complete - called with signature (oElement, bSuccess)
144 * @param {Boolean} discardUrl (optional) By default when you execute an update the defaultUrl is changed to the last used URL. If true, it will not store the URL.
147 var um = this.el.getUpdateManager();
148 um.update.apply(um, arguments);
151 // note - render is a standard framework call...
152 // using it for the response is really flaky... - it's called by UpdateManager normally, except when called by the XComponent/addXtype.
153 render : function(el, response){
155 this.clearSelections();
159 if (response != '') {
160 o = Roo.util.JSON.decode(response.responseText);
163 o = o[this.jsonRoot];
169 * The current JSON data or null
177 * Get the number of records in the current JSON dataset
180 getCount : function(){
181 return this.jsonData ? this.jsonData.length : 0;
185 * Returns the JSON object for the specified node(s)
186 * @param {HTMLElement/Array} node The node or an array of nodes
187 * @return {Object/Array} If you pass in an array, you get an array back, otherwise
188 * you get the JSON object for the node
190 getNodeData : function(node){
191 if(node instanceof Array){
193 for(var i = 0, len = node.length; i < len; i++){
194 data.push(this.getNodeData(node[i]));
198 return this.jsonData[this.indexOf(node)] || null;
201 beforeRender : function(){
202 this.snapshot = this.jsonData;
204 this.sort.apply(this, this.sortInfo);
206 this.fireEvent("beforerender", this, this.jsonData);
209 onLoad : function(el, o){
210 this.fireEvent("load", this, this.jsonData, o);
213 onLoadException : function(el, o){
214 this.fireEvent("loadexception", this, o);
218 * Filter the data by a specific property.
219 * @param {String} property A property on your JSON objects
220 * @param {String/RegExp} value Either string that the property values
221 * should start with, or a RegExp to test against the property
223 filter : function(property, value){
226 var ss = this.snapshot;
227 if(typeof value == "string"){
228 var vlen = value.length;
233 value = value.toLowerCase();
234 for(var i = 0, len = ss.length; i < len; i++){
236 if(o[property].substr(0, vlen).toLowerCase() == value){
240 } else if(value.exec){ // regex?
241 for(var i = 0, len = ss.length; i < len; i++){
243 if(value.test(o[property])){
250 this.jsonData = data;
256 * Filter by a function. The passed function will be called with each
257 * object in the current dataset. If the function returns true the value is kept,
258 * otherwise it is filtered.
259 * @param {Function} fn
260 * @param {Object} scope (optional) The scope of the function (defaults to this JsonView)
262 filterBy : function(fn, scope){
265 var ss = this.snapshot;
266 for(var i = 0, len = ss.length; i < len; i++){
268 if(fn.call(scope || this, o)){
272 this.jsonData = data;
278 * Clears the current filter.
280 clearFilter : function(){
281 if(this.snapshot && this.jsonData != this.snapshot){
282 this.jsonData = this.snapshot;
289 * Sorts the data for this view and refreshes it.
290 * @param {String} property A property on your JSON objects to sort on
291 * @param {String} direction (optional) "desc" or "asc" (defaults to "asc")
292 * @param {Function} sortType (optional) A function to call to convert the data to a sortable value.
294 sort : function(property, dir, sortType){
295 this.sortInfo = Array.prototype.slice.call(arguments, 0);
298 var dsc = dir && dir.toLowerCase() == "desc";
299 var f = function(o1, o2){
300 var v1 = sortType ? sortType(o1[p]) : o1[p];
301 var v2 = sortType ? sortType(o2[p]) : o2[p];
304 return dsc ? +1 : -1;
306 return dsc ? -1 : +1;
311 this.jsonData.sort(f);
313 if(this.jsonData != this.snapshot){
314 this.snapshot.sort(f);