0

i have loaded jqgrid with car maintaince test results and generated grid as follows:

enter image description here

HTML code

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>

    <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/le-frog/jquery-ui.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/free-jqgrid/4.13.6/css/ui.jqgrid.min.css" />
</head>
<body>

<div id="divContainer"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.0/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.0/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script>
    $.jgrid = $.jgrid || {};
    $.jgrid.no_legacy_api = true;
    $.jgrid.useJSON = true;
</script>

<script src="https://rawgit.com/free-jqgrid/jqGrid/master/js/jquery.jqgrid.src.js"></script>

<script type="text/babel" src="sample.jsx">
</script>



</body>
</html>

React object

var SampleGrid = React.createClass({
    componentDidMount:function(){
        this.gridLoad();
    },
    gridLoad:function(){

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        var mydata = [
            { id: "1", test: "Engine checkup", teststart: "12/12/2016", testend: "12/30/2016", passed: true},
            { id: "2", test: "Electrical Checkup", teststart: "1/2/2017", testend: "1/3/2017", passed: false},
            { id: "3", test: "Chasis checkup", teststart: "1/4/2017", testend: "1/5/2017", passed: false},
            { id: "4", test: "Aerodynamics checkup", teststart: "1/6/2017", testend: "1/9/2017", passed: true},
            { id: "5", test: "Balance and stability checkup", teststart: "1/10/2017", testend: "1/12/2017", passed: true},
            { id: "6", test: "Report", teststart: "", testend: "", closed: false }
        ];
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//        var mydata = this.state.dbData;

        jQuery("#grid100").jqGrid({
            data:mydata,
            colNames: ['test','passed','test started','test ended'],
            colModel: [
                {name: 'test', index: 'test', width: 220 },
                {name: 'passed', index: 'passed', width: 60, align: 'center',
                    edittype: 'checkbox',
                    editoptions: {value: 'Yes:No', defaultValue: 'Yes'},
                    formatoptions: { disabled: false},
                    cellattr: function(rowId, tv, rawObject, cm, rdata) {
                        if (Number(rowId) == 6) { return ' colspan="3"'  }},
                    formatter:function(cellvalue, options, rowObject)
                    {
                        if(rowObject.id==6)
                        {
                            return '<input type="text" id="txtnotes" ref="refnotes" />';
                        }
                        else
                        {
                            if(rowObject.passed===true)
                            {                                   

                                return '<input type="checkbox"  id="cbPassed-'+ rowObject.id +'"  checked/>';
                            }
                            else
                            {
                                return '<input type="checkbox"  id="cbPassed-'+rowObject.id+ '"  />';
                            }
                        }
                    }

                },
                {name: 'teststart', index: 'teststart', width: 75, formatter: 'string', sorttype: 'string', align: 'center',
                    cellattr: function(rowId, tv, rawObject, cm, rdata) {
                        if (Number(rowId) == 6) { return ' style="display:none;"'  }}},//return ' colspan="5"'
                {name: 'testend', index: 'testend', width: 75, formatter: 'string', sorttype: 'string', align: 'center',
                    cellattr: function(rowId, tv, rawObject, cm, rdata) {
                        if (Number(rowId) == 6) { return ' style="display:none;"' }}}

            ],
            loadComplete:function(){
var iCol = getColumnIndexByName ($(this), 'passed'), rows = this.rows, i,
                    c = rows.length;


                for (i = 1; i < c; i += 1) {
                    $(rows[i].cells[iCol]).click(function (e) {
                        var id = $(e.target).closest('tr')[0].id,
                            isChecked = $(e.target).is(':checked');    

             $('#grid100').jqGrid('setCell', id, 'passed', isChecked);

           $('#grid100').jqGrid('getLocalRow', id).passed = isChecked;

                        return false;

                    });
                }


                    $(rows[6].cells[iCol]).change(function (e) {
                        console.log(e.target);
                        var id = $(e.target).closest('tr')[0].id,
                            newText = $(e.target).value;   

                $('#grid100').jqGrid('setCell', id, 'passed', newText);

                    });




            },
            rowNum: 10,
            rowList: [5, 10, 20],
            threeStateSort:true,
            autoencode: true,
            sortname: "id",
            viewrecords: true,
            sortorder: "desc",
            shrinkToFit: false,

        });

        jQuery("#grid100").jqGrid('setGroupHeaders', {
            useColSpanStyle: true,
            groupHeaders:[
                {startColumnName: 'passed', numberOfColumns: 3, titleText: 'Test Duration'}
            ]
        });
    },
    render:function(){
        return(<div id="gridContainer" ref="refContainer">
                <form>
                    <table id="grid100"></table>
                </form>
            </div>
        )
    }
})

var getColumnIndexByName = function(grid, columnName) {
    var cm = grid.jqGrid('getGridParam', 'colModel'), i, l;
    for (i = 1, l = cm.length; i < l; i += 1) {
        if (cm[i].name === columnName) {
            return i;
        }
    }
    return -1;
};


ReactDOM.render(<SampleGrid />, document.getElementById('divContainer'));

In the loadComplete() i have added code for change() event to the textbox. but it does not work. Still if i type something from keyboard textbox wont accept any keystrokes.

Why this happens and how do i fix this?

Oleg
  • 220,925
  • 34
  • 403
  • 798
Dore.Ad
  • 163
  • 2
  • 10
  • Sorry, but I still don't understand the goal of the "Report" row, which you try to insert in the middle of other data. Where you plan to save the data from `#txtnotes`? You don't fill any data in the input filed of the "Report" row initially. You try to save `newText` in the Boolean field `passed` of the item with id=6. It's unclear whether it could be **only one** such special row. Which relation has the report to other data? Could one have more as one such "Report" rows? The current code allows to move the "Report" rows row on any place in the grid (not only on top on on the bottom). – Oleg Feb 19 '17 at 11:08
  • 1
    The code which you included in `loadComplete` is bad. Every loop over all items inside of `loadComplete` with *modification* of existing DOM elements makes the grid slowly. I suggest you to look [the demo](http://www.ok-soft-gmbh.com/jqGrid/EditableCheckbox.htm), which I created for [the answer](http://stackoverflow.com/a/24239416/315935). It uses `beforeSelectRow` instead of binding of `click` to **every checkbox** of the column `passed`. – Oleg Feb 19 '17 at 11:24
  • I can't gives you advices about "onchange" event, because I don't understand what you need to implement. In general `contenteditable=true` attribute and "input" event instead of "change" event seems to me the better way. – Oleg Feb 19 '17 at 11:26
  • @Oleg the purpose of the 'Report' row is to add an empty row as the last row so i can create a row on the grid and merge the cells and insert a textbox – Dore.Ad Feb 19 '17 at 11:57
  • @Oleg regarding onChange use: my purpose is to make the textbox accept keystrokes but it did not work out though. Is there way to make it accept the key strokes? – Dore.Ad Feb 19 '17 at 12:00
  • @Oleg Good news : ) once removed loadComplete() and use beforeSelecrRow as suggested on your second comment i was able to enter keystrokes in the textbox. This basically re-solved two issues 1. checkbox check state changes are updated with the dataset under the hood so sorting shows with updated checkboxes 2. Textbox now accepts keystrokes – Dore.Ad Feb 19 '17 at 12:10
  • Back to the problem with "Reports" row. Can one have more as one rows? Should the row be display on top of the grid or in the middle after sorting by first column (`test` column)? Should be the data from the input field be **initialized always as empty**. Where (in which part of data/state) should be saved the modified text of th "Reports" row? – Oleg Feb 19 '17 at 12:24
  • I still think that the report row has no direct relation to the main data. I suppose that the best way to display the row is the usage of the footer/summary row of the grid. One can add `footerrow: true` to display the row. One can use `userDataOnFooter: true` option with `userdata` part of the source data to fill the footer row or one can use `footerData` method alternatively. – Oleg Feb 19 '17 at 12:28
  • @Oleg hmmm... seems like a good idea to put the textbox on the footer but for the moment i would like the textbox to be part of the last row : ) Assume the last is the last row and it always have reports text for 'test' user input by textbox – Dore.Ad Feb 19 '17 at 12:56
  • If you mix the data with the row, then your final code will be essential longer. All the `cellattr` and `formatter` are **absolutely** unneeded for the main grid. If you will force that the "Report" row be displayed as the last row, then you will have to prevent sorting in the "test" column or to define custom sorting (`sorttype` as function) for the column. I can continue... You still don't answered on the question **where in the local data you need to save the text from "Report" row?** and **can the "Report" row contains some data at the start of the grid?**. – Oleg Feb 19 '17 at 13:01

0 Answers0