Drupal:“删除”后如何修改表单 ui使用 AJAX 按下按钮?

发布于 2024-08-17 19:12:53 字数 1740 浏览 4 评论 0原文

我正在使用 Drupal 6 并安装了 AJAX 模块。我有以下代码:

function remove_manufacturer_role_form($form_state) {
  $form['#ajax'] = array(
    'enabled' => TRUE
  );
  $form['hidden'] = array('#type' => 'value', '#value' => 'is_it_here');
  $form['submit'] = array('#type' => 'submit', '#value' => t('Remove yourself as manufacturer'));
  return $form;
}

function remove_manufacturer_role_form_submit($form, &$form_state) {
     $current_vars = ogrolerequestmanufacturerblock_get_current_vars();

     if( $current_vars )
     {
    $curr_gid = $current_vars['current_gid'];
    $curr_uid = $current_vars['current_user_id'];
    $delete_query = "DELETE FROM {og_users_roles} WHERE rid in (SELECT rid FROM {role} WHERE name='Manufacturer') AND uid=$curr_uid AND gid=$curr_gid";
    if( db_query($delete_query) )
        drupal_set_message("You successfully removed yourself as a manufacturer from this project");
     }
}

基本上,我有一个表单可以在项目中删除作为制造商的用户。用户有能力自行删除。该表单允许用户单击一个按钮,该按钮又使用 AJAX 提交表单,从数据库中删除用户作为制造商的角色,并通知用户是否成功。它成功地使用 AJAX 进行调用并按应有的方式删除角色,但表单上仍然显示删除按钮。如果用户再次单击该按钮,则会出现错误,因为没有任何内容可删除。我想这没关系,b/c 这不是致命错误,并且不会真正影响任何事情......但是,这对用户来说并不漂亮。

有没有办法刷新模块的块或修改提交后显示的表单?

编辑:

这似乎是一个很好的解决方案,但我似乎无法让它正常工作。我已经在我的模块中包含了delete.js javascript:

/**
 * Ajax Forms plugin for ogrolerequestmanufacturerblock 
 *
 * @param {String} hook
 * @param {Object} args
 * @return {Bool}
 */

Drupal.Ajax.plugins.ogrolerequestmanufacturerblock = function(hook, args) {
alert('it got into javascript!');
    if (hook === 'submit') {
        alert('submit called within javascript!');
    }
    return true;
}

它似乎永远不会进入这个javascript 函数。 drupal如何知道调用这个特定的函数?

I am using Drupal 6 and have the AJAX module installed. I have the following code:

function remove_manufacturer_role_form($form_state) {
  $form['#ajax'] = array(
    'enabled' => TRUE
  );
  $form['hidden'] = array('#type' => 'value', '#value' => 'is_it_here');
  $form['submit'] = array('#type' => 'submit', '#value' => t('Remove yourself as manufacturer'));
  return $form;
}

function remove_manufacturer_role_form_submit($form, &$form_state) {
     $current_vars = ogrolerequestmanufacturerblock_get_current_vars();

     if( $current_vars )
     {
    $curr_gid = $current_vars['current_gid'];
    $curr_uid = $current_vars['current_user_id'];
    $delete_query = "DELETE FROM {og_users_roles} WHERE rid in (SELECT rid FROM {role} WHERE name='Manufacturer') AND uid=$curr_uid AND gid=$curr_gid";
    if( db_query($delete_query) )
        drupal_set_message("You successfully removed yourself as a manufacturer from this project");
     }
}

Basically, I have a form to remove a user as a manufacturer within a project. The user has the ability to remove themselves. This form allows the user to click a button, which in turn uses AJAX to submit the form, delete the user's role as manufacturer from the database, and notify the user whether it was successful or not. It successfully makes the call using AJAX and deletes the role like it should, but the form still has the remove button shown on it. If the user clicks the button again it gives an error b/c there is nothing to delete. This is OK, I guess, b/c it is not a fatal error and doesn't really affect anything ... however, it's not pretty for the user.

Is there a way I can refresh the module's block or modify the form that is shown once it is submitted?

Edit:

This seems like a good solution, but I can't quite seem to get it working. I've included the delete.js javascript within my module:

/**
 * Ajax Forms plugin for ogrolerequestmanufacturerblock 
 *
 * @param {String} hook
 * @param {Object} args
 * @return {Bool}
 */

Drupal.Ajax.plugins.ogrolerequestmanufacturerblock = function(hook, args) {
alert('it got into javascript!');
    if (hook === 'submit') {
        alert('submit called within javascript!');
    }
    return true;
}

It never seems to get into this javascript function. How does drupal know to call this specific function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃颗糖壮壮胆 2024-08-24 19:12:53

您可以检查通过 AJAX 发送的返回值,如果删除成功,您可以在删除按钮上执行 display: none 操作。

查看 AJAX 模块文档,我认为您需要实现 Drupal.Ajax.plugins.FOO(hook, args)。当hook == Complete时,您可以将按钮设置为display: none

You can check the return value coming through AJAX and if the removal has been successful, you can do a display: none on the remove button.

Looking at the AJAX module documentation, I think you will need to implement Drupal.Ajax.plugins.FOO(hook, args). When hook == complete, you can set your button to display: none.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文