jQWidgets Forums

jQuery UI Widgets Forums Grid make only newly added row as editable , others as false

This topic contains 3 replies, has 2 voices, and was last updated by  Dimitar 11 years, 3 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author

  • lalit singh
    Participant

    Hi,

    how can i make newly added row as editable. rest all rows shall be editable = false.

    Regard,

    lalit singh


    Dimitar
    Participant

    Hello lalit singh,

    Here is an example:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" />
        <script type="text/javascript" src="../../scripts/jquery-1.10.2.min.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxcore.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxdata.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxbuttons.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxscrollbar.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxmenu.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxcheckbox.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxlistbox.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxdropdownlist.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxgrid.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxgrid.selection.js"></script>
        <script type="text/javascript" src="../../jqwidgets/jqxgrid.edit.js"></script>
        <script type="text/javascript" src="../../scripts/demos.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                // prepare the data
                var data = {};
    
                var firstNames =
                [
                    "Andrew", "Nancy", "Shelley", "Regina", "Yoshi", "Antoni", "Mayumi", "Ian", "Peter", "Lars", "Petra", "Martin", "Sven", "Elio", "Beate", "Cheryl", "Michael", "Guylene"
                ];
    
                var lastNames =
                [
                    "Fuller", "Davolio", "Burke", "Murphy", "Nagase", "Saavedra", "Ohno", "Devling", "Wilson", "Peterson", "Winkler", "Bein", "Petersen", "Rossi", "Vileid", "Saylor", "Bjorn", "Nodier"
                ];
    
                var productNames =
                [
                    "Black Tea", "Green Tea", "Caffe Espresso", "Doubleshot Espresso", "Caffe Latte", "White Chocolate Mocha", "Cramel Latte", "Caffe Americano", "Cappuccino", "Espresso Truffle", "Espresso con Panna", "Peppermint Mocha Twist"
                ];
    
                var priceValues =
                [
                    "2.25", "1.5", "3.0", "3.3", "4.5", "3.6", "3.8", "2.5", "5.0", "1.75", "3.25", "4.0"
                ];
    
                var generaterow = function (i) {
                    var row = {};
                    var productindex = Math.floor(Math.random() * productNames.length);
                    var price = parseFloat(priceValues[productindex]);
                    var quantity = 1 + Math.round(Math.random() * 10);
    
                    row["firstname"] = firstNames[Math.floor(Math.random() * firstNames.length)];
                    row["lastname"] = lastNames[Math.floor(Math.random() * lastNames.length)];
                    row["productname"] = productNames[productindex];
                    row["price"] = price;
                    row["quantity"] = quantity;
                    row["total"] = price * quantity;
                    return row;
                }
    
                for (var i = 0; i < 10; i++) {
                    var row = generaterow(i);
                    data[i] = row;
                }
    
                var source =
                {
                    localdata: data,
                    datatype: "local",
                    datafields:
                    [
                        { name: 'firstname', type: 'string' },
                        { name: 'lastname', type: 'string' },
                        { name: 'productname', type: 'string' },
                        { name: 'quantity', type: 'number' },
                        { name: 'price', type: 'number' },
                        { name: 'total', type: 'number' }
                    ],
                    addrow: function (rowid, rowdata, position, commit) {
                        // synchronize with the server - send insert command
                        // call commit with parameter true if the synchronization with the server is successful 
                        //and with parameter false if the synchronization failed.
                        // you can pass additional argument to the commit callback which represents the new ID if it is generated from a DB.
                        commit(true);
                    },
                    deleterow: function (rowid, commit) {
                        // synchronize with the server - send delete command
                        // call commit with parameter true if the synchronization with the server is successful 
                        //and with parameter false if the synchronization failed.
                        commit(true);
                    },
                    updaterow: function (rowid, newdata, commit) {
                        // synchronize with the server - send update command
                        // call commit with parameter true if the synchronization with the server is successful 
                        // and with parameter false if the synchronization failed.
                        commit(true);
                    }
                };
    
                var dataAdapter = new $.jqx.dataAdapter(source);
    
                var newRowIndex;
    
                var cellbeginedit = function (row, datafield, columntype) {
                    if (newRowIndex == undefined || row != newRowIndex)
                        return false;
                };
    
                // initialize jqxGrid
                $("#jqxgrid").jqxGrid(
                {
                    width: 500,
                    height: 350,
                    source: dataAdapter,
                    editable: true,
                    columns: [
                      { text: 'First Name', datafield: 'firstname', width: 100, cellbeginedit: cellbeginedit },
                      { text: 'Last Name', datafield: 'lastname', width: 100, cellbeginedit: cellbeginedit },
                      { text: 'Product', datafield: 'productname', width: 180, cellbeginedit: cellbeginedit },
                      { text: 'Quantity', datafield: 'quantity', width: 80, cellsalign: 'right', cellbeginedit: cellbeginedit },
                      { text: 'Unit Price', datafield: 'price', width: 90, cellsalign: 'right', cellsformat: 'c2', cellbeginedit: cellbeginedit },
                      { text: 'Total', datafield: 'total', width: 100, cellsalign: 'right', cellsformat: 'c2', cellbeginedit: cellbeginedit }
                    ]
                });
    
                $("#addrowbutton").jqxButton({ theme: theme });
    
                // create new row.
                $("#addrowbutton").on('click', function () {
                    var datarow = generaterow();
                    var commit = $("#jqxgrid").jqxGrid('addrow', null, datarow);
                    var rows = $('#jqxgrid').jqxGrid('getrows');
                    newRowIndex = rows.length - 1;
                });
            });
        </script>
    </head>
    <body class='default'>
        <div id='jqxWidget' style="font-size: 13px; font-family: Verdana; float: left;">
            <div style="float: left;" id="jqxgrid">
            </div>
            <div style="margin-left: 10px; float: left;">
                <div>
                    <input id="addrowbutton" type="button" value="Add New Row" />
                </div>
            </div>
        </div>
    </body>
    </html>

    This code works if the new row is added at the bottom of the grid. If you add it to the top, set newRowIndex to 0:

    $("#addrowbutton").on('click', function () {
        var datarow = generaterow();
        var commit = $("#jqxgrid").jqxGrid('addrow', null, datarow, "first");
        newRowIndex = 0;
    });

    Best Regards,
    Dimitar

    jQWidgets team
    http://www.jqwidgets.com/


    lalit singh
    Participant

    Hi Dimitar,

    Thanks for the information.

    right now , all rows are editable=false, new row= editable true. i want to make specific column as editable=true with these.

    regards,

    lalit singh


    Dimitar
    Participant

    Hi lalit singh,

    In this case, set each column’s editable property to true or false, e.g.:

    columns: [
        { text: 'First Name', datafield: 'firstname', width: 100, cellbeginedit: cellbeginedit, editable: true },
        { text: 'Last Name', datafield: 'lastname', width: 100, cellbeginedit: cellbeginedit, editable: false },
        { text: 'Product', datafield: 'productname', width: 180, cellbeginedit: cellbeginedit, editable: false },
        { text: 'Quantity', datafield: 'quantity', width: 80, cellsalign: 'right', cellbeginedit: cellbeginedit, editable: false },
        { text: 'Unit Price', datafield: 'price', width: 90, cellsalign: 'right', cellsformat: 'c2', cellbeginedit: cellbeginedit, editable: false },
        { text: 'Total', datafield: 'total', width: 100, cellsalign: 'right', cellsformat: 'c2', cellbeginedit: cellbeginedit, editable: false }
    ]

    Best Regards,
    Dimitar

    jQWidgets team
    http://www.jqwidgets.com/

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.