How to use checked elements in the list for an action ?

A place for users and developers of the Xataface to discuss and receive support.

How to use checked elements in the list for an action ?

Postby Jean » Mon May 26, 2008 6:47 am

Hi Steve,
How would you add a customized action which took into account the items checked in the list view ?
My problem is how to get the checkbox values into my action.
Thank you
Jean
Jean
 
Posts: 259
Joined: Wed Nov 07, 2007 1:30 am
Location: Pau, France

Postby shannah » Mon May 26, 2008 12:16 pm

Hi Jean,

This is a 3 part process:

1. Add an entry in the actions.ini file.
2. Create a javascript function for pass parameters to the PHP action.
3. Create a custom action (PHP) to do the work.

As an example we will consider the update selected action. The actions.ini file entry looks like:

Code: Select all
[update_selected]
   url="javascript:updateSelected('result_list')"
   label="Update"
   description="Update selected records"
   category=selected_result_actions
   permission=edit
   icon="{$dataface_url}/images/edit.gif"



Key things to notice here:
1. The url refers to a javascript function, which takes a single parameter referring to the result list.

2. The category is 'selected_result_actions'. This is what causes the button to appear along the bottom of the result list.


Now the javascript function looks like:
Code: Select all
function updateSelected(tableid){
   var ids = getSelectedIds(tableid);
   if ( ids.length == 0 ){
      alert("Please first check boxes beside the records you wish to copy, and then press 'Copy'.");
      return;
   }
   var form = document.getElementById("result_list_selected_items_form");
   form.elements['--selected-ids'].value = ids.join("\n");
   form.elements['-action'].value = 'copy_replace';

   form.submit();
}


Things to notice here:

1. It takes a single parameter: tableid which refers to an html table (which is passed in the call to our function in the url attribute in the actions.ini file.

2. We obtain the selected record ids by calling the getSelectedIds() function (part of the xataface distribution).

3. We obtain a reference to the select list form and append an element for the selected ids (to pass to the PHP action). We also set the '-action' element of the form to name our PHP action that we will create.

4. Then we submit the form.

Finally, let's look at the PHP action. It basically needs to obtain Dataface_Record objects corresponding to the selected ids, and do something....

Code: Select all
class dataface_actions_copy_replace {
   
....
   function handle(&$params){
      $app =& Dataface_Application::getInstance();
      $query =& $app->getQuery();
      $table =& Dataface_Table::loadTable($query['-table']);
      
      
      $records = df_get_selected_records($query);

    etc ....





Notice the use of the df_get_selected_records() function to obtain an array of records. From here you can do what you like with the action.

Hope this makes sense. Let me know if you require clarification.

-Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Postby Jean » Tue May 27, 2008 5:37 am

Thank you Steve for your patience and your availability.
Jean :)
Jean
 
Posts: 259
Joined: Wed Nov 07, 2007 1:30 am
Location: Pau, France

Postby Jean » Mon Jun 02, 2008 6:51 am

Hi Steve,
I have dealt with this situation completing your javascript to create a new field dynamically. All this to grab a variable and know if the added button have been clicked on.

Here I create a hidden input field called bouton with the value of 1.

Then in the copy_replace.php action file, I add a if block and can do what I want with it.

Here is the javascript code
Code: Select all
// JavaScript Document
function commandSelected(tableid){
   var ids = getSelectedIds(tableid);
   if ( ids.length == 0 ){
      alert("SVP, veuillez sélectionner des cases à cocher avant de cliquer sur 'Commander'.");
      return;
   }
   var form = document.getElementById("result_list_selected_items_form");
   form.elements['--selected-ids'].value = ids.join("\n");
   form.elements['-action'].value = 'copy_replace';
   //add a form element dynamically to mean it is not to delete selected items
  var newinput = document.createElement('input');
  newinput.setAttribute('name','bouton');
  newinput.setAttribute('id','bouton');
  newinput.setAttribute('value','1');
  newinput.setAttribute('type','hidden');
  form.appendChild(newinput) ;
   form.submit();
}

Hope this will help.
Jean
Jean
 
Posts: 259
Joined: Wed Nov 07, 2007 1:30 am
Location: Pau, France

Postby shannah » Mon Jun 02, 2008 8:30 am

Thanks Jean.

It is nice to have a few examples in this area as there aren't too many examples available yet on actions - especially of this type.

-Steve
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Problems with selected records

Postby fantomasdm » Tue May 19, 2009 1:29 am

Sorry for open an old thread..
I'm using xataface ver:1.1.5

Hi I have white this action.ini
Code: Select all
[stampa]
url = "{$this->url('-action=stampa')}"
template=chiudi.html
permission=view
mode = details
order=6
#category=table_actions
category=selected_result_actions
icon=print_ico.jpg


and this file (named:stampa.php)
Code: Select all
class actions_stampa {
  function handle(&$params)
    {
      $app =& Dataface_Application::getInstance();  // reference to Dataface_Application objectr
      //$current_record =& $app->getRecord();  // Currently selected record (Dataface_Record object)

        $query =& $app->getQuery();
        $records = df_get_selected_records($query);

      print_r ($query);
      print_r ($records);
        }
}



But array $records is always empty!! Where is mistake???
Sorry for my bad English!!
fantomasdm
 
Posts: 114
Joined: Thu Mar 13, 2008 2:35 pm

Postby fantomasdm » Wed May 20, 2009 12:57 am

Sorry for my poor ItalianEnglish.......

I find error...I have to remove url command on action.ini

with:
Code: Select all
[stampa]
#url = "{$this->url('-action=stampa')}"
template=chiudi.html
permission=view
mode = details
order=6
#category=table_actions
category=selected_result_actions
icon=print_ico.jpg


All it works!!
fantomasdm
 
Posts: 114
Joined: Thu Mar 13, 2008 2:35 pm

Postby shannah » Tue Jun 02, 2009 10:31 am

Just a note, in INI files you comment out with a semi colon, not a # sign.

e.g.
Code: Select all
#url = "{$this->url('-action=stampa')}"

should be
Code: Select all
;url = "{$this->url('-action=stampa')}"


Your solution worked only because it changed the 'url' parameter to be named '#url' instead.
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re:

Postby fabien_ecl » Wed Nov 10, 2010 6:40 am

shannah wrote:Hi Jean,

This is a 3 part process:

1. Add an entry in the actions.ini file.
2. Create a javascript function for pass parameters to the PHP action.
3. Create a custom action (PHP) to do the work.

...............CUT here by fabien_ecl-------------

Now the javascript function looks like:
Code: Select all
function updateSelected(tableid){
   var ids = getSelectedIds(tableid);
   if ( ids.length == 0 ){
      alert("Please first check boxes beside the records you wish to copy, and then press 'Copy'.");
      return;
   }
   var form = document.getElementById("result_list_selected_items_form");
   form.elements['--selected-ids'].value = ids.join("\n");
   form.elements['-action'].value = 'copy_replace';

   form.submit();
}


Things to notice here:

1. It takes a single parameter: tableid which refers to an html table (which is passed in the call to our function in the url attribute in the actions.ini file.

2. We obtain the selected record ids by calling the getSelectedIds() function (part of the xataface distribution).

3. We obtain a reference to the select list form and append an element for the selected ids (to pass to the PHP action). We also set the '-action' element of the form to name our PHP action that we will create.

4. Then we submit the form.

-Steve


I reanimate this post because I want to do the same thing as described here but beeing a noob in javascript, I don't find in documentation where you save your javascript : in a specific java file (but then how the xataface knows how to find it?), in the php code of the page (I tried but it didn't work), in the source file with the other functions such as updateSelected (it will disturb me a little)?
I'm sorry if it's a quite basic question but I really can't find the answer by myself :oops:
fabien_ecl
 
Posts: 9
Joined: Wed Nov 10, 2010 6:14 am

Re: How to use checked elements in the list for an action ?

Postby shannah » Wed Nov 10, 2010 7:13 am

Javascript can be included anywhere on the page. Generally you would implement a block or a slot to add the necessary script tags.

e.g. in the application delegate class you might use the curstom_javascripts block:
Code: Select all
function block__custom_javascripts(){
    echo '<script src="/path/to/my/javascript.js"></script>';
}
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: How to use checked elements in the list for an action ?

Postby fabien_ecl » Wed Nov 10, 2010 11:18 pm

Thanx.
I must be a little clumsy but it doesn't work.
code of the javascript calculate.js located in my the repertory of my table named "Annee_2010"
Code: Select all
function calculateSelected(tableid){
   var ids = getSelectedIds(tableid);
   if ( ids.length == 0 ){
      alert("Veuillez sélectionner au moins une ligne");
      return;
   }
   var form = document.getElementById("result_list_selected_items_form");
   form.elements['--selected-ids'].value = ids.join("\n");
   form.elements['-action'].value = 'calculate';
   form.submit();
}


Code of my actions.ini
Code: Select all
[calculate_selected]
   url="javascript:calculateSelected('result_list')"
   label="Calcul"
   description="Calcul sur les entrées sélectionnées"
   category=selected_result_actions
   permission=edit
   icon="{$dataface_url}/images/calc.gif"


The code of Annee_2010.php
Code: Select all
<?
class tables_Annee_2010{
function block__custom_javascripts(){
    echo '<script src="javascripts.js" type="text/javascript" language="javascript"></script>';
}
    /**
     * Trigger that is called just after new record or updated record.
     * @param $record Dataface_Record object that has just been inserted.
     */
    //function afterSave(&$record){
//Générer le coût des heures
//UPDATE  ``Annee_2010`  SET  `Durée`  =  '4:30:00' WHERE  `Annee_2010`.`iid`  =3;
   // }
}
?>


And I have a file named calculate.php in the actions repertory of my site. It works if I use the following actions.ini
Code: Select all
[calculate_selected]
   label="Calcul"
   description="Calcul sur les entrées sélectionnées"
   category=selected_result_actions
   permission=edit
   icon="{$dataface_url}/images/calc.gif"

Is there a big mistake i haven't seen or something I don't understand in the way xatafce works?
fabien_ecl
 
Posts: 9
Joined: Wed Nov 10, 2010 6:14 am


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 42 guests

cron
Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved