Jump to content

determine whether a value has been changed with multi select transfer


johnmerlino

Recommended Posts

Hey all,

 

I would like to know the best way to determine whether a value has been changed in a multi select transfer. For example, I have two select fields. User clicks on button to add an item to the right select element with id of select2 from the left select element with an id of select1. When they click update, it posts to a php method. This is where I am uncertain what to do. How can I check that they indeed added or removed items from the right select element and if they did, determine which ones were removed or added to make the reflected changes in the database?

 

This is what I have, but it's bad solution because it deletes all related records before it adds the ones it finds to the right select element:

 

	public function update(){

		$subcategory = new Category();
		$subcategory->where('id',$this->uri->segment(4))->get();

		$subcategory->delete($subcategory->related_category->get()->all);

		$new_parents = $this->input->post('catupload');
		$parent_category = new Category();
		$parent_category->where_in('controller',$new_parents)->get();

		$subcategory->save($parent_category->all);
	}

 

 

This is what the form elements look like:

 

		echo "You can add or remove one or many parents associated with this category:";
	echo "<div class='multiselect-transfer'>";
	echo form_multiselect('catadd[]',$other_cats,'','id="select1"');
	echo anchor('#','add >>','id="add"'); 
	echo "</div>";
	echo "<div class='multiselect-transfer'>";
	echo form_multiselect('catupload[]',$default_cats,'','id="select2"');
	echo anchor('#','<< remove','id="remove"');
	echo "</div>";		
	echo form_submit('submit','Update');
	echo form_close();

$other_cats variable holds array of categories that have not been asssociated with the current category being updated when this category was first created. The $default_cats variable holds an array of categories that have been associated with the current category being updated when this category was first created.

 

And the javascript:

 

	 
$().ready(function() {  
   $('#add').click(function() {  
    return !$('#select1 option:selected').remove().appendTo('#select2');  
   });  
   $('#remove').click(function() {  
    return !$('#select2 option:selected').remove().appendTo('#select1');  
   });  
  }); 

 

Thanks for response.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.