Skip to navigation
Open_file_dialog for grid
30.05.22
async function open_file_dialog(grid_name) { let el = document.createElement("input"); el.setAttribute('type', 'file'); el.addEventListener("change", load_file); let evt = new MouseEvent('click', { 'view': window, 'bubbles': true, 'cancelable': true }); //console.log('simulateClick', el, evt); if(! el.dispatchEvent(evt)) { log.info('evt got cancelled'); } else { log.info('success'); } async function load_file(e) { let _data = []; let col_names = {}; let col_model = jQuery(grid_name).jqGrid ('getGridParam', 'colModel'); for(let x in col_model) { col_names[col_model[x].name] = col_model[x].label; } let col = jQuery(grid_name).jqGrid('getColProp'); let reader = new FileReader(); let file = e.target.files[0]; //console.log(e.target.files[0]); reader.onload = async function(progressEvent) { let lines = this.result.split(/\r\n|\n/); let header = lines[0].split(/,/); let col_header = {}; let _data = []; for(let x in header) { if(col_names.hasOwnProperty(header[x])) { col_header[x] = col_names[header[x]]; } else if (Object.values(col_names).includes(header[x])) { let ix = Object.values(col_names).indexOf(header[x]); col_header[x] = Object.keys(col_names)[ix]; } } for(var line = 1; line < lines.length-1; line++) { let _row = lines[line].split(/,/); if(_row.length === Object.keys(col_header).length) { let row = {}; for(let i = 0; i < _row.length; i++) { const key = col_header[i]; const val = _row[i]; row[key] = val; } _data.push(row); } } //console.log(_data); //jQuery(grid_name).jqGrid('clearGridData'); jQuery(grid_name).jqGrid('setGridParam', { data: _data }).trigger("reloadGrid"); let new_doc = {'_id':'price_tool','data':_data}; try { let doc = await db.get('price_tool'); new_doc['_rev'] = doc._rev; let response = await db.put(new_doc); } catch (err) { log.info(err); } }; reader.readAsText(file); } }
Reply
Anonymous
Information Epoch 1732498814
Think parallel.
Home
Notebook
Contact us