PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / trunk
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses vtrunk
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / assets / src / js / admin / statistics / data-table.js

data-table.js in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses trunk, at assets/src/js/admin/statistics/data-table.js

111 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Data table renderer — createElement/textContent only, no innerHTML.
3 *
4 * Emits the plugin's shared table markup ( .lp-table-wrap > table.lp-list-table,
5 * per TableListTemplate ) so the dashboard widgets match every other LearnPress
6 * table. The extra lp-stats-table class carries the stats-only behaviours
7 * ( row hover/highlight, clickable performance rows, empty state ).
8 *
9 * Column definition:
10 * { key, label,
11 * format?: ( value, row ) => string|Node // Node for links etc.
12 * badge?: ( row ) => 'green'|'yellow'|'red'|'' // wraps the cell value
13 * csv?: ( row ) => string // plain value for CSV export (Nodes can't export)
14 * }
15 *
16 * @since 4.4.2
17 * @version 1.1.0
18 */
19
20 import { getStatsI18n } from './api.js';
21
22 /**
23 * @param {Element} elContainer Container emptied and refilled.
24 * @param {Array} columns Column definitions.
25 * @param {Array} rows Row objects keyed by column key.
26 * @param {Object} options { emptyText?: string }.
27 * @return {Object} { columns, rows } handle for csv/modal reuse.
28 */
29 export const renderDataTable = (
30 elContainer,
31 columns = [],
32 rows = [],
33 options = {}
34 ) => {
35 if ( ! elContainer ) {
36 return { columns, rows };
37 }
38
39 elContainer.textContent = '';
40
41 const wrap = document.createElement( 'div' );
42 wrap.className = 'lp-table-wrap';
43
44 const table = document.createElement( 'table' );
45 table.className = 'lp-list-table lp-stats-table';
46
47 const thead = document.createElement( 'thead' );
48 const headRow = document.createElement( 'tr' );
49 columns.forEach( ( column ) => {
50 const th = document.createElement( 'th' );
51 th.textContent = column.label ?? '';
52 headRow.appendChild( th );
53 } );
54 thead.appendChild( headRow );
55 table.appendChild( thead );
56
57 const tbody = document.createElement( 'tbody' );
58
59 if ( ! rows.length ) {
60 const tr = document.createElement( 'tr' );
61 const td = document.createElement( 'td' );
62 td.colSpan = columns.length || 1;
63 td.className = 'lp-stats-table__empty';
64 td.textContent =
65 options.emptyText || getStatsI18n( 'noData', 'No data for this period.' );
66 tr.appendChild( td );
67 tbody.appendChild( tr );
68 } else {
69 rows.forEach( ( row ) => {
70 const tr = document.createElement( 'tr' );
71 columns.forEach( ( column ) => {
72 const td = document.createElement( 'td' );
73 const raw = row[ column.key ];
74 const output =
75 'function' === typeof column.format
76 ? column.format( raw, row )
77 : raw;
78
79 let cellNode;
80 if ( output instanceof Node ) {
81 cellNode = output;
82 } else {
83 cellNode = document.createTextNode(
84 null == output ? '' : String( output )
85 );
86 }
87
88 const badgeColor =
89 'function' === typeof column.badge ? column.badge( row ) : '';
90 if ( badgeColor ) {
91 const badge = document.createElement( 'span' );
92 badge.className = `lp-badge lp-badge--${ badgeColor }`;
93 badge.appendChild( cellNode );
94 td.appendChild( badge );
95 } else {
96 td.appendChild( cellNode );
97 }
98
99 tr.appendChild( td );
100 } );
101 tbody.appendChild( tr );
102 } );
103 }
104
105 table.appendChild( tbody );
106 wrap.appendChild( table );
107 elContainer.appendChild( wrap );
108
109 return { columns, rows };
110 };
111