如何在Drupal 10表格里嵌入form元素

By admin , 31 一月, 2024
  public function buildForm(array $form, FormStateInterface $form_state, $id = 0) {
    $form['exam_table'] = [
      '#type' => 'table',
      '#header' => [
        'select' => '选择',
        'name' => '考试名称',
        'exam_time' => '考试时间',
        'grade' => '年级',
        'weight' => '权重',
      ],
      '#empty' => '暂无考试',
    ];

    $gidValues = json_decode($duokao->weights ?? '[]', TRUE);

    // 循环生成每一行
    foreach ($exams as $exam) {
      $id = $exam->gid;

      // 每行的 Checkbox
      $form['exam_table'][$id]['select'] = [
        '#type' => 'checkbox',
        '#default_value' => $gidValues[$id]['select'] ?? 0,
      ];

      // 考试标题(纯文本,不可编辑)
      $form['exam_table'][$id]['name'] = [
        '#plain_text' => $exam->name,
      ];

      $form['exam_table'][$id]['exam_time'] = [
        '#plain_text' => date("Y-m-d", $exam->exam_time),
      ];

      $form['exam_table'][$id]['grade'] = [
        '#plain_text' => School::getGradeName($exam->grade),
      ];

      // 浮点权重输入框
      $form['exam_table'][$id]['weight'] = [
        '#type' => 'number',
        '#step' => '0.01',   // 浮点精度
        '#min' => '0',
        '#default_value' => $gidValues[$id]['weight'] ?? 0.00,
        '#attributes' => [
          'style' => 'width: 5rem;'
        ],
      ];
    }

    // 提交按钮
    $form[] = [
      '#type' => 'submit',
      '#value' => '保存',
    ];
    
    return $form;
  }
  
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $op = $form_state->getValue('op');
    $values = $form_state->getValue('exam_table');
    //\Drupal::messenger()->addMessage(var_export($values, TRUE));

    // 示例:输出选中的考试 + 权重
    $weights = [];
    foreach ($values as $id => $row) {
      if ($row['select']) {
        $weights[$id] = $row['weight'];
      }
    }
  }

下面方法比较麻烦,建议用上面的方法:

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;

// Create a form element callback function to build the select element.
function custom_select_element_callback(array &$form, FormStateInterface $form_state) {
  // Define the select options.
  $options = [
    'option1' => 'Option 1',
    'option2' => 'Option 2',
    'option3' => 'Option 3',
  ];

  // Build the select element.
  $element = [
    '#type' => 'select',
    '#title' => t('Select an option'),
    '#options' => $options,
    '#default_value' => isset($form_state->getValue('custom_select')) ? $form_state->getValue('custom_select') : NULL,
  ];

  return $element;
}

// Create a form function to define the form structure.
function custom_form_example_form(array &$form, FormStateInterface $form_state) {
  // Initialize the table element.
  $form['table'] = [
    '#type' => 'table',
    '#caption' => t('Table with Select Element'),
    '#header' => [t('Column 1'), t('Column 2')],
  ];

  // Add row with select element in the first column.
  $form['table']['row1'] = [
    'column1' => [
      'select_element' => custom_select_element_callback($form, $form_state),
    ],
    'column2' => [
      // Example markup in column 2.
      '#markup' => 'Data for column 2',
    ],
  ];

  // Add more rows if needed.

  // Add a submit button.
  $form['submit'] = [
    '#type' => 'submit',
    '#value' => t('Submit'),
  ];

  return $form;
}

// Define a form submission function.
function custom_form_example_form_submit(array &$form, FormStateInterface $form_state) {
  // Handle form submission here.
}

 

标签

评论

Restricted HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id> <img src>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。
CAPTCHA
请输入"Drupal"
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.