Rows can be assigned css styles in the JSON data directly
with pq_rowstyle
in local or remote data.
Cells can be assigned css styles in the JSON data directly
with pq_cellstyle
in local or remote data.
Columns can be assigned css styles with column.css
property
Cell styles can also be programmatically manipulated with Range.style() and Range.toggleStyle()
methods which can be bound
to toolbar or context menu UI.
x1
2
3<div id="grid_row_styles" style="margin:auto;"></div>
4
1831
2
3$(function () {
4var data = [
5{
6rank: 1, company: 'Exxon Mobil', revenues: 339938.0, profits: 36130.0,
7//cell based styles
8pq_cellstyle: {
9company: { color: '#ff0000', 'font-weight': 'bold' },
10revenues: { color: '#00ff00', 'font-size': '20px' },
11},
12//row based styles
13pq_rowstyle: { "background-color": '#ffff00' }
14},
15{ rank: 2, company: 'Wal-Mart Stores', revenues: 315654.0, profits: 11231.0 },
16{ rank: 3, company: 'Royal Dutch Shell', revenues: 306731.0, profits: 25311.0 },
17{ rank: 4, company: 'BP', revenues: 267600.0, profits: 22341.0 },
18{
19rank: 5, company: 'General Motors', revenues: 192604.0, profits: -10567.0,
20pq_cellstyle: {
21company: {
22border: "1px solid #ff0000"
23}
24}
25},
26{ rank: 6, company: 'Chevron', revenues: 189481.0, profits: 14099.0 },
27{ rank: 7, company: 'DaimlerChrysler', revenues: 186106.3, profits: 3536.3 },
28{ rank: 8, company: 'Toyota Motor', revenues: '185805.0', profits: '12119.6' },
29{ rank: 9, company: 'Ford Motor', revenues: '177210.0', profits: '2024.0' },
30{ rank: 10, company: 'ConocoPhillips', revenues: '166683.0', profits: '13529.0' },
31{ rank: 11, company: 'General Electric', revenues: '157153.0', profits: '16353.0' },
32{ rank: 12, company: 'Total', revenues: '152360.7', profits: '15250.0' },
33{ rank: 13, company: 'ING Group', revenues: '138235.3', profits: '8958.9' },
34{ rank: 14, company: 'Citigroup', revenues: '131045.0', profits: '24589.0' },
35{ rank: 15, company: 'AXA', revenues: '129839.2', profits: '5186.5' },
36{ rank: 16, company: 'Allianz', revenues: '121406.0', profits: '5442.4' },
37{ rank: 17, company: 'Volkswagen', revenues: '118376.6', profits: '1391.7' },
38{ rank: 18, company: 'Fortis', revenues: '112351.4', profits: '4896.3' },
39{ rank: 19, company: 'Crédit Agricole', revenues: '110764.6', profits: '7434.3' },
40{ rank: 20, company: 'American Intl. Group', revenues: '108905.0', profits: '10477.0' }
41];
42
43var obj = {
44scrollModel: {autoFit: true},
45height: 400,
46title: "Row, column & Cell Styles",
47selectionModel: { column: true },
48colModel: [
49{ title: "Rank", width: 100, dataType: "integer", dataIndx: "rank" },
50{ title: "Company", width: 220, dataType: "string", dataIndx: "company" },
51{
52title: "Revenues", width: 180, dataType: "float", dataIndx: "revenues",
53//column based styles
54style: {
55'font-style': 'italic'
56},
57styleHead: {
58'font-style': 'italic'
59}
60},
61{ title: "Profits", width: 160, dataType: "float", dataIndx: "profits" }
62],
63dataModel: {
64data: data
65},
66toolbar: {
67items: [
68{
69type: 'button',
70label: 'Export',
71listener: function () {
72var data = this.exportExcel();
73pq.saveAs(data, "pqgrid.xlsx");
74}
75},
76{
77type: 'button',
78label: "<b>B</b>",
79style: 'margin-right:0',
80listener: function (evt) {
81this.Selection().toggleStyle("font-weight", ["bold", "normal"]);
82}
83},
84{
85type: 'button',
86label: "<i>I</i>",
87style: 'margin-right:0;margin-left:0;',
88listener: function (evt) {
89this.Selection().toggleStyle("font-style", ["italic", "normal"]);
90}
91},
92{
93type: 'button',
94label: "<u>U</u>",
95style: 'margin-left:0',
96listener: function (evt) {
97this.Selection().toggleStyle("text-decoration", ["underline", "normal"]);
98}
99},
100{
101type: 'select',
102attr: 'title="font size"',
103cls: 'font-size',
104options: [9, 10, 11, 12, 14, 16, 20, 36],
105listener: function (evt) {
106this.Selection().style('font-size', $(evt.target).val() + "px");
107}
108},
109{
110type: 'select',
111attr: 'title="font family"',
112cls: 'font-family',
113options: ['Arial', 'Calibri', 'Caveat', 'Georgia', 'Impact', 'Verdana'],
114listener: function (evt) {
115this.Selection().style('font-family', $(evt.target).val());
116}
117},
118{
119type: 'select',
120//label:'font family: ',
121attr: 'title="horizontal align"',
122cls: 'align',
123options: ['left', 'center', 'right'],
124listener: function (evt) {
125this.Selection().align($(evt.target).val());
126}
127},
128{
129type: 'select',
130attr: 'title="vertical align"',
131cls: 'valign',
132options: [{ 'top': 'top', 'center': 'middle', 'bottom': 'bottom' }],
133listener: function (evt) {
134this.Selection().valign($(evt.target).val());
135}
136},
137{ type: 'separator' },
138{
139type: 'button',
140icon: 'ui-icon-arrowreturn-1-s',
141options: { text: false, showLabel: false, disabled: true },
142cls: 'undo',
143listener: function () {
144grid.history({ method: 'undo' });
145}
146},
147{
148type: 'button',
149cls: 'redo',
150icon: 'ui-icon-arrowrefresh-1-s',
151options: { text: false, showLabel: false, disabled: true },
152listener: function () {
153grid.history({ method: 'redo' });
154}
155}
156]
157},
158history: function (evt, ui) {
159var $tb = this.toolbar(),
160$undo = $tb.find(".undo"),
161$redo = $tb.find(".redo");
162
163if (ui.canUndo != null) {
164$undo.button("option", { disabled: !ui.canUndo });
165}
166if (ui.canRedo != null) {
167$redo.button("option", "disabled", !ui.canRedo);
168}
169}
170};
171var grid = pq.grid("#grid_row_styles", obj);
172grid.on('selectEnd history', function () {
173
174var S = this.Selection();
175
176$(".font-size").val(parseInt(S.style('font-size')));
177$(".font-family").val(S.style('font-family'));
178
179$(".valign").val(S.valign());
180$(".align").val(S.align());
181})
182});
183