Problem with removing selected rows in CI flexigrid
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)开发者_如何学C;
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>
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.
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
// ...
});
}
}
精彩评论