在CI flexigrid中删除选定行的问题

时间:2010-11-09 09:13:52

标签: php javascript codeigniter flexigrid

我遇到了一些奇怪的问题。显然,我想在flexigrid中删除我选择的行。但是,当我选择行并单击“删除”按钮时,我只看到实际选择了多少项,但由于某种原因,它没有传递行ID。在JS代码中,当我运行一些测试时,我注意到items [i] .id似乎未定义。也许有人可以告诉我这里我做错了什么。这就是我的实现方式:

JS代码

function test(com,grid)
{
    if (com=='Select All')
    {
        $('.bDiv tbody tr',grid).addClass('trSelected');
    }

    if (com=='DeSelect All')
    {
        $('.bDiv tbody tr',grid).removeClass('trSelected');
    }

    if (com=='Delete')
    {
           if($('.trSelected',grid).length>0){
               if(confirm('Delete ' + $('.trSelected',grid).length + ' items?'))      {
                    var items = $('.trSelected',grid);
                    var itemlist ='';
                    for(i=0;i<items.length;i++){
                        itemlist+= items[i].id+",";
                    }
                    $.ajax({
                       type: "POST",
                       url: "<?=site_url("/ajax/deletec");?>",
                       data: "items="+itemlist,
                       success: function(data){
                        $('#flex1').flexReload();
                        alert(data);
                       }
                    });
                }
            } else {
                return false;
            } 
        }   

控制器

function index()
{

    $colModel['id'] = array('id',40,TRUE,'left',1);
    $colModel['admins.name'] = array('Name',180,TRUE,'left',0);
    $colModel['admins.email'] = array('Email',180,TRUE,'left',1);
    $colModel['admins.password'] = array('Password',180,TRUE,'left',0);
    $colModel['edit'] = array('Edit',30,TRUE,'left',1);

    /* Aditional Parameters */
    $gridParams = array(
    'width' => 'auto',
    'height' => 400,
    'rp' => 15,
    'rpOptions' => '[10,15,20,25,40]',
    'pagestat' => 'Displaying: {from} to {to} of {total} items.',
    'blockOpacity' => 0.5,
    'title' => 'Admins of Fact10best.com',
    'showTableToggleBtn' => true
    );

    /* Buttons, which will appear above this list */
    $buttons[] = array('Delete','delete','test');
    $buttons[] = array('separator');
    $buttons[] = array('Select All','add','test');
    $buttons[] = array('DeSelect All','delete','test');
    $buttons[] = array('separator');
    $buttons[] = array('Add Admin','add','test');

    //Build js
    //View helpers/flexigrid_helper.php for more information about the params on this function
    $grid_js = build_grid_js('flex1',site_url("admin/admin_control/list_admins"),$colModel,'admins.name','asc',$gridParams,$buttons);

    $data['js_grid'] = $grid_js;
    $data['list_details'] = null;
    $data['users_fact'] = null;

    $this->load->view('admin_panel/admin_content', $data);

}

/* List displayer for index() */    
//---------------------------------------------------------------------------------------------------------------
public function list_admins()
{

    $valid_fields = array('id','admins.name','admins.email', 'admins.password');

    $this->flexigrid->validate_post('admins.name','asc',$valid_fields);

    $records = $this->admin_login_model->list_flexigrid_admins();

    $record_items = array();

    foreach ($records['records']->result() as $row)
    {
        $record_items[] = 
        array (
                $row->id,
                $row->name,                 
                $row->email,
                $row->password,
                '<a href='.site_url("admin_login/edit_admin/".$row->id).'>
                    <img src="http://www.fact10best.com/system/application/views/images/magnify.png" style="border:none;">
                </a>',
              );
    }

    //Print please
    $this->output->set_header($this->config->item('json_header'));
    $this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
}

查看

<table id="flex1" style="display:none"></table>

2 个答案:

答案 0 :(得分:1)

我发现了问题的原因。我会在这里发布,以防其他人有类似的问题。在flexigrid库中有一个特定的行:

$obj->id = array_shift($row); //Remove first element, and save it. Its the ID of the row.

我不明白为什么这条线被注释掉了。我删除了评论,现在就像魅力一样。

答案 1 :(得分:0)

在flexigrid选项中:

buttons : [
    {name: 'Add', bclass: 'add', onpress : doCommand},
    {name: 'Edit', bclass: 'edit', onpress : doCommand},
    {name: 'Delete', bclass: 'delete', onpress : doCommand},
    {separator: true}
],

函数doCommand获取所选行以及对哪个字段进行排序:

function doCommand(com, grid) {
    if (com == 'Delete') {      
        $('.trSelected', grid).each(function() {            
            var id = $(this).index();
            var field = $(this).find('.sorted').attr('abbr');
            var content = $(this).find('.sorted').text();
            alert("Edit row: "+id+' sorted by: '+field+'. Content='+content);

            // Now you can make your ajax call using 
            // whatever you need of these 3 variables
            // ...

        });
     }
}