Create function duplicate record in datatable

Create function duplicate record in datatable

jlgiljlgil Posts: 6Questions: 3Answers: 0

I have a Datatable connected to the editor. When i create a new record, i post the data to the database with ajax, then i get the data back. There are two records. The json is well formed and has two records but the datatable show the first record and the second two times. Any tip?

editor = new $.fn.dataTable.Editor( {
    ajax: "infofacturacion.cfm?cdtienda=" + $("#cdtienda").val() + "&idsesion=" + $("#idsesion").val(),
    table: "#factugrid",                
    "idSrc": "cdfacturacion",
    fields: [{
            label: "cdfacturacion:",
            name: "cdfacturacion",
            type: "hidden"
        }, {
            label: "cdTienda:",
            name: "cdtienda",
            type: "hidden"
        }, {
            label: "Plataforma:",
            name: "cdgestor",
            type:  "select",
            options: listaPlataformas
        }, {
            label: "Tipo oferta:",
            name: "intipo_oferta",
            type:  "select",
            options: [
                { label: "oferta base", value: "1" },
                { label: "oferta bonus",value: "2" }
            ]
        }, {
            label: "Código Facturación:",
            name: "dccod_facturacion"
        }, {    
            label: "Código Evento:",
            name: "cdevento"                
        }, {
            label: "Desc. Evento:",
            name: "dcevento"
        }, {
            label: "Texto Oferta:",
            name: "dctexto",
            type: "textarea"
        }, {
            label: "Concepto:",
            name: "cdproducto"
        }, {    
            label: "Subconcepto:",
            name: "cdsubproducto"
        }, {    
            label: "Comision:",
            name: "cacomision",
            fieldInfo: 'formato X,XX'
        }, {
            label: "Tipo de Comisión:",
            name: "intipo_comision",
            type:  "select",
            options: [
                { label: "% (porcentaje)", value: "1" },
                { label: "euros",          value: "2" }
            ]
        }, {    
            label: "IVA:",
            name: "cdiva",
            fieldInfo: 'Formato entero. Introducir 21 para 21%'
        }, {    
            label: "Puntos máximos:",
            name: "capuntos_max"
        }, {
            label: "Puntos:",
            name: "capuntos"                
        }, {
            label: "Euros:",
            name: "caeuros",
            fieldInfo: 'formato X,XX'
        }, {                                                                                                                                                                                                
            label: "Fecha Inicio:",
            name: "fhinicio",
            type: "datetime",
            format: 'DD/MM/YYYY',
            fieldInfo: 'formato dd/mm/yyyy'
        }, {
            label: "Fecha Fin:",
            name: "fhfin",
            type: "datetime",
            format: 'DD/MM/YYYY',
            fieldInfo: 'formato dd/mm/yyyy'
        }, {
            label: "Patrocinador:",
            name: "patrocinador"
        }, {                
            label: "Estado final a procesar:",
            name: "cdestadofinal"
        }, {                
            label: "Intervalo desde:",
            name: "caintervalodesde"
        }, {                
            label: "Intervalo hasta:",
            name: "caintervalohasta"
        }, {                
            label: "Max compra Fraude (euros):",
            name: "camaxcomprafraudeeuros",
            fieldInfo: 'formato X,XX'
        }, {                
            label: "Total max permitido periodo (euros):",
            name: "catotalmaxpermitidoeuros",
            fieldInfo: 'formato X,XX'
        }, {                
            label: "Periodo (dias - euros):",
            name: "caperiododiaseuros"
        }, {                
            label: "Total max permitido periodo (puntos):",
            name: "catotalmaxpermitidopuntos"
        }, {                                
            label: "Periodo (dias - ptos):",
            name: "caperiododiaspuntos"
        }
    ],
    i18n: {
            create: {
                button: "Nuevo",
                title:  "Crear nueva entrada",
                submit: "Crear"
            },
            edit: {
                button: "Modificar",
                title:  "Modificar entrada",
                submit: "Actualizar"
            },
            remove: {
                button: "Eliminar",
                title:  "Eliminar",
                submit: "Eliminar",
                confirm: {
                    1: "¿Estás seguro de que quieres eliminar este registro?"
                }
            },
            error: {
                system: "Se ha producido un error. Contacta con el adminsitrador de la aplicación"
            },
            datetime: {
                previous: 'Anterior',
                next:     'Siguiente',
                months:   [ 'Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre' ],
                weekdays: [ 'Dom', 'Lun', 'Mar', 'Mié', 'Jue', 'Vie', 'Sáb' ]
            }
        },

}); //Fin de editor


/* *********************** */  
/* Facturación. Datatable. */            
/* *********************** */               
var table = $('#factugrid').DataTable({
    dom: "Bfrtip",
    ajax: "infofacturacion.cfm?cdtienda=0" + $("#cdtienda").val() + "&idsesion=" + $("#idsesion").val(),
    scrollX: true,
    language: {"url": "/plugins/datatables/Spanish.json","decimal": ",","thousands": ".",select: {rows: "%d filas seleccionadas"}},
    columns: [
        {data: "cdfacturacion","visible": false,"searchable": false},
        {data: "cdgestor","visible": false,"searchable": false},
        {data: "cdtienda","visible": false,"searchable": false},
        {data: "dcgestor"},
        {
            data: "intipo_oferta",
            render: function ( data, type, row ) {
            if(data == 1)
                return "oferta base"
            else
                return "oferta bonus";
            }
        },          
        {data: "dccod_facturacion"},            
        {data: "cdevento"},
        {data: "dcevento"},
        {data: "dctexto"},
        {data: "cdproducto"},
        {data: "cdsubproducto"},            
        {data: "cacomision"},
        {
            data: "intipo_comision",
            render: function ( data, type, row ) {
            if(data == 1)
                return "%"
            else
                return "euros";
            }
        },          
        {
            data: "cdiva",
            render: function ( data, type, row ) {return data + "%"}
        },                  
        {data: "capuntos_max"},
        {data: "capuntos"},
        {data: "caeuros"},
        {data: "fhinicio"},
        {data: "fhfin"},
        {data: "patrocinador"},         
        {data: "cdestadofinal"},
        {data: "caintervalodesde"},
        {data: "caintervalohasta"},
        {data: "camaxcomprafraudeeuros"},
        {data: "catotalmaxpermitidoeuros"},
        {data: "caperiododiaseuros"},
        {data: "catotalmaxpermitidopuntos"},
        {data: "caperiododiaspuntos"}
    ],
    select: true,

    buttons: [{ extend: "create", editor: editor },{ extend: "edit",   editor: editor },{ extend: "remove", editor: editor },]              
}); //Fin Datatable 

The Json response

{"data": [{"cdfacturacion": "637",
"cdgestor": "12",
"cdtienda": "480",
"dcgestor": "TradeTracker",
"intipo_oferta": "1",
"dccod_facturacion": "19843",
"cdevento": "",
"dcevento": "",
"dctexto": "3 puntos por cada 2? de compra",
"cdproducto": "457",
"cdsubproducto": "1",
"cacomision": "6,00",
"intipo_comision": "1",
"cdiva": "21",
"capuntos_max": "0",
"capuntos": "3",
"caeuros": "2,00",
"fhinicio": "21/09/2016",
"fhfin": "31/12/2099",
"patrocinador": "995",
"cdestadofinal": "",
"caintervalodesde": "",
"caintervalohasta": "",
"camaxcomprafraudeeuros": "",
"catotalmaxpermitidoeuros": "",
"caperiododiaseuros": "",
"catotalmaxpermitidopuntos": "",
"caperiododiaspuntos": ""
},{"cdfacturacion": "714",
"cdgestor": "4",
"cdtienda": "480",
"dcgestor": "Affilinet",
"intipo_oferta": "1",
"dccod_facturacion": "002",
"cdevento": "0364",
"dcevento": "prueba",
"dctexto": "3x2?",
"cdproducto": "386",
"cdsubproducto": "1",
"cacomision": "2,00",
"intipo_comision": "1",
"cdiva": "21",
"capuntos_max": "0",
"capuntos": "3",
"caeuros": "2,00",
"fhinicio": "11/03/2016",
"fhfin": "24/11/2016",
"patrocinador": "995",
"cdestadofinal": "",
"caintervalodesde": "",
"caintervalohasta": "",
"camaxcomprafraudeeuros": "",
"catotalmaxpermitidoeuros": "",
"caperiododiaseuros": "",
"catotalmaxpermitidopuntos": "",
"caperiododiaspuntos": ""
}]}

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,653Questions: 1Answers: 10,094 Site admin

    but the datatable show the first record and the second two times

    Do you mean that both rows are shown twice? Or that the first one is shown twice and the second one not at all?

    Thanks,
    Allan

  • jlgiljlgil Posts: 6Questions: 3Answers: 0

    Hello Allan,

    Starting from an empty table i create a record with full row editing. At the end the table shows that record (record1). Then i create a second record (record2). When the process ends the table shows (record1,record1,record2).

    The editor ajax function returns a Json with (record1,record2) and in the database there are too record1 and record 2.

    Maybe the JSON answer isn't correct and should only response "record2" in the second insert?

    thanks in advance

  • allanallan Posts: 61,653Questions: 1Answers: 10,094 Site admin
    Answer ✓

    Thanks for the clarification. The error you are seeing suggests that the infofacturacion.cfm script isn't just returning the data for the newly added row, but rather the data for all rows.

    Maybe the JSON answer isn't correct and should only response "record2" in the second insert?

    This is exactly correct - yes!

    Allan

  • jlgiljlgil Posts: 6Questions: 3Answers: 0

    It's solved! There was the problem.

    Thank for your help Allan.

This discussion has been closed.