简体   繁体   中英

Auto calculation grocery crud

I want to ask about function that can make auto calculation in grocery crud.

For example: I have some column a, b, and total_a_b. I have used function callback_column but its now work perfectly, callback_column just show the result on the view page and it can't auto save to database. What the function that can refer it? And whether there are a way to show that calculation automatic in field form?

This is my code:

public function try() {
    $crud = new grocery_CRUD();
    $crud->set_table('try');
    $crud->columns('a', 'b', 'total');
    $crud->callback_column('total', array($this, '_callback_column_total'));
    $output = $crud->render();
    $this->_example_output($output);
}

public function _callback_column_total ($value, $row) {
    $a = $row->a;
    $b = $row->b;
    $total=$a + $b;
    return $total;
}
  1. it cant auto save to database. What the function that can refer it?

    • First you need to make sure that, the this field is column in your table, then tell GroceryCrud that this field is available, but invisible in form.

    $crud->change_field_type('your_field', 'invisible');

    • Second (optional), you can use 2 callbacks: calback_before_update, calback_before_insert to validate or use PHP to handle the value of this field

API document can help: http://www.grocerycrud.com/documentation/options_functions

  1. And whether there are a way to show that calculation automatic in field form?

You can use javascript to add new element and handle the auto calculation. And remember that there should be a hidden input will post with your post request to controller of grocery crud. You need to make sure that the value will come with post request by var_dump the value of $post_data in callbacks: callback_before_insert or call_back_before_update.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM