最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dynamic html form generation in MEAN stack - Stack Overflow

programmeradmin3浏览0评论

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 acplish 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 () So will it help because there may be chance of huge data on excel files.

Any help would be really appreciated.

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 acplish 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./GothAck/forms-mongoose) So will it help because there may be chance of huge data on excel files.

Any help would be really appreciated.

Share Improve this question edited Mar 4, 2016 at 17:00 metame 2,6401 gold badge20 silver badges23 bronze badges asked Mar 4, 2016 at 15:08 J.K.A.J.K.A. 7,40425 gold badges99 silver badges164 bronze badges 7
  • The above code looks fine for conversion, especially considering your output looks good. Without an attempt at the front end code, SO can't help you much. The github link you include is for Mongoose so unless you're using mongoose, it's not going to help you much. – metame Commented Mar 4, 2016 at 16:31
  • And just as a small note on MongoDB, you're not actually storing JSON in Mongo, but BSON. – metame Commented Mar 4, 2016 at 16:36
  • @metame: Thanks for replying. I tried moogose but its requires proper attributes in JSON format. In my case JSON is generating from excel file so how can I tackle that issue? – J.K.A. Commented Mar 4, 2016 at 16:36
  • @metame: Ohh so its BSON.. Actually I'm using mongojs nodes module for inserting data – J.K.A. Commented Mar 4, 2016 at 16:37
  • That sounds like another question that is a lot more clearer. I would remend posting that as a separate question or editing your question (and title) to ask that instead. The way your current question is stated, a proper answer would require someone to write a bunch of code for you. – metame Commented Mar 4, 2016 at 16:39
 |  Show 2 more ments

2 Answers 2

Reset to default 4 +25

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. and this came up: https://github./Nijikokun/generate-schema

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./jdorn/json-editor/

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

http://jsonschema/#/

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

发布评论

评论列表(0)

  1. 暂无评论