我尝试开发一个自定义模块,它有两个日期弹出窗口" start_date"和" end_date",我有数据库表,其中包含Id,名称,付费日期,付费金额。我需要获取数据库并在按钮上显示为表格。我在drupal中写了一些代码,如果错误请更正并请指导我获取特定日期范围的表格 form design
我的编码:
Uniview8PastpaymentsController.php
<?php
namespace Drupal\uniview8_pastpayments\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\uniview8_pastpayments\Uniview8PastPaymentsStorage;
class Uniview8PastpaymentsController extends ControllerBase {
public function entryList() {
$content = array();
$content['message'] = array(
'#markup' => $this->t('Pastpayment history for selected date ranges.'),
);
$rows = array();
$headers = array(t('Id'), t('Name'), t('Paid Date'), t('Paid Amount'));
foreach ($entries = Uniview8PastPaymentsStorage::load() as $entry) {
$rows[] = array_map('Drupal\Component\Utility\SafeMarkup::checkPlain', (array) $entry);
}
$content['table'] = array(
'#type' => 'table',
'#header' => $headers,
'#rows' => $rows,
'#empty' => t('No entries available.'),
);
$content['#cache']['max-age'] = 0;
return $content;
}
}
表格 uniview8_pastpayments.php
<?php
namespace Drupal\uniview8_pastpayments\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class uniview8_pastpayments extends FormBase {
public function getFormId() {
return 'uniview8_pastpayments';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['set_start_date'] = [
'#title' => $this->t('START DATE '),
'#type' => 'date',
'#attributes' => array('type'=> 'date', 'min'=> '-12 months', 'max' => 'now' ),
'#date_date_format' => 'd/m/Y',
];
$form['set_end_date'] = [
'#title' => $this->t('END DATE'),
'#type' => 'date',
'#attributes' => array('type'=> 'date', 'min'=> '-12 months', 'max' => 'now' ),
'#date_date_format' => 'd/m/Y',
];
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Fetch'),
'#button_type' => 'primary',
);
return $form;
}
}
我如何使用提交按钮
获取数据库
**DB_select Function
Uniview8PastPaymentsStorage.php**
<?php
namespace Drupal\uniview8_pastpayments;
class Uniview8PastPaymentsStorage {
public static function load($entry = array()) {
$select = db_select('pastpayments', 'example');
$select->fields('example')
$select->condition('paiddate',$entry['set_start_date'], '>=')
$select->condition('paiddate',$entry['set_end_date'], '>=')
$select->execute();
foreach ($entry as $field => $value) {
$select->condition($field, $value);
}
// Return the result in object format.
return $select->execute()->fetchAll();
}
}
答案 0 :(得分:0)
如果您的表单显示在控制器页面上,那么您必须在表单提交中使用ajax 像这样
<?php
namespace Drupal\uniview8_pastpayments\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ChangedCommand;
use Drupal\Core\Ajax\CssCommand;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Ajax\InvokeCommand;
class uniview8_pastpayments extends FormBase {
public function getFormId() {
return 'uniview8_pastpayments';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['set_start_date'] = [
'#title' => $this->t('START DATE '),
'#type' => 'date',
'#attributes' => array('type'=> 'date', 'min'=> '-12 months', 'max' => 'now' ),
'#date_date_format' => 'd/m/Y',
];
$form['set_end_date'] = [
'#title' => $this->t('END DATE'),
'#type' => 'date',
'#attributes' => array('type'=> 'date', 'min'=> '-12 months', 'max' => 'now' ),
'#date_date_format' => 'd/m/Y',
];
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this->t('Fetch'),
'#ajax' => [
//'callback' => 'Drupal\mcal_textmining\Form\CreateReportsForm::submitCreateReport',
'callback' => array($this, 'fetchResult'),
'event' => 'click',
'progress' => array(
'type' => 'throbber',
'message' => t('Submitting...')
)
]
);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function fetchResult(array &$form, FormStateInterface $form_state) {
$maxDate = $form_state->getValue('set_start_date');
$minDate = $form_state->getValue('set_end_date');
$connection = Database::getConnection();
$single = $connection->select('pastpayments', 'x')
->fields('x', array('Id','name','paiddate','paidamount'))
->condition('paiddate', $maxDate, "<=")
->condition('paiddate', $minDate, ">=");
$executed = $single->execute();
$results = $executed->fetchAssoc();
foreach($results as $result){
/*
creat html table's tbody with result
like
$tbody .= "<tr><td>paidamount</td></tr>"
*/
}
// now append the result into the table as tbody
// with drupal ajax
$response = new AjaxResponse();
$response->addCommand(new HtmlCommand('tbodyID', $tbody));
return $response;
}
}
但如果您的表单位于另一个页面,并且在提交表单后将用户重定向到您的控制器页面,则将startdate和enddate作为参数发送到控制器,然后从您的控制器进行数据库查询
然后将结果附加到页面中 感谢