⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

How to add Class to Table Rows, based on database field



amityweb
  • profile picture
  • Member

Posted 17 May 2012 - 11:36 AM

Sorry for long post but want to explain it all, my main question is right at the bottom if you want to skip it all!

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!

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 17 May 2012 - 19:17 PM

First of all I am sorry about this situation and I don't have any report so long with a similar issue so I don't think it is a bug. It sounds more to some database dependency problem. Do you use InnoDB or MyIsam for your database structure?
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.

amityweb
  • profile picture
  • Member

Posted 20 May 2012 - 08:23 AM

Thanks a lot for that... I have just removed Delete for now, but if they want to delete things I will add in the custom delete command as per the example.

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.

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 20 May 2012 - 11:23 AM

I am sorry but I don't have added this functionality to grocery CRUD yet. So the only way to do it is with JavaScript . So one way to do it WITHOUT changing the core of grocery CRUD and not even adding a JavaScript or a CSS to your template is the below code:

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