0

我使用 extjs 和 codeigniter 创建了一个 crud,但它只能读取、更新和删除数据。它无法创建新记录我的 ext 模型:

Ext.define('Mon_hoc', {
    extend: 'Ext.data.Model',
    idProperty: 'ma_mon',
    fields: [
       {name: 'ma_mon', type: 'int'},
       {name: 'ten_mon'},
       {name: 'so_tin_chi', type: 'int'},
       {name: 'so_tiet_lt', type: 'int'},
       {name: 'so_tiet_th', type: 'int'}
    ]        
});

我的商店:

var store = Ext.create('Ext.data.Store', {
    model: 'Mon_hoc',
    remoteSort: true,
    pageSize: 10,
    autoLoad: true,
    autoSync: true,
    proxy: {
        type: 'ajax',
        api: {
            read: 'http://localhost/QLPTH/index.php/data/read_mon_hoc',
            create: 'http://localhost/QLPTH/index.php/data/create_mon_hoc',
            update: 'http://localhost/QLPTH/index.php/data/update_mon_hoc',
            destroy: 'http://localhost/QLPTH/index.php/data/delete_mon_hoc'
        },
        reader: {
            type: 'json'
        },
        writer: {
            type: 'json',
            writeAllFields: true
        },
        listeners: {
            exception: function(proxy, response, operation){
                Ext.MessageBox.show({
                title: 'REMOTE EXCEPTION',
                msg: operation.getError(),
                icon: Ext.MessageBox.ERROR,
                buttons: Ext.Msg.OK
                });
            }
        }
    }
});

我的网格:

var grid = Ext.create('Ext.grid.Panel', {
    title:'Danh sách môn học',
    store: store,
    columns: [{
        id: 'ma_mon',
        header: 'Mã môn',
        dataIndex: 'ma_mon',
        flex: 1,
        editor: {
            xtype: 'numberfield',
            allowBlank: false,
            minValue: 100,
            maxValue: 300
        }
    }, {
        header: 'Tên môn',
        dataIndex: 'ten_mon',

        editor: {
            xtype: 'textfield',
            allowBlank: false

        }
    }, {

        header: 'Số tín chỉ',
        dataIndex: 'so_tin_chi',

        editor: {
            xtype: 'numberfield',
            allowBlank: false,
            minValue: 1,
            maxValue: 10
        }
    }, {

        header: 'Số tiết lý thuyết',
        dataIndex: 'so_tiet_lt',


        editor: {
            xtype: 'numberfield',
            allowBlank: false,
            minValue: 1,
            maxValue: 100
        }
    }, {

        header: 'Số tiết thực hành',
        dataIndex: 'so_tiet_th',

        editor: {
            xtype: 'numberfield',
            allowBlank: false,
            minValue: 1,
            maxValue: 100
        }
    }],
    stripeRows: true,
    height:320,
    width:600,
    frame:true,
    //resizable: {
    //   handles: 's',
    //   minHeight: 100
    //},
    bbar: Ext.create('Ext.PagingToolbar', {
        pageSize: 10,
        store: store,
        displayInfo: true,
        plugins: Ext.create('Ext.ux.ProgressBarPager', {})
    }),
    tbar: [{
        text: 'Thêm môn',

        handler : function() {
            rowEditing.cancelEdit();

            // Create a model instance
            var r = Ext.create('Mon_hoc', {
                ma_mon: 100,
                ten_mon: 'Tên môn',
                so_tin_chi: 1,
                so_tiet_lt: 30,
                so_tiet_th: 30
            });

            store.insert(0, r);
            rowEditing.startEdit(0, 0);
        }
    }, {
        itemId: 'removeMon',
        text: 'Xóa môn',

        handler: function() {
            var sm = grid.getSelectionModel();
            rowEditing.cancelEdit();
            store.remove(sm.getSelection());
            if (store.getCount() > 0) {
                sm.select(0);
            }
        },
        disabled: true
    }],
    plugins: [rowEditing],
    listeners: {
        'selectionchange': function(view, records) {
            grid.down('#removeMon').setDisabled(!records.length);
        }
    }
});

我的数据库结构: ma_mon: int(3), ten_mon: text, so_tin_chi: int(11), so_tiet_lt: int(11), so_tiet_th: int(11)

数据.php:

class Data extends CI_Controller {
function __construct(){
    parent::__construct();

}

function read_mon_hoc(){
    $data = array();
    $this->load->model('data_model');
    if($query = $this->data_model->get_mon_hoc_records())
    {
        $data = $query;
    }
    echo json_encode($data);        
}

function create_mon_hoc(){
    $decode = json_decode($GLOBALS["HTTP_RAW_POST_DATA"], true);
    if (isset($decode["ma_mon"])){
        $data = array(
            'ma_mon' => $decode['ma_mon'],
            'ten_mon' => $decode['ten_mon'],
            'so_tin_chi' => $decode['so_tin_chi'],
            'so_tiet_lt' => $decode['so_tiet_lt'],
            'so_tiet_th' => $decode['so_tiet_th']
        );
        $this->load->model('data_model');
        $this->data_model->add_mon_hoc_record($data);
    }
}

function delete_mon_hoc(){
    $decode = json_decode($GLOBALS["HTTP_RAW_POST_DATA"], true);
    if (isset($decode["ma_mon"])){
        if (is_numeric($decode["ma_mon"]) && $decode["ma_mon"]>0){
            $this->load->model('data_model');
            $this->data_model->delete_mon_hoc_record($decode["ma_mon"]);
        }
    }
}

function update_mon_hoc(){
    $decode = json_decode($GLOBALS["HTTP_RAW_POST_DATA"], true); 
    if (isset($decode["ma_mon"])){
        if (is_numeric($decode["ma_mon"]) && $decode["ma_mon"]>0){
            $data = array(
                'ma_mon' => $decode['ma_mon'],
                'ten_mon' => $decode['ten_mon'],
                'so_tin_chi' => $decode['so_tin_chi'],
                'so_tiet_lt' => $decode['so_tiet_lt'],
                'so_tiet_th' => $decode['so_tiet_th']
            );
            $this->load->model('data_model');
            $this->data_model->update_mon_hoc_record($data);
        }
    }
}
}

数据模型.php:

class Data_model extends CI_Model {

function get_mon_hoc_records()
{
    $query = $this->db->get('mon_hoc');
    return $query->result();
}

function add_mon_hoc_record($data) 
{
    $this->db->insert('mon_hoc', $data);
    return;
}

function update_mon_hoc_record($data) 
{
    $this->db->where('ma_mon', $data['ma_mon']);
    $this->db->update('mon_hoc', $data);
}

function delete_mon_hoc_record($data)
{
    $this->db->where('ma_mon', $data);
    $this->db->delete('mon_hoc');
}

}
4

0 回答 0