File: createdRow.js

package info (click to toggle)
datatables.js 1.11.5%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 22,848 kB
  • sloc: javascript: 65,075; xml: 10,712; php: 4,741; sh: 544; makefile: 18
file content (184 lines) | stat: -rw-r--r-- 4,536 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
176
177
178
179
180
181
182
183
184
describe('createdRow option', function() {
	dt.libs({
		js: ['jquery', 'datatables'],
		css: ['datatables']
	});

	describe('Check the arguments', function() {
		let args;
		let count = 0;

		dt.html('basic');
		it('Called for each', function() {			
			$('#example').dataTable({
				createdRow: function() {
					args = arguments;
					count++;
				}
			});
			expect(count).toBe(57);
		});
		it('Four arguments for the function', function() {
			expect(args.length).toBe(4);
		});		
		it('First argument is a TR element', function() {
			expect(args[0].nodeName).toBe('TR');
		});
		it('Second Argument is an array with 6 elements', function() {
			expect(args[1].length).toBe(6);
		});

		dt.html('basic');
		it('Third argument is the data source for the row', function() {
			let goodRows = 0;
			$('#example').dataTable({
				createdRow: function() {
					if (arguments[1] === $('#example').DataTable.settings[0].aoData[arguments[2]]._aData) {
						goodRows++;
					}
				}
			});
			expect(goodRows).toBe(57);
		});

		dt.html('basic');
		it('Fourth argument is a node list of the cells in the row', function() {
			let goodRows = 0;
			$('#example').dataTable({
				createdRow: function() {
					if (goodRows++ === 0) {
						expect(arguments[3].length).toBe(6);
						expect(arguments[3][0] instanceof HTMLTableCellElement).toBe(true);
						expect($(arguments[3][0]).text()).toBe('Tiger Nixon');
					}
				}
			});
			expect(goodRows).toBe(57);
		});
	});

	describe('Check the basics', function() {

		dt.html('basic');
		it('Row created is called once for each row on init', function() {
			let count = 0;
			$('#example').dataTable({
				createdRow: function() {
					count++;
				}
			});
			expect(count).toBe(57);
		});

		dt.html('basic');
		it("Created isn't called back on other draws", function() {
			let count = 0;
			$('#example').DataTable({
				createdRow: function() {
					count++;
				}
			});
			$('#example th:eq(1)').click();
			expect(count).toBe(57);
		});

		dt.html('basic');
		it('TR element is tied to the correct data', function() {
			let tmp = false;
			$('#example').dataTable({
				createdRow: function(tr, data, index) {
					if (data[0] === 'Airi Satou') {
						if ($('td:eq(3)', tr).text() === '33') {
							tmp = true;
						}
					}
				}
			});
			expect(tmp).toBe(true);
		});

		dt.html('basic');
		it('createdRow allows manipulation of TR elements', function() {
			table = $('#example').dataTable({
				createdRow: function(row, data, dataIndex) {
					if (data[1] === 'Accountant') {
						$(row).addClass('unit-test1');
					}
				}
			});
			expect($('#example tbody tr:eq(0)').hasClass('unit-test1')).toBe(true);
		});

		dt.html('basic');
		it('createdRow dataIndex refers to correct rows/index', function() {
			$('#example').dataTable({
				createdRow: function(row, data, dataIndex) {
					if (dataIndex == 2) {
						$(row).addClass('unit-test2');
					}
				}
			});
			expect($('#example tbody tr:eq(2)').hasClass('unit-test2')).toBe(true);
		});

		dt.html('basic');
		it('createdRow row parameter is correct', function() {
			let counter = 0;
			$('#example').dataTable({
				createdRow: function(row, data, dataIndex) {
					counter++;
					$(row).addClass('row-' + counter);
				}
			});
			expect($('#example tbody tr:eq(0)').hasClass('row-5')).toBe(true);
		});
	});

	describe('Advanced conditions', function() {
		dt.html('basic');
		it('serverSide with AJAX sourced data', function(done) {
			let count = 0;
			let table = $('#example').DataTable({
				serverSide: true,
				ajax: dt.serverSide,
				createdRow: function(row, data, dataIndex) {
					count++;
					if (dataIndex == 2) {
						$(row).addClass('unit-test');
					}
				},
				initComplete: function(setting, json) {
					expect($('#example tbody tr:eq(2)').hasClass('unit-test')).toBe(true);
					expect(count).toBe(10);
					done();
				}
			});
		});

		dt.html('basic');
		it('deferRender with Ajax datafile', function(done) {
			let count = 0;
			let table = $('#example').DataTable({
				ajax: '/base/test/data/data.txt',
				deferRender: true,
				pageLength: 10,
				columns: dt.getTestColumns(),
				createdRow: function(row, data, dataIndex) {
					if (dataIndex == 4) {
						$(row).addClass('unit-test');
					}
					count++;
				},
				initComplete: function(settings, json) {
					expect($('#example tbody tr:eq(0)').hasClass('unit-test')).toBe(true);
					expect(count).toBe(10);

					table.page(2).draw(false);
					expect(count).toBe(20);

					done();
				}
			});
		});
	});
});