⚠ 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 dynamic input fields



vrsotto

vrsotto
  • profile picture
  • Member

Posted 07 October 2014 - 03:45 AM

Hi!

 

just new to grocerycrud. just wanna ask, how can i implement dynamic input fields? meaning, if i have a button "Add new field" and clicked, it will then create new input field(s). 

 

Thanks

 


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 08 October 2014 - 05:08 AM

Hi there,

 

Welcome to the GC world.

Well the solution to your query / situation is simple.. that thing that you are asking, you can achieve it with javascript only. .. where you hook to such a button and keep on adding fields 1 after the other as and when needed.

 

Well what i can suggest you for this - is to manage the fields before insert ... u can do it by adding callback_before_insert - where u take care of extra additional fields added .. and values entered by user...

Dont forget to remove them cuz GC will understand only those fields defined by user or present in the table.. nothing beyond the point. I recommend doing it cuz otherwise GC will throw error

 

Happy GCing :)


vrsotto

vrsotto
  • profile picture
  • Member

Posted 08 October 2014 - 08:59 AM

Hi Amit,

 

I understand other idea you mentioned, but not sure what you mean (by the quote bellow, further explain please). sorry for picking'up slow...

 

Dont forget to remove them cuz GC will understand only those fields defined by user or present in the table.. nothing beyond the point. I recommend doing it cuz otherwise GC will throw error

 

Thanks for your help!


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 08 October 2014 - 09:45 AM

Well.. grocery crud is bound around the fields of a table..

u can just give the table name and it will figure out the max / rest...

but u have a choice to fix the fields with what u wana work on with. Now this can also include fields that dose not exist in the table. When u try to save them, grocery crud wont verify the field existance with the table.. it will just continue with building the insert query from the definition provided ... or it will pickup all the filelds from table and build the query (in case u didnt define the fields u planning to work with).

 

In this scenario of yours when u keep adding dynamic fields using javascript.. and when you click on save.. guess what will happen .. yes gc will fail to save.. it will throw error or whatever...

so in order to avoid the situation.. what i recommended is to add a callback before insert so there - u can manually deal with the new fields added dynamically.. and remove it from post_array (the variable to the callback function) .. and after removing the fields from array.. just return it back to the GC .. and then whats left over are the fields that were defined / from the table structure..

 

Hope this makes the picture more clear...

 

Happy GCing :)


vrsotto

vrsotto
  • profile picture
  • Member

Posted 10 October 2014 - 09:41 AM

Amit! Thanks very much for taking your time. its clear to me now. thanks again!


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 10 October 2014 - 10:49 AM

Happy to help...

and Happy GCing :)


2fed

2fed
  • profile picture
  • Member

Posted 13 May 2016 - 10:56 AM

is it bossible to see a code?