如何在jqxDatatable列上应用样式

时间:2015-10-29 14:26:58

标签: javascript jquery jqxgrid jqwidget

我是JqWidget世界的新手。我有一个表,我希望一个列值显示为粗体。我不知道如何在列

上应用css

2 个答案:

答案 0 :(得分:1)

您可以使用cellClassName列属性。

要了解具体方法,请参阅:

<!DOCTYPE html>
<html lang="en">
<head>
    <title id='Description'>The sample illustrates how to add custom CSS styles to DataTable cells under specific conditions.</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>
    <script type="text/javascript">
        $(document).ready(function () {
            var url = "../sampledata/products.xml";
            // prepare the data
            var source =
            {
                dataType: "xml",
                dataFields: [
                    { name: 'ProductName', type: 'string' },
                    { name: 'QuantityPerUnit', type: 'int' },
                    { name: 'UnitPrice', type: 'float' },
                    { name: 'UnitsInStock', type: 'float' },
                    { name: 'Discontinued', type: 'bool' }
                ],
                root: "Products",
                record: "Product",
                id: 'ProductID',
                url: url
            };
            var cellClass = function (row, dataField, cellText, rowData) {
                var cellValue = rowData[dataField];
                switch (dataField) {
                    case "QuantityPerUnit":
                        if (cellValue < 11) {
                            return "min";
                        }
                        if (cellValue < 14) {
                            return "minavg";
                        }
                        if (cellValue < 50) {
                            return "avg";
                        }
                        return "max";
                    case "UnitPrice":
                        if (cellValue < 20) {
                            return "min";
                        }
                        if (cellValue < 30) {
                            return "minavg";
                        }
                        if (cellValue < 50) {
                            return "avg";
                        }
                        return "max";
                    case "UnitsInStock":
                        if (cellValue < 20) {
                            return "min";
                        }
                        if (cellValue < 30) {
                            return "minavg";
                        }
                        if (cellValue < 50) {
                            return "avg";
                        }
                        return "max";
                }
            }
            var dataAdapter = new $.jqx.dataAdapter(source, {
                downloadComplete: function (data, status, xhr) { },
                loadComplete: function (data) { },
                loadError: function (xhr, status, error) { }
            });
            // initialize jqxDataTable
            $("#dataTable").jqxDataTable(
            {
                source: dataAdapter,                
                pageable: true,
                sortable: true,
                altRows: true,
                selectionMode: 'none',
                enableHover: false,
                columns: [
                  { text: 'Product Name', dataField: 'ProductName', width: 200 },
                  { text: 'Quantity per Unit', dataField: 'QuantityPerUnit', cellClassName: cellClass, cellsAlign: 'right', align: 'right', width: 200 },
                  { text: 'Unit Price', dataField: 'UnitPrice', align: 'right', cellClassName: cellClass, cellsAlign: 'right', cellsformat: 'c2', width: 200 },
                  { text: 'Units In Stock', dataField: 'UnitsInStock', cellsAlign: 'right', width: 250, align: 'right', cellClassName: cellClass}
                ]
            });
        });
    </script>
</head>
<body class='default'>
       <style>
        .max {
            color: black\9;
            background-color: #63be7b\9;
        }
        .avg {
            color: black\9;
            background-color: #f8e984\9;
        }
        .minavg {
            color: black\9;
            background-color: #f9806f\9;
        }
        .min {
            color: black\9;
            background-color: #f8696b\9;
        }
        .max:not(.jqx-grid-cell-hover):not(.jqx-grid-cell-selected), .jqx-widget .max:not(.jqx-grid-cell-hover):not(.jqx-grid-cell-selected) {
            color: black;
            background-color: #63be7b;
        }
        .avg:not(.jqx-grid-cell-hover):not(.jqx-grid-cell-selected), .jqx-widget .avg:not(.jqx-grid-cell-hover):not(.jqx-grid-cell-selected) {
            color: black;
            background-color: #f8e984;
        }
        .minavg:not(.jqx-grid-cell-hover):not(.jqx-grid-cell-selected), .jqx-widget .minavg:not(.jqx-grid-cell-hover):not(.jqx-grid-cell-selected) {
            color: black;
            background-color: #f9806f;
        } 
        .min:not(.jqx-grid-cell-hover):not(.jqx-grid-cell-selected), .jqx-widget .min:not(.jqx-grid-cell-hover):not(.jqx-grid-cell-selected) {
            color: black;
            background-color: #f8696b;
        }
    </style>
    <div id="dataTable">
    </div>
</body>
</html>

示例:http://www.jqwidgets.com/jquery-widgets-demo/demos/jqxtabs/integration.htm?arctic

答案 1 :(得分:0)

我同意Scripto的答案。另一种方法是,在列定义中添加cellrenderer函数。

例如:

var cellsrenderer = function (row, columnfield, value, defaulthtml, columnproperties) {

            var theStatus = $("#jqxgrid").jqxGrid('getrowdata', row).StatusID;
            if (theStatus == 0) {
                return '<span style="margin: 4px;float: ' + columnproperties.cellsalign + ';color: #ff0000;font-weight: bold">' + value + '</span>';
            }
            else if (theStatus == 2) {
                return '<span style="margin: 4px;float: ' + columnproperties.cellsalign + ';color: #006638;font-weight: bold">' + value + '</span>';
            }
            else if (theStatus == 3) {
                return '<span style="margin: 4px;float: ' + columnproperties.cellsalign + ';color: #9B30FF;font-weight: bold">' + value + '</span>';
            }
        };

所以我们的列定义如下所示。

columns: [ { text: 'Number', datafield: 'InvoiceNumber', width: 50, cellsalign: 'right', cellsrenderer: cellsrenderer }]

我希望这会有所帮助。