2010-11-09 4 views
0

Je rencontre un problème étrange. Apparemment, je voudrais supprimer les lignes de mon choix dans flexigrid. Cependant, lorsque je sélectionne les lignes et que je clique sur le bouton "Supprimer", je ne vois que le nombre d'éléments que j'ai effectivement sélectionnés mais, pour une raison quelconque, il ne transmet pas les identifiants des lignes. Dans le code JS lorsque j'effectue un test, j'ai remarqué que les éléments [i] .id semblent être indéfinis. Peut-être que quelqu'un pourrait me dire ce que je fais mal ici. Ce à quoi ressemble ma mise en œuvre comme:Problème avec la suppression des lignes sélectionnées dans CI flexigrid

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

Contrôleur

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

Voir

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

Répondre

1

J'ai découvert la raison de mon problème. Je vais le poster ici au cas où quelqu'un d'autre aura un problème similaire. Dans la bibliothèque Flexigrid il y a une ligne spécifique:

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

Je ne comprends pas pourquoi cette ligne a été commenté. J'ai enlevé le commentaire et ça fonctionne comme le charme en ce moment.

0

Dans les options de Flexigrid:

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

La fonction doCommand obtenir les lignes sélectionnées et par quel champ est trié:

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

     }); 
    } 
} 
Questions connexes