Foros del Web » Programando para Internet » Jquery »

[SOLUCIONADO] Jquery Objeto clonado

Estas en el tema de Jquery Objeto clonado en el foro de Jquery en Foros del Web. estoy aramando un formulario para ingresar items a una lista de productos con precios y cantidad, el problema que tengo es que en la descripcion ...
  #1 (permalink)  
Antiguo 20/11/2015, 16:33
 
Fecha de Ingreso: octubre-2006
Mensajes: 31
Antigüedad: 17 años, 6 meses
Puntos: 0
Jquery Objeto clonado

estoy aramando un formulario para ingresar items a una lista de productos con precios y cantidad, el problema que tengo es que en la descripcion no puedo agregar el autocomplete para poder jalar el nombre del item.

ejemplo:
tecleo lap... y me sale la lista del auto complete escojo el producto y sale en otra casilla el precio.

se clona el formulario y se muestra, pero no puedo adicionarle el automplete() al input #item-description, he tratado por todos los medios y nada y supongo que darle el valor sera igual de complicado :S

podrian ayudarme.

muchisimas gracias de antemano.


Código:
<html>
    <head>
        <meta charset="utf-8">
        <title>Items</title>

        <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
        <link href="/sis/admin-media/jquery-ui/jquery-ui.css" rel="stylesheet">
        <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
        <script type="text/javascript">

            $(function () {

                var new_dialog = function (type, row) {
                    var dlg = $("#dialog-form2").clone();
                    var iquant = dlg.find(("#item-quantity")),
                            idescrip = dlg.find(("#item-description")),
                            iprice = dlg.find(("#item-price")),
                            itotal = dlg.find(("#item-total"));

                    /*
                     idescrip.appendTo(autocomplete({
                     source: "items.php",
                     minLength: 1
                     }));
                     */
                    type = type || 'Create';
                    var config = {
                        autoOpen: true,
                        height: 400,
                        width: 350,
                        modal: true,
                        open: function () {
                            $("#item-description").autocomplete({
                                source: "states_abbrev.php",
                                minLength: 1
                            });
                        },
                        buttons: {
                            "Agregar Producto": save_data,
                            "Cancel": function () {
                                dlg.dialog("close");
                            }
                        },
                        close: function () {
                            dlg.remove();
                        }
                    };

                    if (type === 'Edit4') {
                        config.title = "Editar Producto";
                        get_data();
                        delete (config.buttons['Agregar Producto']);
                        config.buttons['Editar Producto'] = function () {
                            row.remove();
                            save_data();
                        };
                    }

                    dlg.dialog(config);

                    function get_data() {
                        var _iquant = $(row.children().get(0)).text(),
                                _idescrip = $(row.children().get(1)).text(),
                                _iprice = $(row.children().get(2)).text(),
                                _itotal = $(row.children().get(3)).text();

                        iquant.val(_iquant);
                        idescrip.val(_idescrip);
                        iprice.val(_iprice);
                        itotal.val(_itotal);
                    }

                    function save_data() {
                        // calculo total
                        var cant = iquant.val();
                        var pto = iprice.val();
                        var totalItem = cant * pto;

                        $("#items tbody").append("<tr>" +
                                "<td>" + iquant.val() + "</td>" +
                                "<td>" + idescrip.val() + "</td>" +
                                "<td>" + iprice.val() + "</td>" +
                                "<td>" + totalItem + "</td>" +
                                "<td><a href='' class='editx'>Editar</a></td>" +
                                "<td><span class='delete'><a href=''>Borrar</a></span></td>" +
                                "</tr>");
                        dlg.dialog("close");
                    }
                }; // end dialog

                $(document).on('click', 'span.delete2', function () {
                    $(this).closest('tr').find('td').fadeOut(1000,
                            function () {
                                // alert($(this).text());
                                $(this).parents('tr:first').remove();
                            });
                    return false;
                });

                $(document).on('click', 'td a.editx', function () {
                    new_dialog('Edit4', $(this).parents('tr'));
                    return false;
                });

                $("#add-item").button().click(new_dialog);

            });

        </script>    

        <style type="text/css">
            body {
                /* font-size: 62.5%; */
            }

            label, input {
                display: block;
            }

            input.text {
                margin-bottom: 12px;
                width: 95%;
                padding: .4em;
            }

            fieldset {
                padding: 0;
                border: 0;
                margin-top: 25px;
            }

            h1 {
                font-size: 1.2em;
                margin: .6em 0;
            }



            div#users-contain table {
                margin: 1em 0;
                border-collapse: collapse;
                width: 100%;
            }

            div#users-contain table td, div#users-contain table th {
                border: 1px solid #eee;
                padding: .6em 10px;
                text-align: left;
            }

            .ui-dialog .ui-state-error {
                padding: .3em;
            }

            .validateTips {
                border: 1px solid transparent;
                padding: 0.3em;
            }

            #dialog-form, #dialog-form2 {
                display: none;
            }

            div#items-contain {
                /* width: 350px; */
                margin: 20px 0;
            }
            table#items{
                width: 100%;
            }

        </style>

    </head>
    <body>
        <div id="dialog-form2" title="Ingresar Producto">
            <form>
                <fieldset>
                    <label for="item-quantity">
                        Cantidad</label>
                    <input type="text" name="item-quantity" id="item-quantity" value="" class="text ui-widget-content ui-corner-all" />
                    <label for="item-description">
                        Descripción</label>
                    <input type="text" name="item-description" id="item-description" value="" class="text ui-widget-content ui-corner-all" />
                    <label for="item-price">
                        P. Unit.</label>
                    <input type="text" name="item-price" id="item-price" value="" class="text ui-widget-content ui-corner-all" />
                    <label for="item-total">
                        P. Total.</label>
                    <input type="text" name="item-total" id="item-total" value="" class="text ui-widget-content ui-corner-all" />
                </fieldset>
            </form>
        </div>



        <div id="items-contain" class="ui-widget">
            <h1>
                Items</h1>

            <table id="items" class="ui-widget ui-widget-content">
                <thead>
                    <tr class="ui-widget-header ">
                        <th>Cant. </th>
                        <th>Descripción </th>
                        <th>P. Unit.</th>
                        <th>P. Total.</th>
                        <th colspan="2"> Operaciones</th>
                    </tr>
                </thead>
                <tbody>
                    <tr> 
                        <!-- Lista de Items //-->
                </tbody>
            </table>
        </div>

        <button id="add-item">
            Ingresar Item</button>
    </body>
</html>
  #2 (permalink)  
Antiguo 21/11/2015, 06:40
 
Fecha de Ingreso: enero-2015
Ubicación: Cordoba, Andalucía
Mensajes: 111
Antigüedad: 9 años, 4 meses
Puntos: 15
Respuesta: Jquery Objeto clonado

Te paso un ejemplo de como lo hice yo

Código Javascript:
Ver original
  1. /*Aquí hice un array js. Los datos que muestra el autocomplete están en una tabla de mi bd, por lo tanto hice una consulta con php y luego pasé estos resultados a un array js.  La cuestión es que necesitamos partir de un array js.*/
  2.  
  3. var arrayAutocomplete =
  4.         [
  5.             <?php for($i=0; $i < count ($result); ++$i ){
  6.        
  7.                 echo '"'.$result[$i]->product.'", "'.$result[$i]->code.'", ';                      
  8.                    
  9.             } ?>
  10.         ];
  11.  
  12. /* aqui asocia el array con el input text mediante id*/
  13.  
  14. $(document).ready(function(){
  15.    
  16.     $("#autocomplete").autocomplete
  17.     ({
  18.         source: arrayAutocomplete
  19.     });
  20. })

Eso es todo, necesitas cargar la libreria jquery-ui que veo que ya la tienes y un css tipo este.

Código CSS:
Ver original
  1. /*INICIO AUTOCOMPLETAR*/
  2. .ui-front {
  3.     z-index: 100;
  4. }
  5. .ui-autocomplete {
  6.     position: absolute;
  7.     top: 0;
  8.     left: 0;
  9.     cursor: default;
  10. }
  11. .ui-menu {
  12.     list-style: none;
  13.     padding: 0;
  14.     margin: 0;
  15.     display: block;
  16.     outline: none;
  17. }
  18. .ui-menu .ui-menu {
  19.     position: absolute;
  20. }
  21. .ui-menu .ui-menu-item {
  22.     position: relative;
  23.     margin: 0;
  24.     padding: 3px 1em 3px .4em;
  25.     cursor: pointer;
  26.     min-height: 0; /* support: IE7 */
  27.     /* support: IE10, see #8844 */
  28.     list-style-image: url("data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7");
  29. }
  30. .ui-menu .ui-state-focus,
  31. .ui-menu .ui-state-active {
  32.     margin: -1px;
  33. }
  34. .ui-widget {
  35.     font-family: Verdana,Arial,sans-serif;
  36.     font-size: 1.1em;
  37. }
  38. .ui-widget-content {
  39.     border: 1px solid #aaaaaa;
  40.     background: #ffffff url("images/ui-bg_flat_75_ffffff_40x100.png") 50% 50% repeat-x;
  41.     color: #222222;
  42. }
  43. .ui-widget-content .ui-state-focus{
  44.     border: 1px solid #999999;
  45.     background: #dadada url("images/ui-bg_glass_75_dadada_1x400.png") 50% 50% repeat-x;
  46.     font-weight: normal;
  47.     color: #212121;
  48. }
  49. /*FIN AUTOCOMPLETAR*/


Espero sea de ayuda, un saludo!
  #3 (permalink)  
Antiguo 21/11/2015, 11:27
 
Fecha de Ingreso: octubre-2006
Mensajes: 31
Antigüedad: 17 años, 6 meses
Puntos: 0
Respuesta: Jquery Objeto clonado

no funciona, que libreria de jquery usas?

gracias
  #4 (permalink)  
Antiguo 21/11/2015, 11:58
 
Fecha de Ingreso: octubre-2006
Mensajes: 31
Antigüedad: 17 años, 6 meses
Puntos: 0
Respuesta: Jquery Objeto clonado

No me funciono lo que me pasaste pero encontre la solución. espero qeu le sirva a alguien
Código HTML:
<html>
    <head>
        <meta charset="utf-8">
        <title>Items</title>

        <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
        <link href="/sis/admin-media/jquery-ui/jquery-ui.css" rel="stylesheet">
        <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
        <script src="http://code.jquery.com/jquery-migrate-1.2.1.js"></script> 
        <script type="text/javascript">
                    $(function () {

                    var new_dialog = function (type, row) {
                    var dlg = $("#dialog-form2").clone();
                            var iquant = dlg.find(("#item-quantity")),
                            idescrip = dlg.find(("#item-description")),
                            iprice = dlg.find(("#item-price")),
                            itotal = dlg.find(("#item-total"));
                            type = type || 'Create';
                            var config = {
                            autoOpen: true,
                                    height: 450,
                                    width: 350,
                                    modal: true,
                                    open: function () {
                                    $(dlg.find(("#item-description"))).autocomplete({
                                    source: "items.php",
                                            minLength: 1,
                                            select: function (event, ui) {
                                            var code = ui.item.id;
                                                    if (code != '') {
                                            //location.href = '/invoice.php?customercode=' + code;
                                            iprice.val(code);
                                                    //alert("codigo: " + iquant.val());
                                                    var sumTotal = code * iquant.val();
                                                    itotal.val(sumTotal.toFixed(2));
                                            }
                                            },
                                    });
                                    },
                                    buttons: {
                                    "Agregar Producto": save_data,
                                            "Cancel": function () {
                                            dlg.dialog("close");
                                            }
                                    },
                                    close: function () {
                                    dlg.remove();
                                    }
                            };
                            if (type === 'Edit4') {
                    config.title = "Editar Producto";
                            get_data();
                            delete (config.buttons['Agregar Producto']);
                            config.buttons['Editar Producto'] = function () {
                    row.remove();
                            save_data();
                    };
                    }

                    dlg.dialog(config);
                            function get_data() {
                            var _iquant = $(row.children().get(0)).text(),
                                    _idescrip = $(row.children().get(1)).text(),
                                    _iprice = $(row.children().get(2)).text(),
                                    _itotal = $(row.children().get(3)).text();
                                    iquant.val(_iquant);
                                    idescrip.val(_idescrip);
                                    iprice.val(_iprice);
                                    itotal.val(_itotal);
                            }

                    function save_data() {
                    // calculo total
                    var cant = iquant.val();
                            var pto = iprice.val();
                            var totalItem = itotal.val();
                            $("#items tbody").append("<tr>" +
                            "<td>" + iquant.val() + "</td>" +
                            "<td>" + idescrip.val() + "</td>" +
                            "<td>" + iprice.val() + "</td>" +
                            "<td>" + totalItem + "</td>" +
                            "<td><a href='' class='editx'>Editar</a></td>" +
                            "<td><span class='delete2'><a href=''>Borrar</a></span></td>" +
                            "</tr>");
                            dlg.dialog("close");
                    }
                    }; // end dialog

                            $(document).on('click', 'span.delete2', function () {
                    $(this).closest('tr').find('td').fadeOut(1000,
                            function () {
                            //alert($(this).text());
                            $(this).parents('tr:first').remove();
                            });
                            return false;
                    });
                            $(document).on('click', 'td a.editx', function () {
                    new_dialog('Edit4', $(this).parents('tr'));
                            return false;
                    });
                            $("#add-item").button().click(new_dialog);
                    });        </script>    


        <style type="text/css">
            body {
                /* font-size: 62.5%; */
            }

            label, input {
                display: block;
            }

            input.text {
                margin-bottom: 12px;
                width: 95%;
                padding: .4em;
            }

            fieldset {
                padding: 0;
                border: 0;
                margin-top: 25px;
            }

            h1 {
                font-size: 1.2em;
                margin: .6em 0;
            }

            div#users-contain table {
                margin: 1em 0;
                border-collapse: collapse;
                width: 100%;
            }

            div#users-contain table td, div#users-contain table th {
                border: 1px solid #eee;
                padding: .6em 10px;
                text-align: left;
            }

            .ui-dialog .ui-state-error {
                padding: .3em;
            }

            .validateTips {
                border: 1px solid transparent;
                padding: 0.3em;
            }

            #dialog-form, #dialog-form2 {
                display: none;
            }

            div#items-contain {
                /* width: 350px; */
                margin: 20px 0;
            }
            table#items{
                width: 100%;
            }



        </style>

    </head>
    <body>
        <div id="dialog-form2" title="Ingresar Producto">
            <form>
                <fieldset>
                    <label for="item-quantity">
                        Cantidad</label>
                    <input type="text" name="item-quantity" id="item-quantity" value="" class="text ui-widget-content ui-corner-all" />
                    <label for="item-description">
                        Descripción</label>
                    <input type="text" name="item-description" id="item-description" value="" class="text ui-widget-content ui-corner-all" />
                    <label for="item-price">
                        P. Unit.</label>
                    <input type="text" name="item-price" id="item-price" value="" class="text ui-widget-content ui-corner-all" />
                    <label for="item-total">
                        P. Total.</label>
                    <input type="text" name="item-total" id="item-total" value="" class="text ui-widget-content ui-corner-all" />
                </fieldset>
            </form>
        </div>

        <div id="items-contain" class="ui-widget">
            <h1>
                Items</h1>
            <table id="items" class="ui-widget ui-widget-content">
                <thead>
                    <tr class="ui-widget-header ">
                        <th>Cant. </th>
                        <th>Descripción </th>
                        <th>P. Unit.</th>
                        <th>P. Total.</th>
                        <th colspan="2"> Operaciones</th>
                    </tr>
                </thead>
                <tbody>
                    <tr> 
                </tbody>
            </table>
        </div>
        <button id="add-item">
            Ingresar Item</button>
    </body>
</html> 
  #5 (permalink)  
Antiguo 21/11/2015, 14:32
 
Fecha de Ingreso: enero-2015
Ubicación: Cordoba, Andalucía
Mensajes: 111
Antigüedad: 9 años, 4 meses
Puntos: 15
Respuesta: Jquery Objeto clonado

Cita:
Iniciado por walterwn Ver Mensaje
no funciona, que libreria de jquery usas?

gracias
jquery version actual y jquery-ui

sólo hay que hacer un array y asociarlo a una id.

De todas formas si lo has solucionado,

Etiquetas: objeto
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:07.