2

Currently I have a requirement in a system where I need to repeat much of the data from the last row entered. Actualy this is my Grid:

$('#list').jqGrid({
        colNames: ['VendedorId', 'Vendedor', 'Especie', 'Cabezas', 'Kilos', 'Precio', 'Guías Venta', 'Vencimiento'],
        colModel: [
            { hidden: true, name: 'VendedorId' },
            { editable: true, width: 160, edittype: 'select', editoptions: { dataUrl: '/Clientes/CmbClientes' }, editrules: { required: true }, name: 'Vendedor' },
            { editable: true, width: 70, edittype: 'select', editoptions: { dataUrl: '/Especies/CmbEspecie' }, editrules: { required: true }, name: 'Especie' },
            { align: 'right', width: 50, editable: true, editoptions: { size: 3, maxlength: 3 }, editrules: { number: true }, formatter: 'number', formatoptions: { decimalPlaces: 0 }, name: 'Cabezas' },
            { align: 'right', width: 50, editable: true, editrules: { number: true }, formatter: 'number', formatoptions: { decimalPlaces: 0 }, name: 'Kilos' },
            { align: 'right', width: 50, editable: true, editrules: { number: true, required: true }, formatter: 'currency', formatoptions: { prefix: '$',decimalPlaces: 2  }, name: 'Precio' },
            { editable: true, width: 50, editoptions: { maxlength: 20 }, name: 'GuiasVenta' },
            { align: 'right', width: 70, editable: true, editoptions: { size: 3, maxlength: 3 }, editrules: { number: true, required: true }, formatter: 'number', formatoptions: { decimalPlaces: 0 }, name: 'Vencimiento' }
        ],
        url: '@Url.Action("ListVendedores")',
        datatype: 'json',
        editurl: '@Url.Action("SaveVendedor")',
        mtype: 'POST',
        pager: '#vendedoresPager',
        prmNames: { id: 'RemateId' },
        rowList: [5, 10, 15, 20],
        sortname: 'FeriaId',
        viewrecords: true,
        width: 850
    }).jqGrid('navGrid', '#vendedoresPager', { add: false, edit: false, del:true ), search: false },{},{},{ url: '/Remates/BorrarVendedor' }).
        jqGrid('inlineNav', '#vendedoresPager',
            {
                add : true,
                edit : true,
                save : true,
                addParams: {
                    addRowParams: {
                        position: "afterSelected",
                        keys: true,
                        extraparam: {
                            ...
                        }
                    }
                },
                editParams: {
                    keys: true,
                    extraparam: {
                        ...
                    }
                }
            });

When adding the first row no default data, but then fields Vendedor, Especie, Guías Venta and Vencimiento should repeat the last entered.

In this scenario I imagine two possible solutions, one is using the event jqGridInlineEditRow, and the other is using autocomplete. Have read this Question about qGridInlineEditRow. But in this case as I can get the data from the last row of the grid and how it should load the data into the new row. And read this Question about autocomplete.

Maybe there other solutions that can read to get a better approximation to the solution.

Can anyone help?

update 15/04/2013

i replace the Add button for a custom button this is the code:

    $("#Agregar").click(function () {
    var parameters =
        {
            initdata: {},
            position: "first",
            useDefValues: true,
            useFormatter: false,
            addRowParams: {
                keys: true,
                extraparam: {
                    ...
                },
                aftersavefunc: function (rowid) {
                    var grid = jQuery('#vendedores');
                    lastSavedAddData = grid.jqGrid("getRowData", rowid);
                },
                oneditfunc: function (rowid) {
                    var name;
                    var grid = jQuery('#vendedores');
                    for (name in lastSavedAddData) {
                        if (lastSavedAddData.hasOwnProperty(name)) {
                            $('#' + rowid +"_"+  name).val(lastSavedAddData[name]);
                        }
                    }
                }
            }
        };
    jQuery("#vendedores").jqGrid('addRow', parameters);
});

But this work only for text box but not for a combo

Community
  • 1
  • 1
Guillermo
  • 213
  • 3
  • 15
  • Would you just be able to save the data entered into the appropriate fields and then the next time the add form is opened populate those values into the matching fields? – Mark Apr 13 '13 at 00:07
  • @Mark That's the idea, but as it should?. I am also currently exploring this example: http://stackoverflow.com/questions/7392236/jqgrid-with-autocompletion-cant-parse-data-from-controller-to-view – Guillermo Apr 13 '13 at 00:30
  • I don't see why this wouldn't be a fairly reasonable and low profile/overhead solution to your problem. Tying into Autocomplete seems to be about the same level of complexity/overhead to me. – Mark Apr 13 '13 at 01:01

1 Answers1

2

If I understand correctly your question the usage of jQuery UI Autocomplete is independent from initial filling of the input fields of the last added line during starting the next like to add.

It seems to me that you can save data saved in the last added line inside of aftersavefunc callback defined in addRowParams (or use jqGridInlineAfterSaveRow event alternatively). You can use oneditfunc to set the last entered values in the new like. For example you can use

addRowParams: {
    position: "afterSelected",
    keys: true,
    extraparam: {
        ...
    },
    aftersavefunc: function (rowid) {
        lastSavedAddData = $(this).jqGrid("getRowData", rowid);
    },
    oneditfunc: function (rowid) {
        var name;
        for (name in lastSavedAddData) {
            if (lastSavedAddData.hasOwnProperty(name)) {
                $("#" + $.jgrid.jqID(rowid + "_" + name)).val(lastSavedAddData[name]);
            }
        }
    }
}

where lastSavedAddData should be defined in some outer scope. It's not tested code. I just wanted to show the main idea. You can extend the code of oneditfunc to support more different controls which you use in editing line.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Oleg, you are a genius, I will try to do something about this and come back to update my question. – Guillermo Apr 14 '13 at 14:52
  • Oleg, i replace this `$("#" + $.jgrid.jqID(rowid + "_" + name)).val(lastSavedAddData[name]);` for this `$('#' + rowid +"_"+ name).val(lastSavedAddData[name]);` Now that work only for text but not for a select – Guillermo Apr 16 '13 at 01:29
  • 1
    @Guillermo: `$.jgrid.jqID(x)` is the same as `x` if `x` don't contains any special characters, so the usage of `$.jgrid.jqID` in the selectors is just to make the code more safe. `$("#" + $.jgrid.jqID(rowid + "_" + name))` (or `$("#" + rowid + "_" + name)`) provides the elements in the inline editing line. So to set/get the values you should just use methods which are specific for control which you use. `$.val` can be used for selects too. In some cases `:selected'` could be helpful. You have the problem with `select` because you load select data *asynchronously* from `dataUrl`. – Oleg Apr 16 '13 at 05:58
  • @Guillermo: You can use `buildSelect` callback of [editoptions](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:common_rules#editoptions) to modify the response from `dataUrl` *after* the data are loaded. – Oleg Apr 16 '13 at 06:01
  • Oleg, When i tried ´$.jgrid.jqID(x)´ failed but I guess something bad had written, now seek to use what you suggest and come back to update. – Guillermo Apr 16 '13 at 13:46