Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic html form generation in MEAN stack

I've just started learning on MEAN stack and need to generate dynamic forms on the fly.

The requirement is to import document (excel/csv/xml/xls etc..) and generate dynamic forms using it so a user can update their data and again export it into the respective file.

So to accomplish this I'm converting documents to JSON format and storing JSON data into the MongoDB database.

Ex: Consider this xlsx data:

ID  Name       dob        Gender
1   user1      7-Dec-87   m
2   user2      8-Dec-87   f
3   user3      9-Dec-87   f
3   user4      4-Dec-87   m

And I'm converting this using xlsx-to-json module to JSON format and storing it into Mongodb.

app.post('/myapp', function (req, res) {

    //console.log("===========" + req.file.path);

    converter({
        input: req.file.path,
        output: "output.json"
    }, function (err, result) {
        if (err) {
            console.error(err);
        } else {
            console.log(result);
            db.collection('test').insert(result, function (err, doc) {
                console.log(err);
                res.json(doc);
            });
        }
    });

});

Here I'm fetching above data from Mongodb & express.js

app.get('/myapp', function (req, res) {
    db.collection('test').find(function (err, docs) {
        console.log(docs);
        res.json(docs);
    });
});

app.get('/birthdaylist/:id', function (req, res) {
    var id = req.params.id;
    console.log(id);
    db.collection('test').findOne({_id: mongojs.ObjectId(id)}, function (err, doc) {
        console.log(JSON.stringify(doc));
        res.json(doc);
    });
});

and here's the JSON output:

[ 
  { dob: '7-Dec-87', ID: '1', Name: 'user1' },
  { dob: '8-Dec-87', ID: '2', Name: 'user2' },
  { dob: '9-Dec-87', ID: '3', Name: 'user3' },
  { dob: '4-Dec-87', ID: '4', Name: 'user4' } 
]

So, I've few queries:

  • Is this the correct approach I'm doing to generate dynamic form from xlsx/csv..etc ? If yes, then how can I generate dynamic form from above JSON.

  • While exploring on google I've found mongodb generates form automatically (https://github.com/GothAck/forms-mongoose) So will it help because there may be chance of huge data on excel files.

Any help would be really appreciated.

like image 345
J.K.A. Avatar asked Mar 04 '16 15:03

J.K.A.


2 Answers

Do you actually need to analyze an arbitrary spreadsheet and dynamically extract the schema, or do you know the schema ahead of time? If you know the schema, then the Mongoose form generating example is straightforward. But make sure that is actually a requirement because it is tough.

You are never going to be 100% because spreadsheets are created by users and users do weird things. But you can make something that works most of the time.

You need something that takes a JSON object and extracts the schema and puts that in a Mongoose schema format.

So you want to add an interesting module to Mongoose schema. I searched node-modules.com and this came up: https://github.com/Nijikokun/generate-schema

like image 52
Jason Livesay Avatar answered Oct 13 '22 02:10

Jason Livesay


Form generation is not a trivial task. You may want to consider using a library for this. Here are a few that might be useful to you:

http://schemaform.io/

https://github.com/jdorn/json-editor/

Also, if you need help generating JSON schema from JSON:

http://jsonschema.net/#/

and of course: http://json-schema.org/

like image 22
Trevor Avatar answered Oct 13 '22 00:10

Trevor