代码之家  ›  专栏  ›  技术社区  ›  Brad Rhoads

我能在Grails1中这样做吗:动态地创建数据输入表单

  •  0
  • Brad Rhoads  · 技术社区  · 17 年前

    我正在考虑在我当前的项目中使用Grails。我有几个要求,我希望我能在Grails中做到。

    首先,我有以下数据库表:

    TagType
    ---------
    tag_type_id
    tag_type
    
    
    Sample Data: TagType
    --------------------
    1,title
    2,author
    

    基于这些数据,我需要生成这样的数据输入表单 将其数据保存到另一个表。

    平铺______
    作者______

    保存取消

    我能在圣杯里做吗?你能给我指出正确的方向吗?

    谢谢!

    更多细节

    我正在构建一个支持OIA-PMH的数字图书馆系统,这是共享文档元数据的标准。标准规定每个元素都是可选的和可重复的。为了支持这个需求,我进行了以下数据库设计。

    我需要主要基于内容生成用户GUI(数据输入表单) 标签类型表(见上文)。表单中的数据保存到 标签(如果标签是新的)和项目标签表。

    Items
    ---------
    item_id
    last_update
    
    Tags
    --------
    tag_id
    tag_type_id
    tag
    
    TagType
    ---------
    tag_type_id
    tag_type
    
    Item_tags
    ---------
    item_id
    tag_id
    
    Sample Data: Items
    ------------------
    1,2009-06-15
    
    Sample Data: TagType
    --------------------
    1,title
    2,author
    
    Sample Data: Tags
    ------------------
    
    1,1,The Definitive Guide to Grails
    2,2,Graeme Rocher
    3,2, Jeff Brown
    
    Sample Data: Item_tags
    -----------------------
    1,1
    1,2
    1,3
    
    2 回复  |  直到 17 年前
        1
  •  0
  •   Jean Barmash    17 年前

    我不完全确定您在这里关于“将其数据保存到另一个表”的要求,但这里有一些想法。

    对于您拥有的表,您需要的域类如下:

    类标记{ 字符串类型

    }

    创建脚手架时,将自动为您生成ID字段。

    如果这还不够,请在问题中添加更多信息。

        2
  •  0
  •   Brad Rhoads    17 年前

    我是 真的? 喜欢格雷斯。几周前,当我第一次使用它的时候,我没有意识到它是一种成熟的语言。事实上,不止这些。它是一个完整的Web堆栈,包含一个Web服务器和一个数据库。总之,我问题的简短回答是肯定的。你甚至可以说是的,当然!以下是我创建的taglib的代码:

    import org.maflt.flashlit.pojo.Item
    import org.maflt.flashlit.pojo.ItemTag
    import org.maflt.flashlit.pojo.Metacollection
    import org.maflt.flashlit.pojo.SetTagtype
    import org.maflt.flashlit.pojo.Tag
    import org.maflt.flashlit.pojo.Tagtype
    
    /**
    * @return Input form fields for all fields in the given Collection's Metadataset. Does not return ItemTags where TagType is not in the Metadataset.
    *  
    * In Hibernate, the
    *
    *    "from ItemTag b, Tag a where b.tag= a"
    *
    * query is a cross-join. The result of this query is a list of Object arrays where the first item is an ItemTag instance and the second is a Tag instance.
    *
    * You have to use e.g.
    *
    *    (ItemTag) theTags2[0][0]
    *
    * to access the first ItemTag instance.
    * (http://stackoverflow.com/questions/1093918/findall-not-returning-correct-object-type)
    **/
    class AutoFormTagLib {
    
        def autoForm = {attrs, body ->
            //def masterList    = Class.forName(params.attrs.masterClass,false,Thread.currentThread().contextClassLoader).get(params.attrs.masterId)
            def theItem     = Item.get(attrs.itemId)
            def theCollection   = Metacollection.get(attrs.collectionId)
            def masterList  = theCollection.metadataSet.setTagtypes
            def theParams   = null
            def theTags     = null
            def itemTag     = null
            def tag         = null
            masterList.each {
    
                theParams   = [attrs.itemId.toLong(),it.tagtype.id]
                theTags     = ItemTag.findAll("from ItemTag d, Item c, Tag b, Tagtype a where d.item = c and d.tag = b and b.tagtype = a and c.id=? and a.id=?",theParams)
    
                for (int i=0;i<it.maxEntries;i++) {                 
                    out << "<tr>\n"
                    out << "    <td>${it.tagtype.tagtype}</td>\n"
                    out << "    <td>\n"
                    if (theTags[i]) {
                        itemTag     = (ItemTag) theTags[i][0]
                        //item  = (Item)    theTags[i][1]
                        tag     = (Tag) theTags[i][2] 
                        //itemTag   = (Tagtype) theTags[i][3]
                        out << "    <input name='${it.tagtype.tagtype}_${i}_${itemTag.id}' value='${tag.tag}' />\n";
                    }
                    else
                        out << "       <input name='${it.tagtype.tagtype}_${i}' />\n";
                    out << "    </td>\n"
                    out << "</tr>\n"
                }
            }
    
        }
    }
    
    推荐文章