1

シンプルなフィルター フォームと、その下のテーブルが必要です。ユーザーが選択フォームのオプションを変更すると、テーブルが自動的に変更されます。私はそれがああで終わったと思います。

私はこれが欲しいです(テーブルを含むフィールドセットなど、いくつかの変更が可能です):

しかし、働いています..もちろん..

現在、1 つの関数を使用してそのページを表示しています。それは完全な混乱であり、「これを行うことはありません」のようなものですが、私はdrupalの学習者であるため、調査していくつかのことを試しています。これは関連するコードです:

    form = array();  
    ahah_helper_register($form, $form_state);  
    //query here, build $options for the select

$form['listar_veics'] = array(  
        '#type'   => 'fieldset',  
    '#prefix' => '<div id="listar-veics-wrapper">',   
    '#suffix' => '</div>',  
    '#tree'   => TRUE,
    );

if (!isset($form_state['values']['listar_veics']['filial']))
    $choice = 1;
else 
    $choice = $form_state['values']['listar_veics']['filial'];


$form['listar_veics']['filial'] = array(
    '#type' => 'select', 
    '#title' => "Listar veículos da filial", 
    '#options' => $filiais,
    '#default_value' => $choice,
    '#ahah' => array(
        'event'     => 'change',
        'path'      => ahah_helper_path(array('listar_veics')),
        'wrapper'   => 'listar-veics-wrapper',
        'method'    => 'replace',
        ),
);

//query for the rows i wanna show

//building $data array, the rows array

//building $header, as an array of strings

$table = theme_table($header, $data);

$page = drupal_render($form);
$page .= $table;
return $page;

したがって、このコードでは、drupal はフォーム自体のみを置き換えます。選択のオプションを変更すると、選択の新しい値が表示されますが、テーブルは再度レンダリングされないため、変更されません。

ありがとう、すべての提案に感謝します。

4

3 に答える 3

4

ビューモジュールを見ることをお勧めします。

http://drupal.org/project/views

これをインストールすると、カスタム クエリを作成できます。

  1. テーブルに表示するすべてのフィールドを追加します。(プラカ、マルカ)
  2. 次に、クエリを制限するフィルターを追加します。(Listar veiculos da filial)
  3. フィルターを追加するときは、フィルターを「公開」して、クエリを表示するときにページのオプションを変更できるようにします。
  4. 次に、作成したものを表示したい場合は、ディスプレイを追加する必要があります。ページ表示にする場合は、パスをテーブルに直接設定できます。

詳細については、http://gotdrupal.com/videos/drupal-views-tutorialをお勧めします。

きちんと公開されたフィールドが必要な場合は、http://drupal.org/project/better_exposed_filtersもお勧めします

于 2010-11-04T10:30:06.543 に答える
1

Asimov の回答を拡張するために、ノードを選択するための分類用語フィルターを示すコード例 ( Drupal 7用) を示します。選択した用語はセッションに保存され、結果をフィルター処理するためにクエリで使用されます。

カスタムモジュールに入れることができます。ビューやその他の提供されたモジュールは必要ありません。以下のコード例では、カスタム モジュールの名前はticです。ticの名前をカスタム モジュールの名前に変更します。

次の 4 つの要素が必要です。

  1. フィルタを出力し、結果をフェッチして出力する関数
  2. フィルターフォーム
  3. 選択したフィルター オプションをセッションに保存するカスタム送信関数
  4. セッションをクリアするリセット機能

hook_menu() を使用して tic_fetch_results() を呼び出します。

結果の取得、フィルタリング、出力

この例では、条件を使用して簡単に拡張できるため、動的クエリを使用しています。

/**
 * Filters, fetches and outputs results
 */

function tic_fetch_results() {

  // Adds filter form to the build array.
  $form = drupal_get_form('tic_term_filter_form');

  $output = drupal_render($form);

  $node_types = array('article', 'page', 'blog_post');

  // Sets up dynamic query
  $query = db_select('node', 'n')
      ->extend('PagerDefault')
      ->limit(33)
      ->fields('n', array('nid', 'title'))
      ->condition('n.type', $node_types, 'IN')
      ->condition('n.status', 1);

  // Fetches selected values from session and applies them to the query.
  if (isset($_SESSION['form_values']['terms']) && count($_SESSION['form_values']['terms']) > 0) {
    $query->join('field_data_field_tags', 'tags', 'n.nid = tags.entity_id');
    $query->condition('tags.field_tags_tid', $_SESSION['form_values']['terms'], 'IN');
    $query->condition('tags.bundle', $node_types, 'IN');
  }
  $result = $query->execute();

  $items = array();
  foreach ($result as $row) {
    $items[] = array('data' => $row->nid . ' - ' . $row->title);
    // do something interesting with the results
  }
  $output .= theme('item_list', array('items' => $items, 'title' => '', 'type' => 'ul', 'attributes' => array()));
  $output .= theme('pager');
  return $output;
}

フォームを構築する

分類用語オプション リストは、語彙タグから入力されます。

/**
 * Implements hook_form().
 */
function tic_term_filter_form($form, &$form_state) {

  // Loads terms from the Tags vocabulary and use as select options.
  $vocab = taxonomy_vocabulary_machine_name_load('tags');
  $terms = taxonomy_get_tree($vocab->vid);
  $term_options = array();
  foreach ($terms as $term) {
    $term_options[$term->tid] = $term->name;
  }

  // Sets the values that are stored in session as default.
  $storage = (isset($_SESSION['form_values']) ? $_SESSION['form_values'] : 0);
  $selected_terms = isset($storage['tags']) ? $storage['tags'] : NULL;

  $form['terms'] = array(
    '#title' => 'Filter by terms',
    '#type' => 'select',
    '#options' => $term_options,
    '#multiple' => TRUE,
    '#default_value' => $selected_terms,
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Filter'),
  );

  $form['reset'] = array(
    '#type' => 'submit',
    '#value' => t('Reset'),
    '#weight' => 30,
    '#submit' => array('tic_tools_reset'),
  );

  return $form;
}

選択した値をセッションに保存します

/**
 * Implements hook_form_submit().
 */
function tic_term_filter_form_submit(&$form, &$form_state) {

  // Stores form values in session.
  $_SESSION['form_values'] = $form_state['values'];
}

フィルターをリセットする

/*
 * Clears set filters.
 */

function tic_tools_reset() {
  if (isset($_SESSION['form_values'])) {
    unset($_SESSION['form_values']);
  }
  drupal_goto(current_path());
  drupal_set_message('Filters were reset');
}
于 2014-06-26T12:31:42.367 に答える