2

In jquery/javascript the array has following structure. And I am able to fetch the data using, data[cartIndex].rows[i].

//cartIndex is anything from 0 to 10;
//row is like [name, quantity, balance, remarks]

for (var i=0; i < data[cartIndex].length; i++) {
            var row = data[cartIndex].rows[i];
            alert(row.name, row.quantity, row.balance);
}

I used both a list and an array to populate data in C# and serialized it.


UPDATED CODE:

        var arrList = new List<object>();

        string x = string.Empty;
        int i = table.Rows.Count;
        foreach (DataRow row in table.Rows)
        {
            string name = row[0].ToString();
            string quantity = row[1].ToString();
            string balance = row[2].ToString();
            string remove = "X";

            arrList.Add(new Wrapper { rows = new [] { name, quantity, balance, remove }});
        }

        var maList = new List<object>();
        maList.Add(arrList);            
        return (new JavaScriptSerializer()).Serialize(maList);

But somehow it's not in the correct column row format in javascript.

data = [
         [[name, quantity, balance, remarks], 
            [name, quantity, balance, remarks], 
            [name, quantity, balance, remarks]
         ],
         [[name, quantity, balance, remarks], 
            [name, quantity, balance, remarks], 
            [name, quantity, balance, remarks]
         ]         
       ]

With this, I am not able to access the array rows like this:

var row = data[cartIndex].rows[i];
//row.name gives name, row.quantity gives quantity and so on

This structure should also allow to push a row into the array.

data[cartIndex].length++;
data[cartIndex].rows.push({name: name, 
quantity: quantity, balance: blance});

How can I structure the c# array to be accessed via row indices?

1 Answer 1

2

You can access the data in the second dimension like this:

var name= data[cartIndex][0];
var quantiry = data[cartIndex][1];
// And so on...

The property data[cartIndex].rows will not work because the object you are adding to the list from the server side does not have a property rows

Alternatively, you can change your server code as follows:

// Add a new wrapper class
public class Wrapper{
    public string[] rows{ get;set;}
}

And change the line where you add the rows to the list as follows:

alist.Add(new Wrapper { rows = new [] {name, quantity, balance, remove}});
Sign up to request clarification or add additional context in comments.

8 Comments

Because of this alist.Add(new [] {name, quantity, balance, remove});
To get the rows property working, it should be something like alist.Add(new SomeWrapperClass { rows = new [] {name, quantity, balance, remove}});
Can you edit your answer to include the c# code? Is it a must to use a Wrapper class, isn't there a native way to formulate an array like this with rows in C#? Because there are functions which uses rows property alot in the js file data[cartIndex].rows.push(
Answer updated, Yes, you can do this in a one line code using Linq and Anonymous Types instead of the foreach loop.
I tried your code, but the array looks like this now: var data = [[{"rows":["Jorge","25","20","X"]},{"rows":["Jacques","50","10","X"]},{"rows":["Bell","10","10","X"]}]]; it doesn't seem to have a row index. I prefer not to use linq at this point, until this thing gets solved with the forloop.
|

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.