File: columns_default_content.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 (119 lines) | stat: -rw-r--r-- 2,842 bytes parent folder | download | duplicates (4)
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
describe('columns.defaultContent option', function() {
	dt.libs({
		js: ['jquery', 'datatables'],
		css: ['datatables']
	});

	describe('Check the defaults', function() {
		dt.html('basic');
		it('Default is null', function() {
			$('#example').dataTable();
			expect($.fn.DataTable.defaults.column.sDefaultContent).toBe(null);
		});

		dt.html('basic');
		it('Use default content to add static value to column- using columns', function() {
			$('#example').dataTable({
				columns: [
					{
						data: null,
						defaultContent: '<button>Not set</button>'
					},
					null,
					null,
					null,
					null,
					null
				]
			});
			expect($('#example tbody tr:eq(0) td:eq(0) button').text()).toBe('Not set');
		});

		dt.html('basic');
		it('use defaultContent to add button to column- using columnDefs', function() {
			$('#example').dataTable({
				columnDefs: [
					{
						data: null,
						targets: 0,
						defaultContent: '<button>Not set</button>'
					}
				]
			});
			expect($('#example tbody tr:eq(0) td:eq(0) button').text()).toBe('Not set');
		});

		function makeData(wantNull) {
			var a = [];
			var name;
			for (var i = 0; i < 10; i++) {
				name = i % 2 ? (wantNull ? null : undefined) : i;
				e = { name: name, position: i, office: i, age: i, start_date: i, salary: i };
				a.push(e);
			}
			return a;
		}

		dt.html('basic');
		it('ensure nulls are replaced by the defaultContent', function() {
			var table = $('#example').DataTable({
				ajax: function(data, callback, settings) {
					var out = makeData(true);
					callback({
						draw: data.draw,
						data: out,
						recordsTotal: 10,
						recordsFiltered: 10
					});
				},
				columns: [
					{ data: 'name', title: 'Name', defaultContent: 'Fred' },
					{ data: 'position', title: 'Position' },
					{ data: 'office', title: 'Office' },
					{ data: 'age', title: 'Age' },
					{ data: 'start_date', visible: false },
					{ data: 'salary', visible: false }
				]
			});

			expect(
				table
					.cells(null, 0)
					.render('display')
					.unique()
					.count()
			).toBe(6);
		});

		dt.html('basic');
		it('ensure undefined are replaced by the defaultContent', function() {
			var table = $('#example').DataTable({
				ajax: function(data, callback, settings) {
					var out = makeData(false);
					callback({
						draw: data.draw,
						data: out,
						recordsTotal: 10,
						recordsFiltered: 10
					});
				},
				columns: [
					{ data: 'name', title: 'Name', defaultContent: 'Fred' },
					{ data: 'position', title: 'Position' },
					{ data: 'office', title: 'Office' },
					{ data: 'age', title: 'Age' },
					{ data: 'start_date', visible: false },
					{ data: 'salary', visible: false }
				]
			});

			expect(
				table
					.cells(null, 0)
					.render('display')
					.unique()
					.count()
			).toBe(6);
		});
	});
});