asysbang

 找回密码
 立即注册
查看: 5194|回复: 0
打印 上一主题 下一主题

EXTJS-CI-MYSQL数据交互

[复制链接]

510

主题

2

好友

6311

积分

管理员

Rank: 80Rank: 80Rank: 80Rank: 80Rank: 80

最佳新人 活跃会员 热心会员 推广达人 宣传达人 灌水之王 突出贡献 优秀版主 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2015-1-23 16:02:01 |只看该作者 |倒序浏览
mysql创建表article_type
=======数据库=======
CREATE TABLE `article_type` (
  `_id` int(11) NOT NULL auto_increment,
  `_name` text,
  PRIMARY KEY  (`_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of article_type
-- ----------------------------
INSERT INTO article_type VALUES ('1', '生活');
INSERT INTO article_type VALUES ('2', '科技');

=======CI============
model:  article_type.php
<?php
class article_type extends CI_Model{
        function article_type(){
                parent::__construct();
        }
        function getAllArticleType(){
                $this->db->select('_id,_name');
                $query = $this->db->get('article_type');
                $data=array();
                $rearr=array();
                if($query->num_rows()>0){
                        foreach($query->result() as $row){
                                $data['id']=$row->_id;
                                $data['name']=$row->_name;
                                array_push($rearr,$data);
                        }
                }
                return $rearr;
        }
}
?>
controller:article.php
<?php
class article extends CI_Controller{
        function article(){
                parent::__construct();
                $this->load->helper("url");
        }
        function data_article_type() {
                $this->load->model('article_type');
                $data = $this->article_type->getAllArticleType();
                echo json_encode($data);  
        }
}
?>
到这步 访问   http://10.6.0.117/index.php/article/data_article_type  
能够得到ci框架返回的json字符串: [{"id":"1","name":"\u751f\u6d3b"},{"id":"2","name":"\u79d1\u6280"}]

========EXTJS==============
<script type="text/javascript">
            Ext.onReady(function(){
                    Ext.define('User', {
                        extend: 'Ext.data.Model',
                        fields: ['id', 'name']
                    });

                    var mstore = Ext.create('Ext.data.Store', {
                        model: 'User',
                        proxy: {
                            type: 'ajax',
                            url : 'http://10.6.0.117/index.php/article/data_article_type',
                            reader: {
                                type: 'json'
                            }
                        }
                    });
                    mstore.load();

                        Ext.create('Ext.grid.Panel', {
                            store: mstore,
                            columns: [
                                { text: 'id',  dataIndex: 'id' },
                                { text: 'name', dataIndex: 'name'}
                            ],
                            height: 200,
                            renderTo: Ext.getBody()
                        });
            });
        </script>
到此,读取数据库显示基本ok了,接下来做写入数据库的工作
==========这个是treestore和数据库关联================
[{text:'aaaa',leaf:true,iconCls:'settings'},{text:'bbbb',leaf:true,iconCls:'settings'}]

            var treestore = Ext.create('Ext.data.TreeStore', {  
                    defaultRootText:"全部分类",
            root: {  
                expanded: true  
            },  
            proxy: {  
                type: 'ajax',  
                url: 'http://10.6.0.117/index.php/article/test_tree_loader'  
            }  
            });
           
            this.typeView = Ext.create('Ext.tree.TreePanel', {
                id: "typeList",
                    title: '文章分类',
                store: treestore,
                useArrows: true,
                tools: [{type: 'plus',handler: this.onAddClick}],
                rootVisible: true
            });

===============================





回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

Archiver|手机版|aSys-帮 ( 京ICP备13033689号 )

GMT+8, 2024-7-2 02:35 , Processed in 0.042727 second(s), 20 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部