删除 CI Flexigrid 中选定行的问题

发布于 2024-10-01 03:31:22 字数 4085 浏览 4 评论 0原文

我遇到了一些奇怪的问题。显然,我想删除我在 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>

I'm encountering some weird problem. Apparently, I would like to delete the rows of my choice in flexigrid. However, when I'm selecting the rows and click 'Delete' button I only see how many items I actually selected but, for some reason, it's not passing the rows ids. In JS code when I'm running some test I noticed that items[i].id appears to be undefined. Perhaps someone could tell me what I'm doing wrong here. This is how my implementation looks like:

JS code

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;
            } 
        }   

Controller

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));
}

View

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪花飘飘的天空 2024-10-08 03:31:22

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

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

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

I've discovered the reason of my problem. I will post it here in case someone else will have similar problem. In the flexigrid library there is a specific line:

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

I don't understand why this line was commented out. I removed the comment and it works like charm right now.

小矜持 2024-10-08 03:31:22

在 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
            // ...

        });
     }
}

In the flexigrid options:

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

The function doCommand get the selected rows and by which field is sorted:

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
            // ...

        });
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文