ExtJS是一個強大的JavaScript框架,可以幫助開發人員創建功能強大而且易于使用的Web應用程序。其中一個重要的功能就是處理復雜的JSON對象。下面是一些如何使用ExtJS處理復雜JSON對象的示例:
var myData = { "users": [ { "name": "John", "age": 25, "email": "john@example.com", "address": { "street": "123 Main St", "city": "New York", "state": "NY", "zip": "11111" } }, { "name": "Jane", "age": 30, "email": "jane@example.com", "address": { "street": "456 Oak Ave", "city": "Los Angeles", "state": "CA", "zip": "22222" } } ] }; //使用ExtJS的數據模型(DatatModel)創建store var store = new Ext.data.Store({ model: 'UserModel', data: myData.users }); //創建Grid面板,展示store數據 var grid = new Ext.grid.GridPanel({ renderTo: "myGrid", store: store, columns: [ {header: "Name", dataIndex: "name"}, {header: "Age", dataIndex: "age"}, {header: "Email", dataIndex: "email"}, {header: "Street", dataIndex: "address.street"}, {header: "City", dataIndex: "address.city"}, {header: "State", dataIndex: "address.state"}, {header: "Zip", dataIndex: "address.zip"} ] }); //數據模型定義 Ext.define('UserModel', { extend: 'Ext.data.Model', fields: [ {name: 'name', type: 'string'}, {name: 'age', type: 'int'}, {name: 'email', type: 'string'}, {name: 'address', mapping: 'address.street'}, {name: 'address', mapping: 'address.city'}, {name: 'address', mapping: 'address.state'}, {name: 'address', mapping: 'address.zip'} ] });
以上代碼展示了如何使用ExtJS處理一個復雜的JSON對象并展示在Grid面板中。其中數據模型UserModel中定義了數據類型和數據的映射關系。在創建store時,使用數據模型作為參數,將原始JSON對象中的users數據傳遞給store。Grid面板的列使用字段名稱或者字段映射來展示數據。
上一篇mysql句柄千萬