0

I have a form element below:

//Calls a function that contains the html for templates table.
$form['myid_templates_editor_table_wrapper'] = array(
    '#type' => 'item',
    '#markup' => myid_create_templates_table(),   
);

The function below, populates the element above with the HTML of a table.

function myid_create_templates_table(){    

    //Retrieve list of templates in the database
    $query = db_select('studidtemplates', 't1',array('target' => 'import'));

    $result = $query
        ->fields('t1', array('template_id', 'template_name', 'date_created', 'user_name'))
        ->orderBY('template_id', 'DESC')
        ->execute();

    $table_header = array(
        'template_id' => array('data' => 'Template ID' , 'field' => 'template_id','sort' => 'asc'), 
        'template_name' => array('data' => t('Template Name'), 'field' => 'template_name','sort' => 'asc'),  
        'date_created' => array('data' => 'Date Created' , 'field' => 'date_created','sort' => 'asc'), 
        'user_name' => array('data' => t('Created by'), 'field' => 'user_name','sort' => 'asc'),  
    );

    foreach ($result as $record) {          
        $table_rows[] = array(l(t($record->template_id), $record->template_id, array('attributes' => array('class' => 'use-ajax'))), $record->template_name, $record->date_created, $record->user_name);
    }       
    return '<div id="myid_templates_editor_table_wrapper">' . theme('table', array('header' => $table_header, 'rows' => $table_rows)) . '</div>';
}

It looks like the image below:

enter image description here

Whenever a new record is inserted to the table, the function below renders the $form['myid_templates_editor_table_wrapper'] element to update it's table display.

function myid_save_templates_editor($form, &$form_state){
    global $user; 

    $save = db_insert('studidtemplates',array('target' => 'import')) 
        ->fields(array(         
            'template_name' => 'WEW',            
            'date_created' => date("Y-m-d H:i:s", time()),
            'user_name' => $user->name
        ))       
        ->execute();

    return array(
        '#type' => 'ajax',
        '#commands' => array(            
            ajax_command_replace('#myid_templates_editor_table_wrapper', render($form['myid_templates_editor_table_wrapper'])),  

        )
    ); 
}

I notice something weird about it. When my actual record in the database is 1, the table displays 0 records. When my actual record is 2, my table displays 1 record. When my actual record is 3, it displays 2 records in my table and so on and so forth.

I was suspecting that my $form['myid_templates_editor_table_wrapper'] element was being rendered, even if db_insert has not yet successfully inserted a record in my database.

Now, the question is : How will I execute the rendering of my form element after db_insert had successfully inserted a record in my table?

UPDATE 1:

I tried Shabir A. 's suggestion:

$save = db_insert('studidtemplates',array('target' => 'import')) 
    ->fields(array(         
        'template_name' => 'WEW',            
        'date_created' => date("Y-m-d H:i:s", time()),
        'user_name' => $user->name
    ))       
    ->execute();

watchdog('myid', $save);

if($save){
    return array(
        '#type' => 'ajax',
        '#commands' => array(
            ajax_command_replace('#myid_templates_editor_table_wrapper', render($form['myid_templates_editor_table_wrapper'])),                         
        )
    );  
}

But the problem still occurs, When I view my watchdog table, the $save variable has a value of 371. That means it was inserted successfully, as what Shabir A. said, but the table display was not updated. Is there anything wrong with my query? Where am I missing?

1
  • Could you please share your complete form also if possible ur complete rendered form? Commented Mar 22, 2016 at 5:20

4 Answers 4

0
+50

I have fixed your issue locally. Actually Issue is that, you have to render the tablemarkup, instead of the form element.

$markup_table = demo_pane_get_markup_table();
ajax_command_replace('#myid_templates_editor_table_wrapper', render($markup_table))

I am attaching the whole solution, I have created a form, with 2 fields (markup and submit button), on clicking of submit, markup of table refreshes up.

function demo_pane_form($form, &$form_state) {
  $form['myid_templates_editor_table_wrapper'] = array(
    '#type' => 'item',
    '#markup' => demo_pane_get_markup_table(),
  );

  $form['custom_submit'] = array(
    '#type' => 'submit',
    '#value' => 'Click',
    '#ajax' => array(
      'callback' => 'demo_pane_ajax_insert_row',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );

  return $form;
}

function demo_pane_ajax_insert_row($form, &$form_state) {
  $tags_state = variable_get('tags_state', '');
  if(empty($tags_state)) {
    variable_set('tags_state', 4);
    $state = 4;
  } else {
    $state = $tags_state + 1;
    variable_set('tags_state', $state);
  }
  $save = db_insert('field_data_field_tags')
          ->fields(array(
            'entity_type' => 'node',
            'bundle' => 'article',
            'deleted' => 0,
            'entity_id' => $state,
            'revision_id' => $state,
            'language' => 'und',
            'delta' => 0,
            'field_tags_tid' => 1,
          ))
          ->execute();

  watchdog('Table Entry', print_r($save, TRUE));

  if($save) {

    $markup_table = demo_pane_get_markup_table();
    watchdog('Table Entry', print_r($markup_table, TRUE));

    return array(
        '#type' => 'ajax',
        '#commands' => array(
            ajax_command_replace('#myid_templates_editor_table_wrapper', render($markup_table)),
      )
    );
  }
}

function demo_pane_get_markup_table() {
  $query = db_select('field_data_field_tags', 't1');
  $result = $query
            ->fields('t1', array('entity_type', 'bundle', 'entity_id', 'field_tags_tid'))
            ->orderBY('field_tags_tid', 'DESC')
            ->execute();
  $table_header = array(
        'entity_type' => array('data' => 'Entity Type' , 'field' => 'entity_type','sort' => 'asc'),
        'bundle' => array('data' => 'Bundle', 'field' => 'bundle','sort' => 'asc'),
        'entity_id' => array('data' => 'Entity ID', 'field' => 'entity_id','sort' => 'asc'),
        'field_tags_tid' => array('data' => 'Tags TID', 'field' => 'field_tags_tid','sort' => 'asc'),
    );

    foreach ($result as $record) {

      $table_rows[] = array(
        l(
          t($record->entity_type),
          'node/'.$record->entity_id,
          array('attributes' => array('class' => 'use-ajax'))
        ),
        $record->bundle,
        $record->entity_id,
        $record->field_tags_tid
      );

    }
    return '<div id="myid_templates_editor_table_wrapper">' . theme('table', array('header' => $table_header, 'rows' => $table_rows)) . '</div>';
}

Tell me if you have any query for the same.

1
  • That was easy but I never though of it. Thanks.
    – user46175
    Commented Mar 27, 2016 at 5:51
0

You can simply check against inserted id. If insert successful means you get inserted id only then you render the data otherwise do nothing

$inserted_id = db_insert('studidtemplates',array('target' => 'import')) 
        ->fields(array(         
            'template_name' => 'WEW',            
            'date_created' => date("Y-m-d H:i:s", time()),
            'user_name' => $user->name
        ))       
        ->execute();
if($inserted_id) {
   return array(
    '#type' => 'ajax',
    '#commands' => array(            
        ajax_command_replace('#myid_templates_editor_table_wrapper',     render($form['myid_templates_editor_table_wrapper'])),  

    )
); 
}
1
  • See my update above.
    – user46175
    Commented Jan 14, 2016 at 0:59
0

Check if an offset of 1 is being set in the table. I do not know how to check it technically but it could be somewhere in the theme() function that you are using.

May be its considering the 1st row of data as the table header due to some missing setting.

Hope it throws some light on the way to solution.

Regards,

Supriya Rajgopal

0

You're creating a new record and then querying for it, so one possibility is that either the page cache, or other caching that you may have configured is showing you stale information.

Your Answer

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