jQWidgets Forums

jQuery UI Widgets Forums DataTable Change row background color

This topic contains 3 replies, has 2 voices, and was last updated by  Nadezhda 10 years, 5 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
  • Change row background color #63888

    jusmas
    Participant

    hi.. Is there a way to change the default background color of certain rows? If so, please give me the details on how to do it.

    Thanks,
    Justine

    Change row background color #63897

    Nadezhda
    Participant

    Hello Justine,

    Please, provide us more information:
    – How do you want to change the background color – when you initialize the jqxDataTable or dynamically?
    – Is there any condition when you want to change the background color?

    Best Regards,
    Nadezhda

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

    Change row background color #63915

    jusmas
    Participant

    I prefer a color change during table load itself. For example, a specific DB field – say ‘flag_test’ contain values either 0 or 1 and need to set the row background color when ‘flag_test’ =1. I wont be displaying that field in the data table.

    How can I make it possible? Please help me…

    Thanks,
    Justine

    Change row background color #64175

    Nadezhda
    Participant

    Hi Justine,

    Here is an example which shows how to change the background color of field in table when ‘flag_test’ is equal to 1.

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <title></title>    
        <link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" />
        <script type="text/javascript" src="../../scripts/jquery-1.11.1.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/jqxdatatable.js"></script>
        <script type="text/javascript" src="../../scripts/demos.js"></script>
        <style>
            .colorField {
                background-color: green;
            }
        </style>
        <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 < 200; 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",
                    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' }
                    ]
                };
                var cellclassname = function (row, column, value, data) {
                    var selection = $("#table").jqxDataTable('getSelection');
                    for (var i = 0; i < selection.length; i++) {
                        // get a selected row.
                        var rowData = selection[i];
                        if (flag_test == 1) {
                            return "colorField";
                        }
                    }
                }
                var dataAdapter = new $.jqx.dataAdapter(source);
    
                $("#table").jqxDataTable(
                {
                    width: 850,
                    pageable: true,
                    pagerButtonsCount: 10,
                    source: dataAdapter,
                    columnsResize: true,
                    columns: [
                      { text: 'Name', dataField: 'firstname', width: 200, cellclassname: cellclassname },
                      { text: 'Last Name', dataField: 'lastname', width: 200, cellclassname: cellclassname },
                      { text: 'Product', editable: false, dataField: 'productname', width: 180, cellclassname: cellclassname },
                      { text: 'Quantity', dataField: 'quantity', width: 80, cellsAlign: 'right', align: 'right', cellclassname: cellclassname },
                      { text: 'Unit Price', dataField: 'price', width: 90, cellsAlign: 'right', align: 'right', cellsFormat: 'c2', cellclassname: cellclassname },
                      { text: 'Total', dataField: 'total', cellsAlign: 'right', align: 'right', cellsFormat: 'c2', cellclassname: cellclassname }
                    ]
                });
            });
        </script>
    </head>
    <body class='default'>
        <div id="table">
        </div>
    </body>
    </html>

    Best Regards,
    Nadezhda

    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.