简体   繁体   English

Joomla! 组件表单和jQuery动态添加/删除字段子集

[英]Joomla! component form and jQuery to add/remove subset of fields dynamically

I have a Joomla component for managing workshops, called com_workshops with its corresponding table, jos_workshops. 我有一个用于管理研讨会的Joomla组件,称为com_workshops及其相应的表jos_workshops。

Then I have the core com_users component, with its jos_users table. 然后,我有了核心的com_users组件及其jos_users表。

I have a link table, jos_works_users in order to associate users to workshops (N,N). 我有一个链接表jos_works_users以便将用户与工作坊(N,N)关联。

I know how to add a form field type="user" so I can select from the users modal view a user. 我知道如何添加一个表单字段type =“ user”,以便可以从用户模式视图中选择一个用户。

But I would like the form to be able to add as many form field types like this one as I 但是我希望表单能够像我一样添加尽可能多的表单字段类型

want, and to remove them dynamically. 并动态删除它们。 I'm thinking about 我在想

jQuery (append(), clone(), remove() functions)

But don't know if it's possible inside a Joomla! 但不知道Joomla内是否可能! form, and how to do it. 表格,以及操作方法。

I have made a similar field, although I used mootools. 尽管使用了mootools,但我也做了类似的工作。 If I were you I'd create a new field type ( Joomla docs ) and add some javascript to the page using JHTML::script(). 如果您是我,我将创建一个新的字段类型( Joomla docs ),然后使用JHTML :: script()将一些JavaScript添加到页面中。 You need to use a input name like jform[userids][] so that the browsers can pass a array of user ids. 您需要使用输入名称(例如jform [userids] []),以便浏览器可以传递用户ID数组。

My case was not exactly like yours, but this is pretty much how I did it: 我的案子与您的案子并不完全一样,但这几乎就是我做的事:

My HTML looked like this (note that it has two text fields per row): 我的HTML如下所示(请注意,每行有两个文本字段):

<div id="ingredientList1">
    <div class="ingredient-row">
        <div class="delete"></div>
        <input type="text" class="amountField" style="display: block; " name="jform[ingredientAmounts][]">
        <input type="text" class="nameField" style="display: block; " name="jform[ingredients][]">
    </div>
    <div class="ingredient-row">
        <div class="delete"></div>
        <input type="text" class="amountField" style="display: block; " name="jform[ingredientAmounts][]">
        <input type="text" class="nameField" style="display: block; " name="jform[ingredients][]">
    </div>
    <div class="ingredient-row">
        <div class="add"></div>
        <input type="text" class="amountField" style="display: none; ">
        <input type="text" class="nameField" style="display: none; ">
    </div>
</div>

Your javascript should: 您的JavaScript应该:

  1. Remove a row (in mootools: row.dispose()) when user clicks the delete button. 当用户单击删除按钮时,删除一行(在mootools中:row.dispose())。
  2. Make the last row visible and create a new hidden one when add button is clicked. 单击添加按钮时,使最后一行可见并创建一个新的隐藏行。
  3. Have a method that can load previous data from DB. 有一种方法可以从数据库加载以前的数据。
  4. Add code to your table in load (to select your user ids) and store (to save them to db). 将代码加载到表中(选择用户ID)并存储(将它们保存到db)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM