Question

I want to create jqwidgets datagrid, I want all columns as string but when i add new row throught javascript instead of some string value, component set me number 0,1,2,3 ets..

here is code:

var source ={
                datatype: "json",
                cache:false,
                contentType: 'application/json; charset=utf-8',
                datafields: [
                    { name: 'uid', type:'string'},
                    <% 
                    int count = 1;
                    for(String col : us.columns) {

                        if(count == us.columns.length) {
                        %>
                    { name: '<%= col %>' }        
                        <% 
                        }
                        else {
                        %>
                    { name: '<%= col %>' },        
                        <%
                        }
                        ++count;
                    }
                    %>
                ],
                url:"<%= request.getContextPath() %>/test/list/",
                addrow: function (rowid, rowdata, position, commit) {


                    //alert(rowdata.uid +" "+ rowdata.ime);
                    //alert($.param(rowdata));
                    $.ajax({

                        url: "<%= request.getContextPath() %>/test/add/",
                        type: "POST",
                        data: $.param(rowdata),
                        success: function(data) {
                            //alert(data.error + " " + data.result + " " + data.mess);
                            alert(data.ime + " " + data.columns);
                            $("#historygrid").jqxGrid('updatebounddata');
                            increment++;
                            commit(true);
                        },
                        error:function(data) {

                            alert(data);
                            commit(false);
                        }
                    });

                },
                deleterow: function (rowid, commit) {

                    //commit(true);
                }

            };

            var dataAdapter = new $.jqx.dataAdapter(source);

            $("#testgrid").jqxGrid({
                width: 659,
                height: 300,
                source: dataAdapter,
                theme: 'office',
                editable: true,
                selectionmode: 'singlecell',
                autoheight: false,
                altrows: false,
                showtoolbar: true,
                rendertoolbar: function (toolbar) {
                    var me = this;
                    var container = $("<div style='margin: 5px;'></div>");
                    toolbar.append(container);
                    container.append('<input id="addrowbutton" type="button" value="Dodaj novi red" />');
                    container.append('<input style="margin-left: 5px;" id="deleterowbutton" type="button" value="Obrisi oznaceni red" />');
                    //container.append('<input style="margin-left: 5px;" id="updaterowbutton" type="button" value="Update Selected Row" />');

                    $("#addrowbutton").jqxButton();
                    $("#deleterowbutton").jqxButton();

                    // create new row.
                    $("#addrowbutton").on('click', function () {
                        var datarow = newrow();
                        var commit = $("#testgrid").jqxGrid('addrow', null, datarow);
                    });

                    // delete row.
                    $("#deleterowbutton").on('click', function () {
                        var selectedrowindex = $("#testgrid").jqxGrid('getselectedrowindex');
                        var rowscount = $("#testgrid").jqxGrid('getdatainformation').rowscount;
                        if (selectedrowindex >= 0 && selectedrowindex < rowscount) {
                            var id = $("#testgrid").jqxGrid('getrowid', selectedrowindex);
                            var commit = $("#testgrid").jqxGrid('deleterow', id);
                        }
                    });
                },

                columns: [
                    { text: 'UID', datafield: 'uid', editable:false, width: '10%'},

                    <% 

                    count = 1;
                    for(String col : us.columns) {

                        if(count == us.columns.length) {
                        %>
                    { text:'<%= col %>', datafield: '<%= col %>', width: '45%'}         
                        <% 
                        }
                        else {
                        %>
                    { text:'<%= col %>', datafield: '<%= col %>', width: '45%'},         
                        <%
                        }
                        ++count;
                    }
                    %>
                    ]
            });

            var newrow = function (i) {
                var row = {};
                row["uid"] = "hallo";
                <% 
                    for(String col : us.columns) {
                        %>
                row["<%=col%>"]  = "Default Value";      
                        <% 
                    }
                    %>
                return row;
            }

check the newrow function for uid I set value "Hallo" but I got 0 and fo every next row I got increment value, this looks like numeration or? how to solve this?

Was it helpful?

Solution

If you don't want auto-generated ids, you need to set the source object's id member.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top