J.K.A.
J.K.A.

Reputation: 7404

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:

Any help would be really appreciated.

Upvotes: 11

Views: 1455

Answers (2)

Trevor
Trevor

Reputation: 13457

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/

Upvotes: 4

Jason Livesay
Jason Livesay

Reputation: 6377

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

Upvotes: 4

Related Questions