File: columns.createdCell.js

package info (click to toggle)
datatables.js 1.10.13%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,232 kB
  • ctags: 1,329
  • sloc: xml: 10,249; php: 4,387; sh: 492; makefile: 21
file content (175 lines) | stat: -rw-r--r-- 4,620 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//Unsure how to test
describe( "column.createdCell option", function() {
	dt.libs( {
		js:  [ 'jquery', 'datatables' ],
		css: [ 'datatables' ]
	} );

	describe("Check the defaults", function () {
		dt.html( 'basic' );
		it("Default should be null", function () {
			$('#example').dataTable();
			expect($.fn.dataTable.defaults.aoColumnDefs).toBe(null); //Couldn't find default for fnCreatedCell
		});
		dt.html( 'basic' );
		it("Apply red color to any office that equals 'London'", function () {
				$('#example').dataTable({
					"columnDefs": [{
						"targets": 2,
						"createdCell": function (td, cellData, rowData, row, col) {
							if (cellData == "London"){
								$(td).css('color','red');
							}
						}
					}] //columnDefs
				});
				expect($('#example tbody tr:eq(1) td:eq(2)').attr('style') === "color: red;").toBe(true);
		});
		dt.html( 'basic' );
		it("createdCell is called once for each cell on init (total 342 cells from 57 rows * 6 cells per row)", function () {
			 tmp = 0;
			$('#example').dataTable( {
				"columnDefs": [{
					"targets": "_all",
					"createdCell": function (td, cellData, rowData, row, col) {
						tmp++;
					}
				}] //columnDefs
			});
			expect(tmp === 342).toBe(true);
		});

		it("createdCell is not called on other draws", function () {
			$('#example th:eq(1)').click();
			expect(tmp === 342).toBe(true);
		});

		//Testing arguments
		dt.html( 'basic' );
		it("5 arguments for the function", function () {
			tmp = true;
			$('#example').dataTable( {
				"columnDefs": [{
					"targets": "_all",
					"createdCell": function (cell, cellData, rowData, rowIndex, colIndex){

						if (arguments.length !== 5){
							tmp = false;
						}
					}
				}] //columnDefs
			});
			expect(tmp === true).toBe(true);
		});
		dt.html( 'basic' );
		it("First argument is a td element", function () {
			tmp = true;
			$('#example').dataTable( {
				"columnDefs": [{
					"targets": "_all",
					"createdCell": function (cell, cellData, rowData, rowIndex, colIndex){
						if (arguments[0].nodeName !== "TD"){
							tmp = false;
						}
					}
				}] //columnDefs
			});
			expect(tmp === true).toBe(true);
		});
		dt.html( 'basic' );
		it("Second argument is the data for the cell", function () {
			tmp= false;
			var table1 = $('#example').dataTable( {
				"columnDefs": [{
					"targets": 0,
					"createdCell": function (cell, cellData, rowData, rowIndex, colIndex){
						if (arguments[1] == "Tiger Nixon" && rowIndex === 0){
							tmp = true;
						}
					}
				}]
			});
			expect(tmp === true).toBe(true);
		});
		dt.html( 'basic' );
		it("Third argument is the data for the whole row", function () {
			tmp= false;
			$('#example').dataTable( {
				"columnDefs": [{
				"targets": "_all",
				"createdCell": function (cell, cellData, rowData, rowIndex, colIndex){
					if (rowIndex === 0 && rowData == "Tiger Nixon", "System Architect", "Edinburgh", "61", "2011/04/25", "$320,800"){
						tmp = true;
					}
				}
			}]
			});
			expect(tmp === true).toBe(true);
		});
		dt.html( 'basic' );
		it("4th argument is the row Index", function () {
			tmp= false;
			$('#example').dataTable( {
				"columnDefs": [{
				"targets": "_all",
				"createdCell": function (cell, cellData, rowData, rowIndex, colIndex){
					if (cellData === "Tiger Nixon" && rowIndex === 0 ){
						tmp = true;
					}
				}
			}]
			});
			expect(tmp === true).toBe(true);
		});
		dt.html( 'basic' );
		it("5th argument is the column itself", function () {
			colI = 12;
			$('#example').dataTable( {
				"columnDefs": [{
				"targets": 0,
				"createdCell": function (cell, cellData, rowData, rowIndex, colIndex){
					colI = colIndex;
				}
			}]
			});
			expect(colI).toBe(0);
		});
		dt.html( 'basic' );
		it("cell is tied to the correct data", function () {
			tmp = false;
			$('#example').dataTable( {
				"columnDefs": [{
				"targets": 0,
				"createdCell": function (cell, cellData, rowData, rowIndex, colIndex){
					if( rowIndex === 0){
						console.log($('td'));
						if($('td').html() == "Tiger Nixon"){
							tmp = true;
						}
					}
				}
			}]
			});
			expect(tmp).toBe(true);
		});
		dt.html( 'basic' );
		it("createdCell allows manipulation of TD elements", function () {
			tmp = false;
			$('#example').dataTable( {
				"columnDefs": [{
				"targets": "_all",
				"createdCell": function (cell, cellData, rowData, rowIndex, colIndex){
					if( colIndex === 3){
						if (cellData >= 30){
							$(cell).addClass('unit-test');
						}
					}
				}
			}]
			});
			expect($('#example tbody tr:eq(0) td:eq(3)').hasClass('unit-test')).toBe(true);
		});
	
	});

});