jQWidgets Forums

jQuery UI Widgets Forums Grid How to make a cell in the Grid as non editable based on cell value

This topic contains 9 replies, has 3 voices, and was last updated by  Clarence 10 years, 8 months ago.

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

  • DollyB
    Member

    Hi,

    Can anyone suggest me how to disable a cell in the Grid based on its value?

    Thankx
    DolyB


    Dimitar
    Participant

    Hello DollyB,

    Here is an example – if a cell value is less than 5, the cell cannot be edited. You can write a condition of your choice.

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <title id='Description'>This example shows how to create a Grid from Array data.</title>
    <link rel="stylesheet" href="jqwidgets/styles/jqx.base.css" type="text/css" />
    <script type="text/javascript" src="jquery-1.7.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/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="jqwidgets/jqxwindow.js"></script>
    <script type="text/javascript" src="jqwidgets/jqxbutton.js"></script>
    <script type="text/javascript">
    $(document).ready(function () {
    // prepare the data
    var data = new Array();
    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"
    ];
    for (var i = 0; i < 100; 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;
    data[i] = row;
    }
    var source =
    {
    localdata: data,
    datatype: "array"
    };
    var dataAdapter = new $.jqx.dataAdapter(source, {
    loadComplete: function (data) { }
    });
    $("#jqxgrid").jqxGrid(
    {
    selectionmode: 'singlecell',
    editable: true,
    editmode: 'click',
    width: 670,
    source: dataAdapter,
    columns: [
    { text: 'First Name', datafield: 'firstname', width: 100 },
    { text: 'Last Name', datafield: 'lastname', width: 100 },
    { text: 'Product', datafield: 'productname', width: 180 },
    { text: 'Quantity', datafield: 'quantity', width: 80, cellsalign: 'right' },
    { text: 'Unit Price', datafield: 'price', width: 90, cellsalign: 'right', cellsformat: 'c2' },
    { text: 'Total', datafield: 'total', width: 100, cellsalign: 'right', cellsformat: 'c2' }
    ]
    });
    $("#jqxgrid").bind('cellbeginedit', function (event) {
    var column = args.datafield;
    var row = args.rowindex;
    var value = args.value;
    // condition follows
    if (value<5) {
    $("#jqxgrid").jqxGrid('endcelledit', row, column, true);
    }
    });
    });
    </script>
    </head>
    <body class='default'>
    <div id='jqxWidget' style="font-size: 13px; font-family: Verdana; float: left;">
    <div id="jqxgrid"></div>
    </div>
    </body>
    </html>

    Best Regards,
    Dimitar

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


    DollyB
    Member

    Thank You.

    I face one more issue now. I set one of the column cell as dropdownlist when I try to edit the cell.
    I get this.listbox is null or not an object message when I click on dropdown. The dropdown is set for few cells itself.

    Could you tell me why I am getting this message and why I am not able to see the dropdown for few cells?

    var reqJsonAttrTemplate =  eval('(' + '<%=sJsonTemplate%>' + ')');	
    var keys = [], props = [], rangeValues = [];
    var dataf = [];
    var row = {};
    var i = 0;
    row["text"] ="name";
    row["datafield"] ="name";
    row["columntype"] = 'textbox';
    row["width"] = 100;
    row["pinned"] = true;
    row["editable"] = false;
    reqGridColumns[i] = row;
    for (var attribute in reqJsonAttrTemplate) {
    i++;
    row = {};
    props = reqJsonAttrTemplate [attribute];
    rangeValues = props["Range Values"];
    row["datafield"] = attribute;
    row["text"] = props["Caption"]; //attribute +"["+props["Caption"]+"]";
    //Column Type Validation
    if(props["Integer"] == "true"){
    alert('Integer: '+attribute);
    row["columntype"] = 'numberinput';
    row["initeditor"] = function (row, cellvalue, editor) {editor.jqxNumberInput({decimalDigits: 0, digits: 3});};
    }else if(props["Date"] == "true"){
    alert('Date: '+attribute);
    row["columntype"] = 'datetimeinput';
    }else if(props["Range Use"] == "true" && props["Range Multiple"] == "false"){
    alert('RangeUse and Multiple: '+attribute);
    row["columntype"] = 'dropdownlist';
    }else{
    alert('Else: '+attribute);
    row["columntype"] = 'textbox';
    }
    row["width"] = 200;
    if(attribute == "Alias Tag"){
    row["editable"] = false;
    }
    reqGridColumns[i] = row;
    }
    // initialize jqxGrid Requirements
    $("#jqxgridDR").jqxGrid(
    {
    width: 700,
    source: reqDataAdapter,
    editable: true,
    pageable: true,
    editmode: 'dblclick',
    autoheight: true,
    theme: theme,
    selectionmode: 'singlecell',
    columns: [
    { text: 'name', datafield: 'name', columntype: 'textbox', width: 100, pinned: true, editable: false }
    ]
    });
    // change columns collection. Requirments
    $("#jqxgridDR").jqxGrid({ columns: reqGridColumns });

    And my JSonTemplate data looks like,

    {
    “RTC ID”:{
    “Admin”:”false”,
    “Force Modify”:”false”,
    “Caption”:”RTC ID”,
    “Description”:”Description11″,
    “Alias Tag”:”false”,
    “Subset Use”:”false”,
    “Range Values”:”[]”,
    “Range Use”:”false”,
    “Range Multiple”:”false”,
    “Range Item”:””,
    “Range Locked”:”false”,
    “Subset Values”:”[]”,
    “ROWS”:”1″,
    “Basic”:”false”,
    “Subset Master”:”false”,
    “Sequence Order”:”0″,
    “Date”:”false”,
    “Sort Order”:”61″,
    “Mandatory”:”false”,
    “Locked”:”false”,
    “Integer”:”false”

    },

    “Sub Category”:{
    “Admin”:”false”,
    “Force Modify”:”false”,
    “Caption”:”Sub Category”,
    “Description”:”Description16″,
    “Alias Tag”:”false”,
    “Subset Use”:”false”,
    “Range Values”:”[None, Counter, Event]”,
    “Range Use”:”true”,
    “Range Multiple”:”true”,
    “Range Item”:”list”,
    “Range Locked”:”false”,
    “Subset Values”:”[]”,
    “ROWS”:”1″,
    “Basic”:”false”,
    “Subset Master”:”false”,
    “Sequence Order”:”0″,
    “Date”:”false”,
    “Sort Order”:”13″,
    “Mandatory”:”false”,
    “Locked”:”false”,
    “Integer”:”false”

    }}


    Dimitar
    Participant

    Hello DollyB,

    You can’t set a dropdownlist editor in a such way and only for some of the cells in a column. You can set one editor per column as it is demonstrated in the demos.

    Best Regards,
    Dimitar

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


    Clarence
    Participant

    Hi,

    Even i have the same requirement to disable a cell in the Grid based on its value.

    $(“#jqxgrid”).bind(‘cellbeginedit’, function (event) {

    // Logic to disable column
    });

    The above cellbeginedit code gets executed only when there is any change in the row but my requirement is to disable/enable the cell when the whole data gets populated in the grid at the start.

    Is this possible ?


    Clarence
    Participant

    Below is the sample code which is not working

    if the value is Test1 then the column should be enabled else if Test2 then disabled

    var cellclass = function (row, columnfield, value) {

    if (value == “Test1”) {
    $(“#jqxgrid”).jqxGrid(‘endcelledit’, row, columnfield, true);
    }
    else if (value == “Test2”) {
    $(“#jqxgrid”).jqxGrid(‘endcelledit’, row, columnfield, false);
    }
    }

    $(“#jqxgrid”).jqxGrid(
    {
    width: 1100,
    source: dataAdapter,
    columnsresize: true,
    sortable: true,
    editable: true,
    pageable: true,
    autoheight: true,
    filterable: true,
    pagesize: 1000,
    enabletooltips: true,
    columns: [
    { text: ‘1’, datafield: ‘1’, width: 250, editable: false },
    { text: ‘2’, datafield: ‘2’, width: 100, cellclassname: cellclass, editable: false },
    { text: ‘3’, datafield: ‘3’, width: 100, editable: false },
    { text: ‘4’, datafield: ‘4’, width: 150 },
    { text: ‘5’, datafield: ‘5’, width: 200 },
    {
    text: ‘6’, datafield: ‘6’, width: 150, displayfield: ‘Region’, columntype: ‘dropdownlist’,
    createeditor: function (row, value, editor) {
    editor.jqxDropDownList({ source: regionssAdapter, displayMember: ‘REGION_NAME’, valueMember: ‘6’ });
    }
    },
    {
    text: ‘7’, datafield: ‘7’, width: 150, displayfield: ‘ECS_GROUP’, columntype: ‘dropdownlist’,
    createeditor: function (row, value, editor) {
    editor.jqxDropDownList({ source: ECSGroupAdapter, displayMember: ‘Text’, valueMember: ‘7’ });
    }
    }

    ]
    });


    Dimitar
    Participant

    Hello Clarence,

    It is wrong to use cellclassname this way. Use the column’s cellbeginedit callback function instead, e.g.:

    { text: 'Product', columntype: 'dropdownlist', datafield: 'productname', width: 177,
        cellbeginedit: function (row, datafield, columntype) {
            var value = $('#jqxGrid').jqxGrid('getcellvalue', row, datafield);
            if (value == "Test2")
                return false;
        }
    }

    Best Regards,
    Dimitar

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


    Clarence
    Participant

    Hi,

    In my above example, I need to disable column 6 (displayfield: ‘Region’) on basis of column 2 (datafield: ‘2’).

    i.e if Column 2 has value Test2, i need to disable the dropdown of column 6.

    Can you please share me the code for the above mentioned scenario.

    Thanks.


    Dimitar
    Participant

    Hi Clarence,

    It should be something along these lines:

    {
        text: '6', datafield: '6', width: 150, displayfield: 'Region', columntype: 'dropdownlist',
        cellbeginedit: function(row, datafield, columntype) {
            var value = $('#jqxgrid').jqxGrid('getcellvalue', row, '2');
            if (value == "Test2")
                return false;
        }
    },

    Best Regards,
    Dimitar

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


    Clarence
    Participant

    Thanks Dimitar. Working as expected.

    Thanks a lot

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

You must be logged in to reply to this topic.