var grid = new tui.Grid({
el: $('#grid'),
data: gridData,
rowHeaders: ['rowNum', 'radio'],
bodyHeight: 300,
rowHeight: 40,
virtualScrolling: true,
treeColumnOptions: {
name: 'name',
useCascadingCheckbox: false
},
columns: [
{
title: 'Name',
name: 'name',
width: 300,
editOptions: {
type: 'text',
useViewMode: true
}
},
{
title: 'Artist',
name: 'artist'
},
{
title: 'Type',
name: 'type'
},
{
title: 'Release',
name: 'release'
},
{
title: 'Genre',
name: 'genre'
}
]
});
function appendRows(parentRowKey, isDynamicLoaing) {
var childrenRowData = [
{
id: 502796,
name: 'test4',
artist: 'test4',
release: '2015.05.12',
type: 'EP',
typeCode: '2',
genre: 'Hiphop',
genreCode: '5',
grade: '5',
price: 18000,
downloadCount: 1000,
listenCount: 5000
},
{
id: 502790,
name: 'test5',
artist: 'test5',
release: '2015.05.12',
type: 'EP',
typeCode: '2',
genre: 'Hiphop',
genreCode: '5',
grade: '5',
price: 18000,
downloadCount: 1000,
listenCount: 5000
}
];
grid.appendRow([{
id: 502795,
name: 'test1',
artist: 'test1',
release: '2015.05.12',
type: 'EP',
typeCode: '2',
genre: 'Hiphop',
genreCode: '5',
grade: '5',
price: 18000,
downloadCount: 1000,
listenCount: 5000,
_extraData: {
treeState: 'COLLAPSE'
},
_children: isDynamicLoaing ? true : childrenRowData
},
{
id: 502793,
name: 'test2',
artist: 'test2',
release: '2015.05.12',
type: 'EP',
typeCode: '2',
genre: 'Hiphop',
genreCode: '5',
grade: '5',
price: 18000,
downloadCount: 1000,
listenCount: 5000
},
{
id: 502797,
name: 'test3',
artist: 'test3',
release: '2015.05.12',
type: 'EP',
typeCode: '2',
genre: 'Hiphop',
genreCode: '5',
grade: '5',
price: 18000,
downloadCount: 1000,
listenCount: 5000
}], {
parentRowKey: parentRowKey
});
}
function removeRows(rowKeys) {
_.each(rowKeys, function(rowKey) {
grid.removeRow(rowKey);
});
}
$('#append').on('click', function() {
var parentRowKey = grid.getCheckedRowKeys()[0];
appendRows(parentRowKey);
});
$('#remove').on('click', function() {
var rowKeys = grid.getCheckedRowKeys();
removeRows(rowKeys);
});
grid.on('expanded', function(ev) {
var parentRowKey = ev.rowKey;
var descendantRowKeys = ev.descendantRowKeys;
if (!descendantRowKeys.length) {
removeRows(descendantRowKeys);
appendRows(parentRowKey, true);
}
});