⚠ 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

Add entry in related field



gps

gps
  • profile picture
  • Member

Posted 15 July 2012 - 07:32 AM

Hi,

I am trying to build an app so that I can keep a record of various papers/articles. Articles have a n-n relationship with authors and a 1-n relationship with journals.

Presently if I wish to add an article from a new author or from a new journal, I have to add the author and journal first and then go to add paper view and then add the paper.

What I would like is that somehow I can directly go to the add paper view and from there jump to add author view (as a pop up or in new tab or within the same tab as long as I don't lose any data already filled) if I don't find the author. Similarly for the journal.

One way I know to achieve this is to make the CRUD myself and have all things the way I need but I do not want to spend so much time doing that.

Could you suggest an alternative way to do this?

PS: This can be a suggestion for future features in Grocery (if not already present).

fdias

fdias
  • profile picture
  • Member

Posted 15 July 2012 - 18:23 PM

If you don't mind having another tab or a popup I would suggest using a link on the crud by using a callback function such as this:

On crud before render:


$crud->callback_add_field('somefield',array($this,'add_field_callback_1'));


Then the function:



function add_field_callback_1()
{
return '<input type="text" maxlength="50" value="" name="somefield" style="width:462px"> - <a href="LINK_TO_ADD_AUTHOR CRUD" target="_blank">Add Author</a>';
}


Check the docs for the callback_add_field http://www.grocerycrud.com/documentation/options_functions/callback_add_field and callback_edit_field http://www.grocerycrud.com/documentation/options_functions/callback_edit_field

You could have it so it opens on a pop up (using a window.open javascript function or a lightbox iframe style jquery plugin to make it fancy) and even set it to refresh the parent page once you close it, this way you will be able to see the newly added author on the relation list.

I hope I was clear :)

Cheers.

fdias

fdias
  • profile picture
  • Member

Posted 15 July 2012 - 18:29 PM

I was checking out this for a project myself and I'm thinking about using fancybox jquery plugin.

Check out the iframe demo:

http://fancybox.net/

If you call the other crud on the iframe and refresh the parent frame (your current crud) on a onClosed event. I think it could work well.

Let me know how it works out for you. I'm thinking about not only using this but perhaps add it as a plugin for GC as I find it very usefull.

Cheers.

gps

gps
  • profile picture
  • Member

Posted 16 July 2012 - 04:14 AM

Thanks a lot fdias.

For now I have used the iframe method and it worked like a charm!

I am sure it would be a good idea to add this in as a plugin. Let me know when you create the said plugin :)

Thanks again and cheers!

--
Just one more thing here: Author and Paper have a n-n relation. By following your method now I have to keep looking up the name and enter the corresponding ID or query and display a table of id-name relationship on this page. The original n-n relation input system was much better at this. Can I somehow add this 'add author' button along with the original input mechanism rather than the 'text input'.

fdias

fdias
  • profile picture
  • Member

Posted 16 July 2012 - 11:45 AM

Well you would have to call it from the callback function as it will replace whatever you have.
I guess it's possible, but it should be a little tricky to do.

Another option I thought about was to have an iframe on the callback function with the desired grid and sent to that iframe the primary key so I can filter the data.

gps

gps
  • profile picture
  • Member

Posted 16 July 2012 - 16:04 PM

For now I have used a check-box type system with a list of all authors displayed there. It may work out with a small number of options but I do not think it will look good if I have a huge number of authors in the database. So, I am now thinking of building a system where the list is generated on the fly (based on the few characters entered by the user) by using ajax and increasing the number of authors every-time an author is selected.

Iframes is a good idea. It might bring down the amount of coding. It would be something new for me to pass data between the parent and the child iframes.

It will solve the purpose but we surely need a plugin to do this job.

fdias

fdias
  • profile picture
  • Member

Posted 16 July 2012 - 16:49 PM

I know I faced this same problem passing data. They way I figured was to pass the ID on the URL of the iframe and get the URI $this->uri->segment(3, 0) and use the id on the where clause.