How to update multiple checked records?
How to update multiple checked records?
uTrx
Posts: 24Questions: 6Answers: 0
in DataTables
Want to update multiple checked rows: I extend datatables custom button and have write a function which help me to edit a single row. But my problem is i need to update multiple rows. And i dont know how to pass multiple data with this?:
Answers
Use the
rows()
API to fetch multiple rows. If you need the row data as a pure Javascript array usetoArray()
.Kevin
Thanks for replay. And i replace row() with rows() and this give me multiple record i select ,its okay, but now my assign button dont work. Also data cant pass on lavravel controller
also i get this error: list:1026 Uncaught TypeError: Cannot set properties of undefined (setting 'length')
What doesn't work? Do you get errors in the browser's console?
Did you try using
toArray()
to convert the API result fromrows().data()
to a Javascript array?If you need help debugging please post a link to your page or a test case replicating the issues.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
i tryed in this way ** var rowdata = tableToQuery.row(selectedRow).toArray();**
and what i get on console : [Array(1)]0: [0]0: 0length: 1[[Prototype]]: Array(0)at: ƒ at()concat: ƒ concat()constructor: ƒ Array()copyWithin: ƒ copyWithin()entries: ƒ entries()every: ƒ every()fill: ƒ fill()filter: ƒ filter()find: ƒ find()findIndex: ƒ findIndex()flat: ƒ flat()flatMap: ƒ flatMap()forEach: ƒ forEach()includes: ƒ includes()indexOf: ƒ indexOf()join: ƒ join()keys: ƒ keys()lastIndexOf: ƒ lastIndexOf()length: 0map: ƒ map()pop: ƒ pop()push: ƒ push()reduce: ƒ reduce()reduceRight: ƒ reduceRight()reverse: ƒ reverse()shift: ƒ shift()slice: ƒ slice()some: ƒ some()sort: ƒ sort()splice: ƒ splice()toLocaleString: ƒ toLocaleString()toString: ƒ toString()unshift: ƒ unshift()values: ƒ values()Symbol(Symbol.iterator): ƒ values()Symbol(Symbol.unscopables): {copyWithin: true, entries: true, fill: true, find: true, findIndex: true, …}[[Prototype]]: Objectlength: 1[[Prototype]]: Array(0)
also this error : Uncaught TypeError: Cannot set properties of undefined (setting 'length')
Here is the code supplied by @uTrx
Please use Markdown formatting as noted in the Leave a Comment box:
Looks like the error is here:
If
rowData
is na array then you will need to use a Javascript loop to loop through the array.If you use
var rowdata = tableToQuery.row(selectedRow).toArray();
therow()
API will return only one row. You don't needtoArray()
. My suggestion above is to do something like this for multiple rows:It uses
rows().data()
to return an array of rows. This actually returns an API instance. So if you need pure Javascript then chaintoArray()
.Kevin
You are right
var rowdata = tableToQuery.rows(selectedRow).data().toArray();
now i get multiple value on my console.log
but when i hit submit on modal i get this error
array:4 [
"_token" => "nQ2CSvZTLMXqUbZwTmwatRbV8xBdwb6buq5bWn3N"
"assign" => "8"
"id" => "undefined"
"button_action_assign" => "assign"
]
as i use console.log(form_data) or in laravel dd($request->all).
So i can get multiple rows but i cant pass this rows on laravel controller
url:"{{ route('list.assignpostdata') }}",
method:"POST",
data:form_data,
console.log(form_data)
var rowdata = tableToQuery.rows(selectedRow).data().toArray();
You are right this fix the problem
But now when i hit submit on modal i get "id" => "undefined"
url:"{{ route('list.assignpostdata') }}",
method:"POST",
data:form_data,
console.log(form_data) //if i do console log here and hit submit
You are right this fix the problem
But now when i hit submit on modal i get "id" => "undefined"
Seems like a console.log statement on line 4 if your code snippet will cause a syntax error. Sounds like you are trying to use one
id
from an array of one or moreid
s. Are you looping through all the rows?Please post a link to your page or a running test case showing the issue if you want help debugging the code. This way we can see what is happening and can offer suggestions.
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
This is all script i use. I dont now how to display all this code on live example
iam getting this error now on console: Uncaught TypeError: Cannot read properties of undefined (reading 'length')
This is the statement causing the error. You are now getting an array of one or more rows, not a single object. You will need to loop through the array to get the
id
s. See this SO thread for options.Kevin
It would be worth looking at Editor - as that supports multiple edits.
Colin
@kthorngren please can you do an example for my case, i dont understand how to use loop in this case
The question is what are you trying to do? Looks like you are trying to open a form but do mass edits. Without knowing what your solution requirements are it would be difficult to provide the correct solution. However this is one way to loop through the array:
This may or may not be what you want. As Colin said the Editor will support this for you.
Kevin
@kthorngren this helps me. But why can i edit just last record. For example if i select row with id 1 and row with id 2 if i hit submit i can edit just row with id 2
Help with development of your application is beyond the scope of this forum. If you have specific Datatables questions we are glad to help. A forum like Stack Overflow is more appropriate for how to build your Javascript application and handle your forms.
Kevin