Collecting total prices on a recipe form using field collections, JQuery / AHAH
(function($) {
Drupal.behaviors.recipesForm = {
attach: function (context, settings) {
$(".field-name-field-recipe-quantity input[type=text]").focus(function() {
$ingredient = $(this).parent().parent().parent().prev().find("select");
nid = $ingredient.find('option:selected').val();
//console.log(nid);
$.get('/ingredient/price/get/' + nid , null, updateCost);
});
$(".field-name-field-recipe-quantity input[type=text]").blur(function() {
total_cost = 0;
$quantity = $(this);
quantity_val = $(this).val();
if (quantity_val && cost_per_kg) {
var item = $(".field-name-field-recipe-cost");
$cost = $(this).parent().parent().parent().parent().find(item).find("input[type=text]");
cost_val = quantity_val * cost_per_kg;
//console.log($quantity_val);
//console.log($cost_per_kg);
//console.log($cost_val.toFixed(2));
$cost.val(cost_val.toFixed(2));
}
$('.field-name-field-recipe-cost').each(function() {
//console.log(this);
//console.log($(this).find("input[type=text]"));
//console.log($(this).find("input[type=text]").val());
cost = $(this).find("input[type=text]").val();
total_cost = total_cost + parseFloat(cost);
});
//console.log(total_cost);
$total_cost = $('.field-name-field-total-cost').find("input[type=text]");
$total_cost.val(total_cost.toFixed(2));
});
$(".field-name-field-recipe-ingredient select").change(function() {
nid = $(this).find('option:selected').val();
$.get('/ingredient/price/get/' + nid , null, updateCost);
var item = $(".field-name-field-recipe-quantity");
$quantity = $(this).parent().parent().parent().find(item).find("input[type=text]");
//console.log($quantity);
$quantity.val(0);
var item = $(".field-name-field-recipe-cost");
$cost = $(this).parent().parent().parent().find(item).find("input[type=text]");
$cost.val(0);
});
}
};
var updateCost = function(response) {
cost_per_kg = response['data'];
//console.log(cost_per_kg);
}
})(jQuery);
/**
* Implements hook_menu().
*/
function mymodule_menu() {
$items['ingredient/price/get'] = array(
'page callback' => 'mymodule_get_ingredient_price_ajax',
'type' => MENU_CALLBACK,
'access arguments' => array('access content'),
);
return $items;
}
/**
* Callback to return JSON encoded ingredient price for given nid.
*/
function mymodule_get_ingredient_price_ajax($nid) {
$node = node_load($nid);
//print_r($node->field_cost_per_kg['und'][0]['value']);
$cost_per_kg = $node->field_cost_per_kg['und'][0]['value'];
drupal_json_output(array('status' => 0, 'data' => $cost_per_kg));
drupal_exit();
}
function mymodule_form_recipe_sheet_node_form_alter(&$form, &$form_state, $form_id) {
//dsm($form_id);
//dsm($form['field_total_cost']);
foreach ($form['field_collection_ingredients']['und'] as $key => &$value) {
if (is_numeric($key)) {
//dsm($key);
//dsm($value);
$value['field_recipe_cost']["#disabled"] = TRUE;
}
}
$form['field_total_cost']["#disabled"] = TRUE;
drupal_add_js(drupal_get_path('module', 'mymodule') .'/scripts/mymodule.js');
}
Tags: