⚠ 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

Adding new runtime data in $crud->set_relation_n_n field



clustersblue
  • profile picture
  • Member

Posted 15 March 2016 - 06:49 AM

From the example of $crud->set_relation_n_n that can be found in the GC site we could easily create a multiselect field.  I have tried it and it works perfectly.  However, one time it came into my requirement that I need to add data on runtime into the list (i.e. in category).  I wonder if it is possible to add a new option into the select field.    

function film_management()
{
    $crud = new grocery_CRUD();
 
    $crud->set_table('film');
    $crud->set_relation_n_n('actors', 'film_actor', 'actor', 'film_id', 'actor_id', 'fullname','priority');
    $crud->set_relation_n_n('category', 'film_category', 'category', 'film_id', 'category_id', 'name');
 
    $crud->unset_columns('description','special_features','last_update');
    $crud->fields('title', 'description', 'actors' ,  'category' ,'release_year', 'rental_duration', 'rental_rate', 'length', 'replacement_cost', 'rating', 'special_features');
 
    $output = $crud->render();
 
    $this->_example_output($output);
}

What I have tried so far is using javascript like this below.  However, though it shown on display but functionally it didn't work out.  Clicking on the new option will not display its data in the multiselect field.  

$("#field-Category").append('<option value=1>My option</option>');
$(".chzn-results").append('<li id="field_INTERESTED_chzn_o_11" class="active-result" style="">My option</li>');

Does anybody have tried so far or have this kind of implementation before?  Appreciate if someone could provide me some solution or could say that this is not possible. : )

 

Thanks,


Amit Shah
  • profile picture
  • Member

Posted 15 March 2016 - 07:28 AM

http://grocerycrud.amitsgarage.com/bwl-knowledge-base/conditional-multiselect/

 

Read the above solution shared ... there u will find the answer as what to do post you append ..:)

Happy GCing :)


clustersblue
  • profile picture
  • Member

Posted 15 March 2016 - 09:03 AM

Amit,

 

I got your KB.  Steps 1 and 2 seems fine.  However, steps 3 and 4 have me some issues:

  • For step 3, do we need to empty the field-actors? how?
  • For step 4, I couldn't use the multiselect function to refresh the list if it is what that statement is implying to.  In my console it show that it is not a function. TypeError: $(...).multiselect is not a function

 

Here's my code so far

    var items = [];
    $('#field-INTERESTED option:selected').each(function(){
    	items.push($(this).val())
    	});
    
    $("#field-INTERESTED").append("<option value='2999'>Amit Shah</option>");
    
    $("#field-INTERESTED").multiselect('destroy');
    $("#field-INTERESTED").multiselect();

By the way,  I bookmarked your KB for my future reference if you don't mind.  :)

 

 

Thanks


Amit Shah
  • profile picture
  • Member

Posted 18 March 2016 - 06:22 AM

Sorry for a delayed update - but was very busy this days ....

 

Very first - absolutely no problem bookmarking it - it is meant to be done :)

 

Secondly - to empty the selectbox - 

$('#field-INTERESTED option:selected').empty()

 

Thirdly - i referred to the following page - http://167.114.155.240/crudteste/CodeIgniter-3.0.4/index.php/examples/programmation_theorique/add

There - you see, the library used was multiselect - hence i set the code accordingly.

If you have something other then multiselect .... like select2 - u need to find the alternative mechanism for the same. That should solve your problem.

 

Happy GCing :)


clustersblue
  • profile picture
  • Member

Posted 04 April 2016 - 01:57 AM

No problem.  It's been awhile also, I was not able to check my post.  Priorities are changing almost everyday.  :)

 

Anyway, I think I will be using the another kind of GC multiselect.  The one with the "category" field: http://www.grocerycrud.com/documentation/options_functions/set_relation_n_n is more appropriate of our needs right now.

 

I'm not sure if your link above still applicable to another kind of GC multiselect with the priority as null.

 

Here's the snippet of my code:

$crud->set_table('trms_request')
                 ->set_relation('CATEGORY_ID','trms_category','CATEGORY')
                 ->set_relation('REQ_STATUS_ID','trms_req_status','STATUS')
                 ->set_relation_n_n('INTERESTED', 'trms_interested_party', 'trms_users', 'REQUEST_ID', 'EMP_NUM','USER_NAME',NULL)

So my 'INTERESTED' field has a null value for the priority field.