How to use multiSet()?
How to use multiSet()?
Link to test case: https://editor.datatables.net/examples/simple/simple.html
Error messages shown:
Uncaught TypeError: Cannot read property 'length' of undefined
at Editor.<computed>.multiGet (dataTables.editor.min.js:21)
at String.<anonymous> (dataTables.editor.min.js:21)
at Function.each (jquery-3.5.1.js:381)
at Editor.<computed>.<computed> [as multiGet] (dataTables.editor.min.js:21)
at <anonymous>:1:8
Description of problem:
I'd like to add rows programmatically in JavaScript using multiSet()
. However, after following the simple example, when I add in the browser console editor.multiGet();
as a starting point, I get this error. I mention that the issue is even reproducible on the example page. I have the same issue locally with my downloaded trial version of Editor.
This question has an accepted answers - jump to answer
Answers
How are you calling the function? Is the form already in edit / create mode?
When I load up that example and select a row (id
row_5
) I can do:to set the value.
I'm wondering if you haven't yet called the
create()
method (oredit()
if editing)?Allan
Thanks Allan for your quick reply. If I directly execute after loading the example page the
multiset
function you mentioned, I get:To avoid this error, I have to click on the New button (and close the Create new entry modal window) before executing the function.
I recorded the situation: https://recordit.co/m7cr94ShLG
You're most probably right. I didn't see the info about that in the doc. Could you link the section about how to do it programmatically?
I noticed that if I use
editor.create(false);
beforeeditor.multiSet({first_name: {row_5: 'Allan'}});
, the error does not appear anymore but the table is still not updated.How to use
multiSet()
in the console of the simple example?Are you using
submit()
to submit the data to the server?You probably don't want to define the row ID when creating a new record as that is something that should be done automatically by the server.
Try these commands to add a record:
Kevin
Oh yes, it works! Thank you Kevin.
By any chance would you be able to give an example with
editor.edit(false);
? I can't figure out how to edit for example the row with Extn. 1042The first question is how are you wanting to find that row programmatically?
The second question is are you wanting to edit just the single row or are you still wanting the ability to select multiple rows and use
multiSet()
?I'll assume you still want the ability to programmatically edit multiple rows. The
e-edit()
docs state that the first parameter, which is required, are the item(s) you want to edit. You have these options for passing one or more rows:For this example we will use the row indexes for editing and for choosing the rows. See the
row-selector
to see the options you have to programmatically select the rows.The row ID of the row with
Extn. 1042
is row_41 and the index is 41. You can look at the Ajax Load tab of the example to find this. Use the following to get two rows:Use
editor.edit(rows, false);
to put then in edit mode.Optionally you can use
editor.multiGet();
to see the rows for editing.Use the following to change the first name of the user for Extn 1042:
Optionally you can use
editor.multiGet();
to see the updated data.Then use
editor.submit();
to submit to the server. Use the browser's network inspector to see that both rows are sent to the server for updating and the server responds with the updated two rows.Kevin
Thank you Kevin for your very clear and quality answer.