Import CSV with parent ID

Import CSV with parent ID

nitinknitink Posts: 17Questions: 6Answers: 1

I have Parent - Child tables - Quiz and Question. With Alan's help they are working perfectly.

Now I am trying to import Questions using this example. The import works fine except I don't know how to send the id of the parent table [Quiz].

I have added the following code that adds a new field to the Question editor in the initSubmit event. That seems to work but it is adding it to each record. Is there a better way of sending in the quizId to the server?

    questionSimpleEditor.on('initSubmit', function (e, action) {
        return new Promise(function (resolve) {
            var quizId22 = quizTable.row({ selected: true }).data().Id;
            questionSimpleEditor.add( {
                name: "quizId",
                type: "hidden",
            }, null);

            questionSimpleEditor.val('quizId', quizId22);
            questionSimpleEditor.submit();
            resolve("");
        });
    });

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    I'd say that is close, but small modification. When you first create questionSimpleEditor add your quizId field there. Then set its value in the initSubmit as you are doing, but don't call submit() (already in the submit phase, so no need to).

    Allan

  • nitinknitink Posts: 17Questions: 6Answers: 1

    Thank you allan. The reason I did not add quizId to the questionSimpleEditor is because after I upload the file, that field still shows up on the form before submission even if I mark it as hidden. Is there a way to hide that field? If so, it will be great.

    Also, do you now I can disable the New & Import CSV buttons in the child table till user selects a row in the parent table?

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    edited September 2019 Answer ✓

    I never considered the case of want to upload a file with a hidden field to be honest. I don't actually quite get that - could you explain that a little further for me please?

    Also you can use button().enable() and button().disable() to enable / disable buttons. Use select to know when a row has been selected.

    Allan

  • nitinknitink Posts: 17Questions: 6Answers: 1

    Thanks for the New idea. Would you suggest I disable them when the child table is first loaded and enable them when a parent row is selected?


    As to why I need to send a value via hidden field - here is the scenario

    I have a Parent table - Quiz. Each Quiz has multiple Questions for which I have a child table.

    I am using the Import CSV to import Questions for a given Quiz. On server side, I need id of the selected Quiz to save the Questions in the my database.

    Please note that I am not using DataTables server side code as I have a custom API which drives the website.

    Hope that clarifies.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Would you suggest I disable them when the child table is first loaded and enable them when a parent row is selected?

    Yes, that sounds about right.

    Thanks for the clarification - I thought that it was the file upload that you wantted hidden, rather than specific fields!

    Allan

This discussion has been closed.