⚠ 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 hide fields in the view area (clicking view button) / setting default values in fields



larasmith
  • profile picture
  • Member

Posted 25 July 2014 - 02:26 AM

Using this library: custom_grocery_crud.php which is found at: /topic/61-default-field-values-for-add-form/, I was able to set default values on any field. However, I observed that my foreign keys turns into IDs when i view it using the view button on the action column. On the grid area, it was able to display the needed fields via set_relation. Was there any workaround for this? I was thinking of hiding those fields on the view area because they are showing the ID's instead of the needed values since they are working on the datagrid view. Here's the picture of the said situation: c22034b714ebc8792f77ae67e5615a46.pngBy the way I'm a newbie here and I did my best to find the solution before asking so please help me... Thank you very much in advance.  :)

 


Amit Shah
  • profile picture
  • Member

Posted 25 July 2014 - 16:18 PM

well.. heres a post that will allow you to do a callback on read fields

/topic/2525-callback-addedit-field-changes-the-display-on-the-read-method/

 

as for removing  / hiding fields in read state - it can be set using unset_read_fields

or setting up what fields u wana allow to be seen up while in read state

 

If you get stuck up anywhere .. feel free to contact back :)

 

Happy GCing :)


larasmith
  • profile picture
  • Member

Posted 29 July 2014 - 04:32 AM

well.. heres a post that will allow you to do a callback on read fields

/topic/2525-callback-addedit-field-changes-the-display-on-the-read-method/

 

as for removing  / hiding fields in read state - it can be set using unset_read_fields

or setting up what fields u wana allow to be seen up while in read state

 

If you get stuck up anywhere .. feel free to contact back :)

 

Happy GCing :)

 

Thank you for the reply  :)  i was trying to do your recommendation in that post but got lost so i tried other method. The qb_encoder_id was not visible in the read() view though but its ok because it can be seen on the grid. So this is my solution:

 

1.) I created a callback

	function qb_log_encoder($post_array,$primary_key)
	{
	    $log_encoder = array(
	        "qb_id" => $primary_key,
	        "qb_encoder_id" => $this->session->userdata('username_id'), // sets encoder
	        "qb_published_id" => '0', // sets Published = No
	        "qb_PilotTest_id" => '0'  // sets Pilot Tested = No
	    );	 
	    $this->db->update('tblqb',$log_encoder,array('qb_id' => $primary_key));	 
	    return true;
	}

2.) Excluded the qb_encoder_id field in the $crud->fields() to remove it from the add/edit mode.

3.) Execute the call back using this: 

$crud->callback_after_insert(array($this, 'qb_log_encoder'));

This enables the setting of the default value of qb_encoder_id when the user adds a record.

4.) I created a code that executes when the user edits a record by using state:

     	$state = $crud->getState();
    	$state_info = $crud->getStateInfo();
    	
		if($state == 'edit')
	    {
	        //This updates the encoder of the question upon edit mode.
	        $primary_key = $state_info->primary_key;
	        $data = array(
			   'qb_encoder_id' => $username_id,   
			);
			$this->db->where('qb_id',$primary_key);
 			$this->db->update('tblqb',$data);
	    }

With this, I can assign the new encoder name once other users modifies the record.

 

I hope this helps other people with similar problem  :rolleyes:

 

Off this topic... i would like to ask you if it is possible to hide a visible field in add or edit mode upon selection of a value in a dropdown list? if it is... can you show a working example? I cannot figure out how to do it. The situation that I'm gonna use is: I'm creating a question bank. I have question types such as Multiple Choice, Fill in the Blanks, Essay etc. By default a Multiple Choice type of question has 4 options and the correct answer. But other types such as Fill in the Blanks have no option... they only have the question and the correct answer. I wanted to hide the 4 options during add or edit mode and set a value = "n/a" when the user selects a question type that is NOT Multiple Choice. Thank you in advance! God bless!


Amit Shah
  • profile picture
  • Member

Posted 29 July 2014 - 07:55 AM

Good you learning 2 discover solutions .. thats what is much better!!

 

well that is very much possible ..

you have to use javascript for the same

create a javascript file.. (shading u the exmple script i use) ...   a part of my script.. u wil understand as how this works out

//hide the fields by default
    $("#expires_field_box").hide();
    $("#max_views_field_box").hide();
    $("#max_redumptions_field_box").hide();
    $("#always_display_field_box").hide();

    $("#field-default_coupon-true").click(function() {
        $("#always_display_field_box").show();
        //$("#start_field_box").hide();
        $("#expiry_condition_field_box").hide();
    });

    $("#field-default_coupon-false").click(function() {
        $("#always_display_field_box").hide();
        //$("#start_field_box").show();
        $("#expiry_condition_field_box").show();
    });

    $("#field-expiry_condition").change(function() {
        if ($("#field-expiry_condition").val() == '') {
            $("#expires_field_box").hide();
            $("#max_views_field_box").hide();
            $("#max_redumptions_field_box").hide();
        } else if ($("#field-expiry_condition").val() == 'By Date') {
            $("#expires_field_box").show();
            $("#max_views_field_box").hide();
            $("#max_redumptions_field_box").hide();
        } else if ($("#field-expiry_condition").val() == 'By Views') {
            $("#expires_field_box").hide();
            $("#max_views_field_box").show();
            $("#max_redumptions_field_box").hide();
        } else if ($("#field-expiry_condition").val() == 'By Redemptions') {
            $("#expires_field_box").hide();
            $("#max_views_field_box").hide();
            $("#max_redumptions_field_box").show();
        }
    })

    $("#field-expiry_condition").trigger('change');
    if ($("#field-default_coupon-false").attr("checked") == 'checked') {
        $("#always_display_field_box").hide();
    }
    if ($("#field-default_coupon-true").attr("checked") == 'checked') {
        $("#always_display_field_box").show();
    }

1ce u created the script .. just use $crud->set_js method 2 include the javascript file

happy GCing :)


larasmith
  • profile picture
  • Member

Posted 30 July 2014 - 06:41 AM

hello! thanks for the reply... sorry but can you elaborate this example... I still cannot get this to work.

Thank you!  :)


Amit Shah
  • profile picture
  • Member

Posted 31 July 2014 - 07:23 AM

well.. this is a clear piece of jquery ... i give u only 1 simple bit of example

$("#field-expiry_condition").change(function() {
        if ($("#field-expiry_condition").val() == '') {
            $("#expires_field_box").hide();
            $("#max_views_field_box").hide();
            $("#max_redumptions_field_box").hide();
        } else if ($("#field-expiry_condition").val() == 'By Date') {
            $("#expires_field_box").show();
            $("#max_views_field_box").hide();
            $("#max_redumptions_field_box").hide();
        } else if ($("#field-expiry_condition").val() == 'By Views') {
            $("#expires_field_box").hide();
            $("#max_views_field_box").show();
            $("#max_redumptions_field_box").hide();
        } else if ($("#field-expiry_condition").val() == 'By Redemptions') {
            $("#expires_field_box").hide();
            $("#max_views_field_box").hide();
            $("#max_redumptions_field_box").show();
        }
    })

now here field field-expiry_condition is a drop down.. when changed .. based on the value of change, it shows / hides a certain additional fields.. you need to understand the dom / html generated by the gc code to control it.. and for that .. i recommend u use firefox with firebug extension !!


larasmith
  • profile picture
  • Member

Posted 01 August 2014 - 15:01 PM

well.. this is a clear piece of jquery ... i give u only 1 simple bit of example

$("#field-expiry_condition").change(function() {
        if ($("#field-expiry_condition").val() == '') {
            $("#expires_field_box").hide();
            $("#max_views_field_box").hide();
            $("#max_redumptions_field_box").hide();
        } else if ($("#field-expiry_condition").val() == 'By Date') {
            $("#expires_field_box").show();
            $("#max_views_field_box").hide();
            $("#max_redumptions_field_box").hide();
        } else if ($("#field-expiry_condition").val() == 'By Views') {
            $("#expires_field_box").hide();
            $("#max_views_field_box").show();
            $("#max_redumptions_field_box").hide();
        } else if ($("#field-expiry_condition").val() == 'By Redemptions') {
            $("#expires_field_box").hide();
            $("#max_views_field_box").hide();
            $("#max_redumptions_field_box").show();
        }
    })

now here field field-expiry_condition is a drop down.. when changed .. based on the value of change, it shows / hides a certain additional fields.. you need to understand the dom / html generated by the gc code to control it.. and for that .. i recommend u use firefox with firebug extension !!

Thank you for this! I will try this out and give feedback! Thanks and more power!  :)


larasmith
  • profile picture
  • Member

Posted 03 August 2014 - 15:44 PM

Well here it goes... i placed my code in the view where the grocery crud is called. Using inspect element I find the name of the element that I am trying to hide. I still cannot make it work. Please tell me what I did wrong. Here's the whole code of that page:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<?php include "link.php"; ?>
	<script src="<?php echo $link_;?>/jquery-2.1.1.min.js"></script>
	 	
	<?php foreach($css_files as $file): ?>
		<link type="text/css" rel="stylesheet" href="<?php echo $file; ?>" />
	<?php endforeach; ?>
	<?php foreach($js_files as $file): ?>
		<script src="<?php echo $file; ?>"></script>
	<?php endforeach; ?>

	<style type='text/css'>
		body
		{
			font-family: Arial;
			font-size: 14px;
		}
		a {
		    color: blue;
		    text-decoration: none;
		    font-size: 14px;
		}
		a:hover
		{
			text-decoration: underline;
		}
	</style>

	<script>
	$("#field-qb_opt1").hide();
	$("#field-qb_opt2").hide();
	$("#field-qb_opt3").hide();
	$("#field-qb_opt4").hide();	
	$("#field-qb_qtype_id").change(function() {
	        if ($("#field-qb_qtype_id").val() != 'Multiple Choice') {
	            $("#field-qb_opt1").hide();
	            $("#field-qb_opt2").hide();
	            $("#field-qb_opt3").hide();
	            $("#field-qb_opt4").hide();
	        } else {
	            $("#field-qb_opt1").show();
	            $("#field-qb_opt2").show();
	            $("#field-qb_opt3").show();
	            $("#field-qb_opt4").show();			
			}
	        }
	    });	
	</script> 
</head>
<body>

	<div style='height:20px;'></div>  
    <div>
		<?php echo $output; ?>
    </div>

</body>
</html>

Thanks for your support! Sorry I'm just a beginner.... I hope u don't find me annoying. Thanks!  :ph34r:  :)


Amit Shah
  • profile picture
  • Member

Posted 05 August 2014 - 10:38 AM

Sorry i was out for sometimes.. i have had been bz recovering from my accident :(

 

To my knowledge - u need to put this in document ready state of jquery / javascript.

If added there - it will set / execute properly.

Rest i dont really see any issue here.

Also .. do check up in firebug - console if there is anything wrong / javascript error anywhere. Cuz if there is any, it wont let the others execute itself. That might cause a correct code to be non functional

 

Hope this works out 4 u


larasmith
  • profile picture
  • Member

Posted 05 August 2014 - 11:47 AM

Thank you for your reply.  :)  It was very unfortunate to hear about your accident... :( Good thing that you are recovering now...

I hope for your fast recovery. I was able to make the code run now and here is my code:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<?php include "link.php"; ?>
	<script src="<?php echo $link_;?>/jquery-2.1.1.min.js"></script>
	 	
	<?php foreach($css_files as $file): ?>
		<link type="text/css" rel="stylesheet" href="<?php echo $file; ?>" />
	<?php endforeach; ?>
	<?php foreach($js_files as $file): ?>
		<script src="<?php echo $file; ?>"></script>
	<?php endforeach; ?>

	<style type='text/css'>
		body
		{
			font-family: Arial;
			font-size: 14px;
		}
		a {
		    color: blue;
		    text-decoration: none;
		    font-size: 14px;
		}
		a:hover
		{
			text-decoration: underline;
		}
	</style>

</head>
<body>
 
    <div style='height:20px;'></div>  
    <div>
		<?php echo $output; ?>
    </div>
   
	<script>
	$("#qb_opt1_field_box").hide();
	$("#qb_opt2_field_box").hide();
	$("#qb_opt3_field_box").hide();
	$("#qb_opt4_field_box").hide();	
	$("#field-qb_qtype_id").change(function() {
	if ($("#field-qb_qtype_id").val() == 1) { 
	        $("#qb_opt1_field_box").show();
	        $("#qb_opt2_field_box").show();
	        $("#qb_opt3_field_box").show();
	        $("#qb_opt4_field_box").show();		        		
	    } else {       	            
	        $("#qb_opt1_field_box").hide();
	        $("#qb_opt2_field_box").hide();
	        $("#qb_opt3_field_box").hide();
	        $("#qb_opt4_field_box").hide();	
		}
	    });	
	</script> 
</body>
</html>

I was using Chrome and I used the inspect element and its console just like in Firefox like you suggested... Thank you very much for your guidance  :)

God bless!