Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By ayazbaloch on
Dear Drupal community,
Need help, pagination not reset after filtering table via ajax submitting. Guide me what I am doing wrong.
<?php
namespace Drupal\loanapp\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Database\Database;
use Symfony\Component\HttpFoundation\RedirectResponse;
// Use for Ajax.
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Ajax\AppendCommand;
use Drupal\Core\Ajax\HtmlCommand;
/**
*
* @package Drupal\loanapp\Form
*/
class ListForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'list_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#theme'] = 'ListForm_theme';
$form['cnic'] = [
'#type' => 'textfield',
'#title' => t('CNIC'),
'#attributes' => array('class' => array('form-control')),
];
$form['gender'] = [
'#type' => 'select',
'#title' => ('Gender'),
'#attributes' => array('class' => array('form-control')),
'#options' => array(
'Any' => t('- Any -'),
'Female' => t('Female'),
'Male' => t('Male'),
),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Search'),
// The AJAX handler will call our callback, and will replace whatever page
// element has id list-container.
'#ajax' => [
//'callback' => '::getLoanRows2',
'callback' => [$this, 'findLoans'],
'wrapper' => 'list-container',
],
];
// Create table header.
$header = [
'id' => $this->t('Id'),
'cnic' => $this->t('CNIC'),
'firstname' => $this->t('First Name'),
'lastname' => $this->t('Last Name'),
'gender'=> $this->t('Gender'),
];
$cnic = "";
$gender = "Any";
$form['table'] = [
'#id'=>'list-container',
'#type' => 'table',
'#header' => $header,
'#rows' => $this->getLoanRows($cnic,$gender),
'#empty' => $this->t('No records found'),
'#sticky'=>TRUE,
];
$form['pager'] = [
'#type' => 'pager'
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
}
private function getLoanRows($cnic,$gender){
$query = \Drupal::database()->select('loanapp', 'l')
->fields('l',['id','cnic','firstname','lastname','gender'])
->extend('Drupal\Core\Database\Query\TableSortExtender')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->limit(10);
if($cnic!=""){
$query->condition('l.cnic', $cnic,'=');
}
if($gender!="Any"){
$query->condition('l.gender', $gender,'=');
}
$results = $query->execute();
$rows = [];
foreach($results as $data){
if ($data->id != 0) {
$rows[$data->id] = array(
'id' =>$data->id,
'cnic' =>$data->cnic,
'firstname' => $data->firstname,
'lastname' => $data->lastname,
'gender' => $data->gender,
);
}
}
return $rows;
}
function findLoans(array &$form, FormStateInterface $form_state){
$field = $form_state->getValues();
$cnic = $field["cnic"];
$gender = $field["gender"];
$rows = [];
$rows =$this->getLoanRows($cnic,$gender);
$form['table']['#rows'] =$rows ;
return $form['table'];
}
}