How to pre-populate edit form with only some fields?

How to pre-populate edit form with only some fields?

YOMYOM Posts: 53Questions: 22Answers: 1

Hey there,

Datatables has this handy example for creating duplicate rows.

For my use case, I need almost this exact logic except I need some of the fields to be programmatically cleared/emptied in the form so the user can fill them with different data.

I've tried the following solution:

const cleanDuplicateRow = (editor, dt) => {
  return editor
    .edit(dt.rows({ selected: true }).nodes(), false)
    .val(cleanedFields); // cleanedFields is an object of key:value pairs for fields:values
};

const cleanedRow = cleanDuplicateRow(editor, dt);

editor
  .edit(cleanedRow, {
    title: "Duplicate record",
    buttons: "Create from existing",
  })
  .mode("create");

But it appears that the .edit().val() chain doesn't return the type of object that editor.edit() is looking for- and it doesn't appear to return an object that I can call .indexes() or .nodes() on either. I'm sure the solution is pretty simple but I'm not familiar enough with datatables API to figure it out.

Let me know how I can accomplish this

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin
    Answer ✓

    Hi,

    Try this:

    editor
      .edit(cleanedRow, {
        title: "Duplicate record",
        buttons: "Create from existing",
      })
      .mode("create")
      .val(cleanedFields);
    

    Explanation: You need to trigger the editing first, then modify the values. When the edit mode is activated, the fields will be populated with the values from the selected row(s). So any modifications need to happen after that.

    Allan

Sign In or Register to comment.