| Project: | Ajax Table |
| Version: | 5.x-1.x-dev |
| Component: | Code |
| Category: | support request |
| Priority: | normal |
| Assigned: | enboig |
| Status: | active |
Issue Summary
I has needing to pass a variable to the table in order to construct the query. Imagine a scenario where you want to make a website where with just one table definition and menu_callback you can:
q=list/2007 list all the nodes in 2007
q=list/2007/9 list all the nodes in september 2007
q=list/2008 list all the nodes in 2008
...
Or inside a node list all the nodes from the node author, or....
I modified ajaxtable_render(... $options=array()) and then ajaxtable_table_mytable($options) can construct de query according to the parameters in "$options". It took me sometime to figure how to get ajaxtable_refresh() and autosearch work with it; but now it does.
I think it would be a good thing to add to your module permanently, I don't know how to make a patch, but I can send you de code so you can do so. I may help to add the documentation.
| Attachment | Size |
|---|---|
| ajaxtable.zip | 11.76 KB |
Comments
#1
I'm trying to eval the changes as I'm encountered by a similar requirement. Can you show what changes need to go in the code of the calling screen or menu.
Should setting of table['options'] in the table definition sufficient ?
#2
First of all I thought of using $override to pass variables, but I didn't like that, so I ended removing override and passing a $options array with the paramters, the table definition will construct its query depending of the options.
The changes I made to the code are:
* function ajaxtable_render($id,$uid='',$refresh=FALSE,$input_value='',$options=NULL) {
I needed some way of passing the variables to the table; I had to change some other lines of the module so the table definition is called with the $options, and the ajax uses it when request for a refresh.
*Menu system:
<? $items[] = array('path' => 'comptacau/proveidors/despeses',
'title' => t('Page title'),
'callback' => 'ajaxtable_render',
'callback arguments' => array(
"id" => "comptacau_taula_despeses",
"uid" => $user->uid,
"refresh" => false,
"input_value" => '',
"options" => array("supplier_nid" => arg(3)),
),
'access' => true,
'type' => MENU_CALLBACK);
?>
Here it receive a nid and passes it to the table, it will construct the query according to the nid. I didn't change the parameters order so it is full compatible with previous version.
If calling it inside a page it must be called with ajaxtable_render('my_table', $user->uid,FALSE, '', array('assentament_nid'=>$node->nid)
With my code you can list all nodes published by a used; all the nodes from a month, etc...
An example of a table definition is pasted below; I commented some lines of the provided examples, and I use SESSION variables also.
<?php
function ajaxtable_table_comptacau_taula_despeses($options=false) {
$search_inputs = array(
array(
),
);
// Get the inputs so they can be added to search
$inputs = ajaxtable_search_inputs($search_inputs);
// Alter the theme's search bar template to include the
// additional search inputs.
$theme['search'] = '
<table class="ajaxtable-default-search-table">'
.'<tr>'
.'<td class="ajaxtable-default-search-table-left"></td>'
.'<td>{search_help}</td><td>'.t('Search for').' {search_input} </td>'
.'<td>{search_button}</td>'
.'<td class="ajaxtable-default-search-table-right"></td>'
.'</tr>'
.'</table>'
.'';
// Change the text of the buttons
$theme['button_text'] = array(
'search' => t('Search'),
'jump' => t('Jump to page'),
'next' => '>>',
'prev' => '<<',
);
$table = array(
'theme_override' => $theme,
'rows' => 20,
'edit' => FALSE,
'options'=>$options,
'return_url' => 'comptacau/despeses',
'access' => true,
'theme_module' => 'comptacau',
'pages' => 15,
'theme' => 'default',
'search_inputs' => $search_inputs,
'autosearch' => TRUE,
'empty' => '<p>'.t("Cap resultat per aquesta llista.").'</p>', //trans
'query' => "SELECT node.nid AS nid, d.nid AS assentament_nid, node.title AS title, a.import, a.data, a.project_nid AS project_nid, project.title AS project_txt, (a.import) AS pendent, supplier.title AS supplier_txt, SUM(m.import) AS pagat FROM {comptacau_despeses} d
INNER JOIN {node} node ON d.nid = node.nid
INNER JOIN {comptacau_assentaments} a ON a.nid = d.nid
INNER JOIN {wu_nodes} wu ON d.nid = wu.nid
LEFT JOIN {node} project ON a.project_nid = project.nid
LEFT JOIN {node} supplier ON d.supplier_nid = supplier.nid
LEFT JOIN {comptacau_moviments} m ON m.assentament_nid = d.nid
WHERE a.data >='".$_SESSION['comptacau_exercici']."-09-01' AND a.data <='".($_SESSION['comptacau_exercici']+1)."-08-31' "
.($_SESSION['working_unit_active']>0 ? " AND wu.wu_ref=".sprintf("%d",$_SESSION['working_unit_active']) : "" )
.($options['supplier_nid']>0 ? " AND d.supplier_nid = ". sprintf("%d",$options['supplier_nid']): "")
." GROUP BY a.nid"
,
'columns' => array(
array(
'col' => 'node.nid',
'label' => 'nid',
'sortable' => FALSE,
'searchable' => FALSE,
'default_sort' => 'desc',
'help' => 'The node ID, for uniquely identifying nodes.',
'hide' => TRUE,
),
array(
'col' => 'a.project_nid',
'label' => 'project_nid',
'sortable' => FALSE,
'alias' => 'project_nid',
'searchable' => FALSE,
'default_sort' => 'desc',
'help' => 'Nid del projecte.',
'hide' => TRUE,
),
array(
'col' => 'node.title',
'label' => 'Title',
'alias' => 'title',
'sortable' => TRUE,
'searchable' => TRUE,
'help' => t('Concept'),
'callback' => 'ajaxtable_link_al_node',
),
array(
'col' => 'project.title',
'label' => t('Project'), //trans
'sortable' => TRUE,
'searchable' => TRUE,
'alias' => 'project_txt',
'callback' => 'ajaxtable_link_al_projecte',
'help' => t('Project name'),
),
array(
'col' => 'supplier.title',
'label' => t('Supplier'), //trans
'alias' => 'supplier_txt',
'sortable' => TRUE,
'searchable' => TRUE,
// 'callback' => 'ajaxtable_link_al_projecte',
'help' => t('Supplier name'),
),
array(
'col' => 'a.import',
'label' => t('Cost'), //trans
'sortable' => TRUE,
'searchable' => TRUE,
'help' => t('import_sortida de l\'despesa'), //trans
'callback' => 'ajaxtable_nombres_vermells',
),
array(
'label' => t('Paid'),
'sortable' => TRUE,
'searchable' => FALSE,
'callback' => 'ajaxtable_sumar_moviments',
'help' => t('import_sortida pagat'), //trans
),
array(
'col' => 'a.data',
'label' => t('Date'),
'sortable' => TRUE,
'searchable' => TRUE,
'help' => t('Data de l\'despesa'), //trans
'callback' => 'ajaxtable_format_date',
),
),
);
return $table;
}
?>
I am still working in this table, but it works right now.
#3
This is Ashok ,
i am tested the following file
ajaxtable.zip 11.76 KB
when i am used this default document table given in render(ajaxtable_example1....example7) are works fine , but
searching and sorting works fine , but if ia have copied it and add this in body tag and called it in render doesnt works ,
that means sorting , searching returns empty.
In body
print ajaxtable_render("ajaxtable_example2");
works fine
but following not works
function ajaxtable_table_ajaxtable_ex2($override='') {
$search_inputs = array(
array(
'type' => 'text',
'id' => 'is_like_title',
'operator' => 'like',
'help_title' => 'Like',
'help' => 'Search the node titles matching this phrase',
'col' => 'title',
'attributes' => array(
'size' => '5',
),
),
array(
'type' => 'text',
'id' => 'is_not_like_title',
'operator' => 'not like',
'help_title' => 'Not like',
'help' => 'Search the node titles that do not match this phrase',
'col' => 'title',
'attributes' => array(
'size' => '5',
),
),
);
// Get the inputs so they can be added to search
$inputs = ajaxtable_search_inputs($search_inputs);
// Alter the theme's search bar template to include the
// additional search inputs.
$theme['search'] = '
';
// Change the text of the buttons
$theme['button_text'] = array(
'search' => 'Search',
'jump' => 'Jump to page',
'next' => '>>',
'prev' => '<<',
);
$table = array(
'theme_override' => $theme,
'rows' => 10,
'theme_module' => 'ajaxtable',
'pages' => 15,
'theme' => 'default',
'search_inputs' => $search_inputs,
'autosearch' => TRUE,
'empty' => '
Nothing matched your query, sorry!
',
'query' => '
SELECT * FROM {node}
',
'columns' => array(
array(
'col' => 'nid',
'label' => 'nid',
'sortable' => TRUE,
'searchable' => TRUE,
'default_sort' => 'desc',
'help' => 'The node ID, for uniquely identifying nodes.',
),
array(
'col' => 'title',
'label' => 'Title',
'sortable' => TRUE,
'searchable' => TRUE,
'help' => 'The title of the node',
),
),
);
ajaxtable_override($table,$override);
return $table;
}
print ajaxtable_render("ajaxtable_ex2");
please help me