Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 132

Přidáno uživatelem Tomáš Dulík před asi 16 roky(ů)

Přidána nová komponenta libraries/Table_Form.php (s views/table_form.php) pro snadnější realizaci formulářů s tabulkovým formátováním, které nepotřebují validaci - typický příklad použití jsou filtry u výpisu s Grid-em - viz funkce show_all v kontroleru users.php

Zobrazit rozdíly:

freenetis/trunk/kohana/application/controllers/users.php
unset($url_array);
$sql_offset = ($sql_offset>$total_users) ? 0 : $sql_offset;
if ($this->input->post()) {
$dotaz=$model_users->like(array(
"name" =>$this->input->post("filter_name"),
"surname" =>$this->input->post("filter_surname")
)) -> orderby($order_by,$order_by_direction)
-> limit($limit_results,$sql_offset)
-> find_all();
} else
$dotaz = $model_users->orderby($order_by,$order_by_direction)->limit($limit_results,$sql_offset)->find_all(); // get all members from database
$filter=new Table_Form("get", array(
"name"=>"Name",
"surname"=>"Surname",
"tr",
"email"=>"Email",
"phone"=>"Phone",
"tr",
"login"=>"Login name",
"td",
"submit"=>"Filter"
)
);
$dotaz=$model_users
-> like($filter->values())
-> orderby($order_by,$order_by_direction)
-> limit($limit_results,$sql_offset)
-> find_all();
$grid = new Grid(url_lang::base().'users', url_lang::lang('texts.List of all users'),array(
'separator' => '',
//'use_paginator' => false,
......
$view = new View('template');
$view->header = new View('base/header');
$filter = new View('users_filter');
$view->content = $filter . $grid;
$view->footer = new View('base/footer');
$view->content = $filter->view . $grid;
$view->footer = new View('base/footer');
$view->header->menu = Controller::render_menu();
$view->header->title = url_lang::lang('texts.List of all users');
//$view->content->heading = "Úprava uživatele ".$data['row']['name']." ".$data['row']['surname'];
freenetis/trunk/kohana/application/libraries/Table_Form.php
<?php
class Table_Form {
protected $method, $form_def, $form_val;
public $view;
public function __construct($method, $form_def, $view=NULL) {
if (isset($view)) $this->view=$view;
else $this->view=new View('table_form');
$this->view->form_def=$this->form_def=$form_def;
$this->view->method=$this->method=$method;
$this->view->form_val=$this->form_val=$this->values();
}
public function values() {
$input=Kohana::$instance->input;
$method=$this->method;
if (isset($this->form_val)) return $this->form_val;
else {
$values=array();
if ($input->$method("submit", true)) // if the form was submitted
foreach (array_keys($this->form_def) as $key) // check all its input fields
if (!is_int($key) && $key!="submit") // ignore special fields
if (($getval=$input->$method($key, true))!="")
$values[$key]=$getval; // and store them into array as field=>values pairs
return $this->form_val=$values;
}
}
}
?>
freenetis/trunk/kohana/application/views/table_form.php
<?php
/**
* Tableform view is a form rendered using HTML table
* @author Tomas Dulik
* @version 0.9 beta
*
* Input data are in $form_def and $form_values variables
* $form_def is an array('field1'=>'label1', 'field2'=>'label2', ...)
* where
* 'fieldX' is the value for the "name" attribute of a field in HTML form
* 'labelX' is the name of label attached to the fieldX by the <label> tag
* Aside names for form fields, the "fieldX" without 'label' assigned will just render
* its content to the HTML, but in such case, 'fieldX' can also have following special values:
* 'submit' - will rended a submit button
* 'tr' - will render a new table row, e.g. as </tr><tr>
* 'td' - will render an empty table cell, e.g. as </td><td>
* $form_values is an array('field1'=>'value1', 'field2'=>'value2', ...)
* which contains the initial values for the form fields
*
*/
echo form::open(NULL, array('method'=>'get')) . "\n<table><tr>\n";
foreach ($form_def as $field=>$label) {
if (is_int($field))
switch ($label) {
case "tr":
echo "</tr><tr>\n"; break;
case "td":
echo " <td></td>\n"; break;
default:
echo $label;
} else {
switch ($field) {
case "submit":
echo " <td><input name='submit' type='submit' value='"
.url_lang::lang("texts.$label")."'/></td>\n";
break;
/**
* case "input":
* here you can put implementation of other field types, like
* selections, check buttons etc.
*/
default:
if (!isset($form_val[$field])) $val="";
else $val=$form_val[$field];
echo " <td><label for='$field'>".url_lang::lang("texts.$label").":</label></td>\n"
." <td><input type='text' name='$field' value='$val'/></td>\n";
}
}
}
?>
</tr></table>
</form>

Také k dispozici: Unified diff