0

I have two elements in my form: a select and a table element. Everytime onchange event is triggered on select element, I want to update the values in my table. I want to do query and change the values of '#rows' in the table.

function myid_print_user_page_form($form, &$form_state)
{
    $form = array();
    $form['myid_print']['option'] = array(
        '#type' => 'select',        
        '#options' => array(
            0 => t('All'),
            1 => t("Printed ID's"),
            2 => t("Unprinted ID's"),
        ),
        '#default_value' => 0,             
        '#ajax' => array(
            'callback' => 'myid_print_all_submitted_id_callback',
            'wrapper' => 'myid_table_wrapper',
        ),                             
    );

    // Build the sortable table header.
    $header = array(
        'id_number' => array('data' => t('ID Number'), 'field' => 'id_number','sort' => 'asc'),     
        'student' => array('data' => t('Student'), 'field' => 'student', 'sort' => 'asc')
    );

    //Get the node data. 

    $query = db_select('student', 't1',array('target' => 'import'));
    $result = $query
        ->fields('t1', array('studid','lastname', 'firstname', 'middlename', 'extname'))   
        ->condition('t1.id_status',1,'=')
        ->distinct()
        ->execute();

    //Build the rows.
    $rows = array();  
    foreach($result as $record){
        $rows[$record->studid] = array(
            'id_number' => $record->studid,
            'student' => $record->firstname . ' ' . $record->middlename . ' ' . $record->lastname . ' ' . $record->extname,    
        );
    }

    //Build the table 
    $form['table'] = array(
        '#theme' => 'table',
        '#header' => $header,
        '#rows' => $rows,
        '#prefix' => '<div id="myid_table_wrapper">',
        '#suffix' => '</div>',
    );
    return $form;
}   

I'm stuck here, I don't know what to put in my callback function.

function myid_print_all_submitted_id_callback($form, $form_state)
{
    //Do query here and put the query results in the table.
}

Any help would be very much appreciated.

1 Answer 1

0

You can access the values of the form elements using the $form_state parameter. So when you have the value of the select element, you can perform the query as required, build the table using theme_table and return the result. The returned result can be replaced or appended to the ajax wrapper defined in your form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.