1

I have an object array in the following format, with name, status and the Cells.Cells hold background color.

Result =
    {
    "Name" : "Check",
    "Status" : 0,
    "Cells" : [#A9A9F5,#8181F7,#8258FA,#8258FA,#8258FA] ,

    "Name" : "Test",
    "Status" : 1,
    "Cells" : [#8181F7,#8258FA,#8181F7,#8258FA,#8258FA] 
}

I am using knockout to bind them and display the result in the table. My desired output would be to get the following result in a table. The cell color would be the background color of the cell.

   Check|0|[Cell Color1]|[Cell Color2]|[Cell Color3]|[Cell Color4]|[Cell Color5]
    Test|1|[Cell Color1]|[Cell Color2]|[Cell Color3]|[Cell Color4]|[Cell Color5]

What I have so far is

            <table border="1">
            <tbody data-bind="foreach: Result">
                <tr>
                    <td data-bind="text: Name"></td>
                    <td data-bind="text: Status"></td>
                    <td data-bind="foreach: Cells">
                     <td data-bind="style: {'background-color':Cells}"></td></td>

                </tr>
            </tbody>

        </table>

I am getting the following result

Check|0|
Test|1|

But not the cells with the respective background color. How can I access the objects inside 'Cell' and have them generate its own cell with the respective background color? Thanks

1
  • 1
    <td> inside <td> thats not a valid case , you try using $data in foreach in place of cells Commented Oct 20, 2015 at 12:55

1 Answer 1

2

You can access the current iteration of foreach binding with $data variable. also you might want to replace

<td data-bind="foreach: Cells">

with

<!-- ko foreach: Cells -->
 <td data-bind="style:{'background-color':$data}></td>
<!-- /ko -->

because as @super cool noted in comments - td inside td is not valid

Sign up to request clarification or add additional context in comments.

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.