1

I have a multi dimentional array called dataArray like below. it may have indexes 0,1,2,3 without a limit.

I am using codeigniter framework. dataArray is all data of a table in the database returned using a model.

0:
status: "1"
added_by: "1"
added_on: "2018-12-12 07:01:23"
approved: 0
c _id: "4"

I pass this array by onclick function when adding datatable rows as below. (in a foreach)

$('#table').DataTable().row.add([
    x, //counter 
    data[j]['action'], //data in foreach
    data[j]['status_1'],
    data[j]['description'],
    data[j]['timestamp'],
    '<input type="button" value="..." onclick="openModal(\'' + dataArray + '\')">' // this is passing array
]).draw(false);

openModal function is like below.

function openModal(data_array){
    console.log(data_array); 
}

above console.log returns only [object Object]. I want to get data and add a foreach here. But data is not showing. Please help on this.

6
  • Would be good to see where dataArray is getting generated. Commented Dec 12, 2018 at 8:14
  • A wild guess would be to pass dataArray.data but we need to see the where is that variable being filled. Commented Dec 12, 2018 at 8:22
  • @fixatd I am using codeigniter framework. dataArray is return by database using model. Commented Dec 12, 2018 at 8:26
  • @IslamElshobokshy I am using codeigniter framework. dataArray is return by database using model. Commented Dec 12, 2018 at 8:26
  • @tenten What I mean is how it gets generated in your javascript code rather than the server side code. Commented Dec 12, 2018 at 8:27

3 Answers 3

1

You would need to stringify the argument first

"<input type='button' value='foo' onclick='openModal(\"" + JSON.stringify(arr) + "\")'>";

and then parse on the calling function

function openModal(data_array){
    console.log(JSON.parse(data_array)); 
}
Sign up to request clarification or add additional context in comments.

Comments

1

I would use JSON.stringify/parse to pass the array data.

<div id="container"></div>
<script>
        var arr = [1,2,3];           

        function openModal(x) {
            x = JSON.parse(x);
            console.log(x);
        }

        document.querySelector("#container").innerHTML+="<input type='button' value='foo' onclick='openModal(\""+JSON.stringify(arr)+"\")'>";       
</script>

Comments

1

First, you should add object instead array to DataTable Row.

$('#table').DataTable().row.add({ x, //counter data[j]['action'], //data in foreach data[j]['status_1'], data[j]['description'], data[j]['timestamp'], '<input type="button" value="..." onclick="openModal(\'' + dataArray + '\')">' // this is passing array}).draw(false);

Second, Console.log(data_array) always show object, u can use Console.log(JSON.stringify(data_array)), or if u want show item one by one, u can use for or jQuery.each

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.