Data Filling

Introduction #

Data filling is SmartChart’s core mechanism for writing frontend user input to databases. Through ds_save(dataset_index, dataset), user form data, button-triggered data, and extracted cell data can be written to databases. After configuring the fill dataset, all write logic is controlled in frontend JS.

Use Cases #

  • Collect user input data
  • Write processed data back to any system
  • Build survey/questionnaire systems
  • Analytics tracking

Configuration #

Create a lazy-load dataset with content:

dataset = {
    "table": "table_name"
}

With field specification and pre/post SQL:

dataset = {
    "table": "table_name(field1, field2)",
    "preSql": ["truncate table abc"],
    "postSql": ["insert into xxx select ...."]
}

Data Formats #

  • Single row:
dataset = ['a', 'b']
// or
dataset = {'id': 123, 'name': 'john'}
// Auto-record username:
dataset = ['$username', 'b']
  • Multiple rows:
dataset = [[], ['a1','b1'], ['a2', 'b2']]
dataset = [['__A','B'], ['a1','b1'], ['a2','b2']]
// Headers starting with __ use header field names

Template Custom Write #

HTML components:

<h1 class="smtdrag" id="id_title">Data Entry</h1>
<div class="smtdrag" id="id_visitor">
    <label>User</label><input id="id_visitor">
</div>
<div class="smtdrag" id="id_action">
    <label>Action</label><input id="id_action">
</div>
<div class="smtdrag" id="id_submit">
    <button id="idbtn01">Submit</button>
</div>

JS handler:

$('#idbtn01').click(function(){
    let visitor = $('#id_visitor').val();
    let action = $('#id_action').val();
    let dataset = [visitor, action];
    let res = ds_save(0, dataset);
})

ds_save Function #

ds_save(dataset_index, dataset, update=0)
// update: 0=insert, 1=update (by primary key), 2=delete
Data Format Example Description
Single row array ['a', 'b'] Write by config field order
Single row dict {'name': 'john', 'age': 30} Write by field name
Multi-row (no header) [[], ['a1','b1'], ['a2','b2']] Empty header + data rows
Multi-row (with header) [['__A','B'], ['a1','b1']] Headers with __ prefix use field names
With username ['$username', 'b'] $username auto-replaced

Key: Fill datasets must be lazy-load datasets with dataset={...} config in the editor.