Jump to content


set_relation when used with change_field_type readonly displays ID rather than related value

set_relation change_field_type work around callback_edit_field

  • Please log in to reply
3 replies to this topic

#1 fieldju

fieldju

    Member

  • Members
  • PipPip
  • 14 posts

Posted 04 June 2012 - 08:01 AM

Summary:
When the set_relation function is used with the change_field_type function and the readonly type is selected the CRUD will display the id rather than the related value.

Example:
using the following tables
"students"
+------------------------------------------+
| student_id | studentName | enrollmentStatus |
| 0 | Justin | 1 |
| 1 | Billy | 0 |

+------------------------------------------+

"classGrades"
+-----------------------------------+
| class_id | grade | student_id |
| biology | A | 0 |
| english101 | B | 1 |
+-----------------------------------+

function viewGrades() {
$crud = new grocery_CRUD();
$crud->set_table('classGrades')
  ->set_relation('student_id','students','studentName')
  ->change_field_type('student_id','readonly');

$output = $crud->render();
  
$this->load->view('templates/grid', $output);
}

then the CRUD produced will show the studentID instead of the studentName

Workaround:
This issue can be worked around through the use of the callback_edit_field function as I did below
function viewGrades() {
$crud = new grocery_CRUD();
$crud->set_table('classGrades');

$crud->callback_edit_field('student_id', array($this, 'getNameFromStudentID'));

$output = $crud->render();
  
$this->load->view('templates/grid', $output);
}

with this callback function (please note that we used active record and your call back might be different depending on how you interact with your database and how your models are set up here is our model if you want to see it)

function getNameFromStudentID($value, $row){
$name = Student::find_by_studentid($value);
$nameAttr = $name->attributes();
$string = $nameAttr['firstname'] . ' ' . $nameAttr['lastname'];
return $string;
}

this will display the students name rather than the ID
we also included this in our css to vertically center the names

<style type="text/css">
.flexigrid div.form-div input[type=text], .flexigrid div.form-div input[type=password]{
padding: 0 5px !important;
}
#StudentID_input_box, #UserID_input_box{ margin-top: 10px !important; }
.required{
   color:	   #EE0000;
   font-weight: bold;
   font-size:   18px;
}
</style>

where it says StudentID_input_box must be [the name of your column]_input_box


Platforms:
Chrome, Firefox, and Internet Explorer

Software Versions:
Codeignighter: 2.1.0
AG_Auth: 2.0
Fancybox: 2.0.6
Grocery Crud: 1.2.3
php-activerecord: 2.0


How to reproduce:
1. Create a CRUD that uses the set_relation() function
2. Set the related column to read only via the change_field_type function.
3. Observe that the CRUD displays the ID rather than the related data

#2 twister88888

twister88888

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 10 July 2012 - 08:41 AM

Gracias por la aportaciĆ³n!!

#3 deib97

deib97

    Member

  • Members
  • PipPip
  • 18 posts

Posted 21 May 2013 - 04:07 AM

Thanks bro..



#4 deib97

deib97

    Member

  • Members
  • PipPip
  • 18 posts

Posted 21 May 2013 - 05:03 AM

any example.. ?







Also tagged with one or more of these keywords: set_relation, change_field_type, work around, callback_edit_field

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users