Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6582443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:16:51+00:00 2026-05-25T16:16:51+00:00

I have the following code (also in a jsfiddle ): <table> <thead><tr><th>Equation</th><th>Equation</th></tr></thead> <tbody data-bind=template:

  • 0

I have the following code (also in a jsfiddle):

<table>
    <thead><tr><th>Equation</th><th>Equation</th></tr></thead>
<tbody data-bind="template: {name: 'equationTemplate', foreach: equations}"></tbody>
</table>

<script language="javascript" type="text/javascript">

</script>

<script type="text/x-jquery-tmpl" id='equationTemplate'>
    <!-- In here I want to be able to break it up into two 
    columns of two rather than one column of four-->
    <tr>
        <td>${first}+${second}=<input data-bind="value: answer"/></td>
    </tr>
</script>

With this JS:

$(document).ready(function () {

    var viewModel = {
        equations: ko.observableArray([
            { first: 1, second: 2, answer: 3 },
            { first: 4, second: 4, answer: 8 },
            { first: 10, second: 10, answer: 20 }, 
            { first: 5, second: 5, answer: 10}])
    };

    ko.applyBindings(viewModel);
});

How would I modify the template to output this in a table of two rows and two columns? Equations 10+10=20 and 5+5=10 should appear in the second column.

Any help greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:16:52+00:00Added an answer on May 25, 2026 at 4:16 pm

    One option is to switch to {{each}} instead of the foreach option of the template binding, so that you have access to the index. The disadvantage to using {{each}} is that your template will be re-rendered completely if any equations are added/removed dynamically.

    It would look like:

    <table>
        <thead><tr><th>Equation</th><th>Equation</th></tr></thead>
    <tbody data-bind="template: { name: 'equationTemplate', foreach"></tbody>
    </table>
    
    <script type="text/x-jquery-tmpl" id='equationTemplate'>
        {{each equations}}
            {{if $index % 2 == 0}}<tr>{{/if}}
                <td>${first}+${second}=<input data-bind="value: answer"/></td>
            {{if $index % 2 == 1}}</tr>{{/if}}   
        {{/each}}
    </script>
    

    Sample here: http://jsfiddle.net/rniemeyer/QESBn/1/

    Another alternative would be to build a dependentObservable that represents your data in a structure that maps to rows/columns.

    Here is a sample: http://jsfiddle.net/rniemeyer/QESBn/2/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following javascript code found also in this fiddle: http://jsfiddle.net/periklis/k4u4c/ <button id
I have downloaded sample code from Apple Center.I also have gone through following question:
I have following code class User attr_accessor :name end u = User.new u.name =
I have following code: private void ProcessQueue() { foreach (MessageQueueItem item in GetNextQueuedItem()) PerformAction(item);
I have following code in a html form <select name=category class=input onchange=ShowTB(this,'suggest');> <option value=0
Hey all. I have the following code: http://jsfiddle.net/g7Cgg/ As you can see, there are
I have the following code which pulls json data from an ASP.NET page and
I have the following working code: http://jsfiddle.net/NtHwN/4/ It will deduct the number that is
I have the following code: I also would like to add a image to
I have following code in my application: // to set tip - photo in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.