slGrid: Edit Mode without Add or Delete
I’ve been customizing slGrid for an application I’m developing at work. One of the things I needed to do was to enable the MODE_EDIT but at the same time prevent additions or deletions.
To prevent deletions, there is an option in /classes/gridclass.php called $editmode_delete, which you can simply set to false to prevent deletions and remove the ‘Delete’ column. However, this creates another problem: While editing a row, the ‘Delete’ button and the ‘Cancel’ button share the same column:

So by disabling deletions, we’re giving up our ‘Cancel’ button. This means the only way to get out of edit mode after clicking the ‘Edit’ button, is to refresh the page. OK, so thats not the end of the world. Lets continue.
Our next problem is insertions — we need to prevent users from adding new rows. I saw the $editmode_add option in /classes/gridclass.php, and assumed it would be as simple as changing it to false. But to my surprise, that only removed the ‘Add’ button, leaving the entire (empty) insertion row at the top of the table:

Well that doesn’t make any sense. If there is an option to disable insertions, why leave the unused empty row? After lots of digging I finally found the block of code that needs to be commented/removed to prevent the empty row from loading:
gridclass.php:
if ($this->mode == MODE_EDIT)
{
$insert_row = array();
$row_index = -1;
foreach ($this->columns as $column)
$insert_row[$column->name] = "";
$this->CreateRow($insert_row, $row_index, $visible_row_index, $table_main);
$row_index++;
}
After commenting out that block of code, I finally have what I want; slGrid in Edit Mode without the ability to Add or Delete:

I’m going to be working with slGrid a lot now and I’ll be tweaking/customizing it quite a bit. I will be sure to share everything I learn here on my blog for others who may wish to use it.

6 Comments, Comment or Ping
Johny
Hi RamDev,
Thanks for providing this valuable information.
Johny
Nov 23rd, 2007
Raam
You’re welcome Johny!
I’m happy you found the tutorial useful!
Nov 23rd, 2007
Alex
If you change this
if ($this->mode == MODE_EDIT)
for this
if (($this->mode == MODE_EDIT)&& ($this->editmode_add))
you don’t need to comment everything!!
Jan 30th, 2008
Raam
Ah, of course! I don’t know why I didn’t think of that.
Thanks Alex!
Jan 30th, 2008
fetuline
…or you read the Doku:
SetEditModeAdd(boolean is_enabled)
Use this function on MODE_EDIT. You can decide if it should be possible to add rows. Sometimes you want to limit this possibility.
SetEditModeEdit(boolean is_enabled)
Use this function on MODE_EDIT. You can decide if it should be possible to edit a row.
SetEditModeDelete(boolean is_enabled)
Use this on MODE_EDIT. You can decide if it should be possible to delete rows.
Mar 28th, 2008
Raam
Hello Fetuline,
I’m already aware of those options, however the problem is that they don’t functionally change the grid in a way that makes it usable in Edit mode without Add or Delete.
Mar 28th, 2008
Reply to “slGrid: Edit Mode without Add or Delete”