This sample is to change lookups values based on default values. If you want to change a text box, then use $j('#field').val(value)
In your tablename-dv.js file, listening to the change event of the field
Code: Select all
$j('#entidad_id-container').on('change', function(){
// Al cambiar entidad se localiza valores por defecto.
if ($j('#entidad_id').val() == '{empty_value}') return false;
valor_entidad = $j('#entidad_id').val();
$j.ajax({
url: 'hooks/ajax-entidad-datos-defecto.php',
data: { entidad_id: valor_entidad },
success: function(data){
if (data) {
actualizaLookupsEntidad(data);
}
}
});
});
My function actualizaLookupsEntidad in the same file:
Code: Select all
function actualizaLookupsEntidad(datos){
var dataJSON = JSON.parse(datos);
updateLookup('serie_id', dataJSON.serieid, dataJSON.seriecodigo + ' - ' + dataJSON.serie );
updateLookup('formaDePago_id', dataJSON.formadepagoid, dataJSON.formadepagoref + ' - ' + dataJSON.formadepago );
updateLookup('condicionDePago_id', dataJSON.condicionid, dataJSON.condicionref + ' - ' + dataJSON.condicion );
updateLookup('comercial_id', dataJSON.comercialid, dataJSON.comercialref + ' - ' + dataJSON.comercial );
}
My updateLookup function
Code: Select all
function updateLookup(field, idValue, textValue){
if (idValue) {
$j('#' + field).val(idValue);
$j('#' + field + '-container').select2('data', {id:idValue, text:textValue});
} else {
$j('#' + field).val(idValue);
$j('#' + field + '-container').select2('data', {id:null, text:'<No>'});
}
}
My ajax-entidad-datos-defecto.php ajax "service"
Code: Select all
<?php
$currDir = dirname(__FILE__) . '/..';
include("$currDir/defaultLang.php");
include("$currDir/language.php");
include("$currDir/lib.php");
/* grant access to all users who have acess to the orders table */
$od_from = get_sql_from('entidad');
if(!$od_from){
header('HTTP/1.0 401 Unauthorized');
exit;
}
$entidad = makesafe($_REQUEST['entidad_id']);
if (!$entidad) die("Ocurrió un error");
//consultar siguiente
$sql = "SELECT
formadepago.nombreFormaDePago AS formadepago,
formadepago.ref AS formadepagoref,
formadepago.id AS formadepagoid,
seriedocumento.codigoSerie AS seriecodigo,
seriedocumento.id AS serieid,
seriedocumento.nombreSerie AS serie,
direccionentidad.id AS direccionid,
direccionentidad.nombreDireccion AS direccion,
condiciondepago.id AS condicionid,
condiciondepago.nombreCondicionDePago AS condicion,
condiciondepago.ref AS condicionref,
comercial.id AS comercialid,
comercial.ref AS comercialref,
comercial.nombreComercial AS comercial
FROM
entidad
LEFT JOIN seriedocumento ON entidad.seriePorDefecto_id = seriedocumento.id
LEFT JOIN direccionentidad ON entidad.id = direccionentidad.entidad_id AND direccionentidad.direccion_defecto = 1
LEFT JOIN formadepago ON entidad.formaDePagoVentas_id = formadepago.id
LEFT JOIN condiciondepago ON entidad.condicionDePagoVentas_id = condiciondepago.id
LEFT JOIN comercial ON entidad.comercial_id = comercial.id
WHERE entidad.id = {$entidad}";
$datos = sql($sql, $eo);
//$salida = array();
$valores_defecto = db_fetch_assoc($datos);
echo json_encode($valores_defecto);