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.

Link Action plugin for slGrid

There was recently a plugin system added to slGrid, which is great because I recently found the need for a plugin that creates a link around specific cell content and decided to write one.

Link Action allows you to create a URL around the contents of a cell using the unique database column ID. It also allows you to pass the target page for the URL and optional URL arguments. If no target page is passed, it defaults to the current page calling the grid.

<?php
/*
 * Example usage:
 * $_SESSION["grid"]->SetPlugin("Full_Name", "link_action", array("target" => "users.php", "extra_args" => "&action=edit"));
 *
 * Assuming the database column ID is 24, and the Full_Name is Raam Dev, here is what the cell would contain:
 * Raam Dev
 */

require_once('class.plugin.php');

class link_action_Plugin extends Grid_Plugin
{

	function introspect()
	{
		return(array(
			"name" 			=> "Link Action",
			"description" 	=> "Creates a URL to call an action using the unique database column ID",
			"author"		=> "Raam Dev ",
			"version"		=> "1.0"
			));
	}

	function generateContent($cell, $args)
	{
		if(empty($args["target"])){
			$args["target"] = $PHP_SELF;
		}
		if(empty($args["extra_args"])){
			$args["extra_args"] = "";
		}
		return("$cell");
	}
}
?>

slGrid, Ajax-Based PHP Grid Component

I found an absolutely awesome application called slGrid. It was recently bought by Senza Limiti, a technology consulting company based in Slovakia. They've since made slGrid an open-source application, which is very cool. It uses AJAX and PHP to provide an interface for creating grids by loading content directly from a database. You specify the table and fields, and it takes care of the rest. Check out the slGrid demo to get a better idea of what it does.

Using slGrid is literally as easy as this:


<?php
	$_SESSION["grid"]->SetDatabaseConnection("database_name", "user_name", "password");
	$_SESSION["grid"]->SetSqlSelect('field1, field2', 'table');
	$_SESSION["grid"]->SetUniqueDatabaseColumn("id_field", false);
	$_SESSION["grid"]->SetTitleName("slGrid");

	$_SESSION["grid"]->PrintGrid(MODE_VIEW);
?>