How to add Class to Table Rows, based on database field
- Single Page
Posted 17 May 2012 - 11:36 AM
A customer is reporting that data they have added has magically disappeared. They assigned hundreds of participants to an event they created, then a few weeks later the assignment has gone. I am very confused by this. If there is no bug or something in GroceryCRUD that could do this, then the only thing I can think of is someone deleted the event and recreated a new one, or deleted participants... I dont know.
So what I want to do is rule out the delete action. I disabled Delete on all my data. The customer cannot delete anything now.
I am going to add a custom action called disable.. ( I will tell the customer it is Delete). What this will do is change a field in the database called "deleted" to 1 (default 0). Basically the record will be updated to mark it as delete it, instead of deleting it.
In my output I will insert a WHERE clause to only show data that is not "deleted"
As an Admin user I want to see the deleted data but that it is marked as deleted. So I wanted to add a class to the row if the user is deleted, and then in the stylesheet I can make it greyed out or something. So I can see all data but separate deleted users.
[b]So to my main question....[/b]
Can I add a class to the table output depending on the database data, without modifying core?
Thanks a lot!
Posted 17 May 2012 - 19:17 PM
Also did you change the suhosin settings that I told you at: http://www.grocerycr...indpost__p__780 , perhaps it has something to do with the limitations of your server? I don't know, it's really weird!
OK back to the question now... actually I have a better solution for your situation.
You don't have to tell anything to your customer, you can just change the default functionality of the delete without changing the CORE. You can simply use the callback_delete. I also have an example at: http://www.grocerycr...callback_delete that does exactly what are you looking for. I think it will make it more complicated to tell to your customer about the disable thing and have a different action for this.
Posted 20 May 2012 - 08:23 AM
But do you know is it possible to add a class to rows in the table? Because with the custom delete command (which disables) for my Super Admin users I want to show the "deleted" entries but grey them out, so they know they are deleted. Super Admins can then undelete easily without accessing the database.
Posted 20 May 2012 - 11:23 AM
[b]Note: [/b]I know this solution may be a bit complicated but I tested and it works fine for me.
/* Add this code BEFORE render */
$crud->callback_column('deleted',function($value){
return $value == '1' || $value == 'active' ? '<span class="disabled-status">'.$value.'</span>' : $value;
});
$extra_javascript = "<script type='text/javascript'> $(function(){ $('span.disabled-status').closest('tr').addClass('disabled-row');});</script>";
$crud->getStateInfo();
if($crud->getState() == 'ajax_list')
{
echo $extra_javascript;
}
/* --------------------------- */
$output = $crud->render();
/* Add this code AFTER render */
if($crud->getState() == 'list')
{
$output->output .= $extra_javascript.
"<style type='text/css'>
tr.disabled-row{
color: #999 !important;
}
</style>";
}
/* --------------------------- */
Also make sure that you have the column "deleted" at the method columns for example:
$crud->columns('customerName','contactLastName','phone','deleted');
Just tell me know if this will work for you.
[b]Edit:[/b] I forgot to tell you that:
$crud->callback_column('deleted',function($value){
return $value == '1' || $value == 'active' ? '<span class="disabled-status">'.$value.'</span>' : $value;
});
works fine with PHP 5.2.3 or later, for PHP version lower that 5.2.3 (for example 5.2.11) you have to change this with:
$crud->callback_column('deleted',array($this,'change_deleted_span'));
and add this to your controller:
function change_deleted_span($value)
{
return $value == '1' ? '<span class="disabled-status">'.$value.'</span>' : $value;
}