Tableselect with return new response doesn't work

by vitius   Last Updated October 19, 2019 12:07 PM

I'm newbie in drupal and I want to create contact list with dynamic search on ajax. I want to ajax load my tableselect. So I use return new Response(render($form)); where the form is tableselect. I use this for return only form without header, footer and etc. But the tableselect shows no data in table.

If I use it as normal table, it works. You can see on the picture. This is what I want, but with checkboxes.

enter image description here With tableselect it return table with no entries. Can I implement checkbox to normal table? Or can I return only the table differently?

My code:

        <?php
  /**
   * @file
   * Contains \Drupal\contactlist\Form\Contactlist
   */
namespace Drupal\contactlistmodule\Form;

use Drupal\Core\Database\Database;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use \Symfony\Component\HttpFoundation\Response;
/**
 * Provides contact list form.
 */
class Contactlist extends FormBase {
public function getFormId() {
    return 'contact_list';
  }
  protected function load($header) {
    $select = Database::getConnection()->select('civicrm_contact', 'c');
    $select->join('civicrm_email', 'e', 'c.id = e.contact_id');
    $select->join('civicrm_phone', 'p', 'c.id = p.contact_id');
    $select->addField('c', 'display_name' );
    $select->addField('e', 'email');
    $select->addField('p', 'phone');
    $select->addField('c', 'id');
    $select = $select->extend('Drupal\Core\Database\Query\TableSortExtender');
    $select = $select->orderByHeader($header);
    $select = $select->extend('Drupal\Core\Database\Query\PagerSelectExtender')->limit(20);
    $entries = $select->execute()->fetchAll(\PDO::FETCH_ASSOC);
    return $entries;
  }

  public function buildForm(array $form, FormStateInterface $form_state) {
    $header = array(
      'name' => array('data' => t('Name'), 'field' => 'c.display_name'),
      'email' => array('data' => t('Email'), 'field' => 'e.email'),
      'phone' => array('data' => t('Phone'), 'field' => 'p.phone',  'sort' => 'DESC'),
    );

    foreach ($entries = $this->load($header) as $entry) {
      $name = array('data' => $entry['display_name'], 'class' => 'link', 'id' => $entry['id']);
      $email = array('data' => $entry['email'], 'class' => 'link', 'id' => $entry['id']);
      $phone = array('data' => $entry['phone'], 'class' => 'link', 'id' => $entry['id']);
      $profile = array('data' => t(""), 'colspan' => '3');
      $loading = array('data' => t("LOADING"), 'colspan' => '3');
      $attributes = array('class' => array('content'));
      $profile_id = 'profile_' . $entry['id'];
      $loading_id = 'loading_' . $entry['id'];

      $rows[$entry['id']] = array(
        'name' => $name,
        'email' => $email,
        'phone' => $phone,
        '#attributes' => $attributes
      );   // row with basic info
      $rows[$profile_id] = array(
        'name' => $profile,
        '#attributes' => array('class' => array('profile', $profile_id))
      );   // profile row, hidden in default
      $rows[$loading_id] = array(
        'name' => $loading,
        '#attributes' => array('class' => array('loading', $loading_id))
      );   // loading row hidden in default
    }

    $form['table'] = array(
      '#id'    => 'select-attendees',
      '#type' => '**tableselect**',
      '#header' => $header,
      '#options' => $rows,
      '#empty' => t('No entries available.'),
    );
    // do not cache this page.
    $form['$cache']['max-age'] = 0;
    $form['pager'] = array(
      '#type' => 'pager'
    );
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('submit'),
    );

    **return new Response(render($form));**
  }
  public function validateForm(array &$form, FormStateInterface $form_state) {

  }
  public function submitForm(array &$form, FormStateInterface $form_state) {
    drupal_set_message(t('The form is working.'));
  }

  public function processTable(&$element, FormStateInterface $form_state, &$complete_form) {


  }


}

And I want load the table with ajax like this:

  $('.control-label').mousedown(function(event) {
    $.ajax({
      type: "post",
      url: 'contacts',
      data: "somedata",
      cache: false,
      success: function(html) {
        $('#contact_table').html(html);
      }
    });
  });
Tags : 8 forms ajax


Related Questions


Updated July 06, 2015 13:03 PM

Updated April 20, 2015 06:03 AM

Updated June 03, 2015 00:03 AM

Updated July 16, 2015 13:11 PM

Updated March 28, 2015 16:03 PM