Sencha Touch ã® NestedList 㧠JSON ãã¼ã¿ãåçã«ãªã¹ã表示
以åãiUI ã jQTouch ã§ä½æãããã®ï¼iUIç㯠id:fits:20100715, jQTouchç㯠id:fits:20100731ï¼ã¨åæ§ã®ãµã³ãã«ã Sencha Touch ã§ä½æãã¦ã¿ã¾ãããï¼ãµã¼ãã¼å´ã®å¦çãåããã®ã使ç¨ï¼
Ext.NestedList ã使ã£ã¦å®è£ ãã¦ã¿ã¾ããããExt.NestedListï¼ã¨ãããã TreeStoreï¼ã¯é層çãªãã¼ã¿ãä¸æ¬åå¾ããã±ã¼ã¹ãæ³å®ãã¦ããããã§ãä»åå©ç¨ãããµã¼ãã¼å´ã®å¦çã®ããã«é層æ¯ã«å¥ã® URL ãããã¼ã¿ãåå¾ãããããªã±ã¼ã¹ï¼ã¹ãã¼ãåå¾ ã¨ãã¼ãã«åå¾ã§URLã¨ãã¼ã¿ã¹ãã¼ããç°ãªãï¼ã§ã®å®è£ ã¯ããªãè¦å´ãã¾ããã
ãã®ãããä»åã®ãããªã±ã¼ã¹ï¼é層æ¯ã«åå¾å URLããã¼ã¿ã®ã¹ãã¼ããç°ãªãï¼ã«ã¯ Ext.List 㨠CardLayout ã®çµã¿åããã§å®è£ ããã®ãç¾å®çãã¨æãã¾ãã
ãµã³ãã«ã½ã¼ã¹ã¯ http://github.com/fits/try_samples/tree/master/blog/20101005/
ã¡ãªã¿ã« Sencha Touch 0.96 ã§ã¯ãSafari 5.0.2 㧠NestedList ã®ãªã¹ãã¢ã¤ãã ãããã«ã¯ãªãã¯ããã¨é·ç§»å
ã®æ¬¡ç»é¢ã 2éã«ç¹ãã£ã¦ãã¾ãç¾è±¡ãçºçãã¦ã¾ãããã1.0 ã§ã¯ä¿®æ£ããã¦ãã¾ãã
ãªãã1.0 ã§ã¯ Firefox ã§è¡¨ç¤ºããã§ããªããªãã¾ãããï¼WebKitPoint ãæªå®ç¾©ã«ãªã模æ§ï¼
ç»é¢æ§æ
ç»é¢æ§æ㯠iUIã»jQTouch çã¨åºæ¬çã«åãã§ãã
(2) ãã¼ãã«ã®ãªã¹ã表示ç»é¢ï¼mysql é¸ææï¼
Ext.NestedList ã§ã®ãªã¹ã表示
ã¾ããJavaScript ã«ãã¼ãã³ã¼ãã£ã³ã°ããçµã¿è¾¼ã¿ãã¼ã¿ã使ã£ã¦ Ext.NestedList ã§ã®ãªã¹ã表示ãå®è£ ãã¦ã¿ã¾ãã
注ç®ç¹ã¯ä»¥ä¸ã®éãã
- TreeStore ã使ã£ã¦é層ãã¼ã¿ã NestedList ã«è¨å®
- Ajax ã§ãµã¼ãã¼ãããã¼ã¿åå¾ããªãã¦ã TreeStore ã« proxy ã®è¨å®ãå¿ è¦
- é層ãã¼ã¿ã®æ«ç«¯ï¼leaf: true ã®æå®ããããã¼ã¿ï¼ã¯è©³ç´°ç»é¢ã«é·ç§»å¯è½ï¼ãã ãã詳細ç»é¢ãä½æãã getDetailCard é¢æ°ãå®è£ ããå¿ è¦ããï¼
- getDetailCard é¢æ°ã®æ»ãå¤ã« recordNode ãè¨å®ï¼ãããç¡ãã¨ãããã¼ã¿ã¤ãã«ã¨æ»ããã¿ã³åãæ£å¸¸è¡¨ç¤ºãããªãã£ãï¼
以å試ããæã« NestedList ã§ã¯ Store ã使ããªãã£ãæ°ããã¾ãããä»ã§ã¯ TreeStore ã使ãããã«ãªã£ã¦ã¾ãã
ãªãããªã¹ãã¢ã¤ãã æ¯ã®ã¢ã¯ã»ãµãªè¡¨ç¤ºï¼å³ç«¯ã® " > " ã®ç»åï¼ã®ä»çµã¿ãç¨æããã¦ããªãã£ãã®ã§ãã¹ã¿ã¤ã«æå®ã§å¯¾å¿ãã¾ããã
ä»ã« getItemTextTpl ã使ãæãèãããã¾ãããçæãã HTML ã span è¦ç´ ã®ä¸ã«å
¥ãããã¦ãã¾ããããã¢ã¯ã»ãµãªè¡¨ç¤ºã®ç¨éã«ã¯åãã¦ããªãã¨æãã¾ãã
index_static.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Nested List Sample(static data)</title> <link rel="stylesheet" href="js/resources/css/sencha-touch.css" type="text/css" /> <style type="text/css"> /* ã»ã«ã®ã¢ã¯ã»ãµãª " > " ã表示ããããã®ã¹ã¿ã¤ã«å®ç¾© */ .x-list-item { background-image: url(img/chevron.png); background-position: right center; background-repeat: no-repeat; } </style> <script type="text/javascript" src="js/sencha-touch-debug.js"></script> <script type="text/javascript"> Ext.setup({ onReady: function() { //ç»é¢è¡¨ç¤ºç¨ã®é層ãã¼ã¿ var data = { items: [ {title: "cdcol", items: [ {title: "cds", leaf: true} ]}, {title: "information_schema", items: [ {title: "CHARACTER_SETS", leaf: true, table_name: "CHARACTER_SETS", table_type: "SYSTEM VIEW", engine: "MEMORY", create_time: "" }, ã»ã»ã» ]}, ã»ã»ã» ] }; //ã¢ãã«å®ç¾© Ext.regModel("ListItem", { fields: [ {name: "title", type: "string"}, {name: "table_name", type: "string"}, {name: "table_type", type: "string"}, {name: "engine", type: "string"}, {name: "create_time", type: "string"}, {name: "items", type: "array"} ] }); //å®éã« ajax ã®å¦çããããªãã¦ã //proxy è¨å®ï¼type ãå¿ é ï¼ãå¿ è¦ãªç¹ã«æ³¨æ var dbStore = new Ext.data.TreeStore({ //ã¢ãã«ã®æå® model: "ListItem", //ãã¼ã¿ã®æå® root: data, proxy: { type: "ajax", reader: { type: "tree", root: "items" } } }); var list = new Ext.NestedList({ fullscreen: true, title: "DB", displayField: "title", store: dbStore, //詳細ç»é¢ã®è¨å® getDetailCard: createDetail }); } }); //詳細ç»é¢ã®ä½æ function createDetail(record, parentRecord) { var rec = record.getRecord(); return new Ext.Panel({ //ãããã¼ã¿ã¤ãã«ã¨æ»ããã¿ã³åãæ£å¸¸è¡¨ç¤ºãããããã« //recordNode ã®è¨å®ãå¿ è¦ recordNode: record, items: [ { xtype: 'textfield', label: 'table_name', placeHolder: rec.get("table_name") }, { xtype: 'textfield', label: 'table_type', placeHolder: rec.get("table_type") }, { xtype: 'textfield', label: 'engine', placeHolder: rec.get("engine") }, { xtype: 'textfield', label: 'create_time', placeHolder: rec.get("create_time") } ] }); } </script> </head> <body> </body> </html>
Ext.NestedList ã§ã® JSON ãã¼ã¿åçãªã¹ã表示
åºæ¬çã«åè¿°ã®ãµã³ãã«ã¨åãå 容ã§ããã以ä¸ã®ç¹ãç°ãªã£ã¦ãã¾ãã
- TreeStore ã« url ãè¨å®ãããµã¼ãã¼ããã¹ãã¼ãï¼table_schemaï¼ä¸è¦§ãåå¾
- ã¹ãã¼ããã¯ãªãã¯ããéã«ãµã¼ãã¼ãã該å½ãããã¼ãã«ã®ä¸è¦§ãåå¾ããTreeStore ã«åçãã¼ã¿è¿½å ï¼ã³ã¡ã³ã㧠private æå®ã®ãã fillNode ã使ç¨ï¼
- ãã¼ãã«ä¸è¦§åå¾æã«åæåã® Ajax å¦çãè¡ããããã jQuery ã使ç¨
ãªããtable_schema ãç»é¢è¡¨ç¤ºé ç®ã«æå®ããããããã¼ãã«ã®ä¸è¦§åå¾æã«ã table_schema ãã¼ã«å¯¾ãã¦ãã¼ãã«åãè¨å®ããããã«ãã¦ãã¾ããï¼ãããã¼ã®ã¿ã¤ãã«ãæ»ããã¿ã³åã«ãå½±é¿ãããããé層æ¯ã®å¤æ´ã¯å°é£ï¼
ã¾ããTreeStore ã« fillNode ããéãå ã TreeStore ã«æå®ããã¢ãã«ï¼SchemaItemï¼ã¨ã¯ç°ãªãã¢ãã«ï¼TableItemï¼ã§ä½æããã¬ã³ã¼ãã追å ãã¦ãã¾ããããã®ç¹ã«é¢ãã¦ã¯ä¸å¿åé¡ãªãåä½ãã¾ãã
index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Nested List Sample</title> <link rel="stylesheet" href="js/resources/css/sencha-touch.css" type="text/css" /> <style type="text/css"> /* ã»ã«ã®ã¢ã¯ã»ãµãª " > " ã表示ããããã®ã¹ã¿ã¤ã«å®ç¾© */ .x-list-item { background-image: url(img/chevron.png); background-position: right center; background-repeat: no-repeat; } </style> <script type="text/javascript" src="js/jquery-1.4.4.js"></script> <script type="text/javascript" src="js/sencha-touch-debug.js"></script> <script type="text/javascript"> Ext.setup({ onReady: function() { Ext.regModel("SchemaItem", { fields: [ {name: "table_schema", type: "string"} ] }); Ext.regModel("TableItem", { fields: [ {name: "table_name", type: "string"}, {name: "table_type", type: "string"}, {name: "engine", type: "string"}, {name: "create_time", type: "string"} ] }); var dbStore = new Ext.data.TreeStore({ model: "SchemaItem", proxy: { type: "ajax", url: "databases", reader: { type: "tree" } } }); var list = new Ext.NestedList({ fullscreen: true, title: "DB", displayField: "table_schema", store: dbStore, //ãã¼ãã«è©³ç´°ç»é¢ã®è¨å® getDetailCard: createDetail }); //ã¢ã¤ãã é¸ææã®å¦ç list.on("itemtap", function(subList, subIdx, el, e) { var rec = subList.getStore().getAt(subIdx); var schema = rec.get("table_schema"); //table_schema 表示ç»é¢ã®æã ããã¼ãã«ä¸è¦§ã®åå¾å¦çãå®è¡ if (schema) { var req = new jQuery.ajax({ method: "get", url: "tables/" + rec.get("table_schema"), async: false }); var jsonObj = eval("(" + req.responseText + ")"); var records = []; var Model = Ext.ModelMgr.getModel("TableItem"); Ext.each(jsonObj, function(item, index, allItems) { item["leaf"] = true; //ãããã¼ã¿ã¤ãã«ã¨ãªã¹ã表示ç¨ã«è¨å® item["table_schema"] = item.table_name; var record = new Model(item, item.table_name); record.raw = item; records.push(record); }); //TreeStore ã¸ã®ãã¼ã¿è¿½å dbStore.fillNode(rec.node, records); } }); } }); //詳細ç»é¢ã®ä½æ function createDetail(record, parentRecord) { ã»ã»ã» index_static.html ã¨åãã»ã»ã» } </script> </head> <body> </body> </html>