jsRender create an template for html table , given a certain number of rows and cols

Axel

I need to do something like this, but using templates.

Do you know how to use counters inside a template? I ask you because I need those counters to generate the IDs for the inputs I create.

HTML Code:

<div> 
    <span>N Cols:</span>
    <input id="txtNCols" type="text" /> 
    <span>N Rows:</span>
    <input id="txtNRows" type="text" />
    <input type="button" id="btnCrearMatriz" value="Create" />
</div>

JS Code

$('#btnCrearMatriz').click(function () {
    var rows = $('#txtNRows').val(); //here's your number of rows and columns
    var cols = $('#txtNCols').val();
    var table = $('<table><tbody>');
    for (var r = 0; r < rows; r++) {
        var tr = $('<tr>');
        for (var c = 0; c < cols; c++){
             var td=$('<td>');
             var textbox="<input type='text' id='txt_r" +(r+1) + "c"+ (c+1) + "'/>";
             td.append(textbox);
             td.appendTo(tr);
        }      
        tr.appendTo(table);
    }

    table.appendTo('body');
});

Thanks in advance.

EDIT:

I've just found this . Well, it seems that getting what I need, will be much more difficult than I thought.

Anyway, I tried something like this , but apparently it doesn't support multiple levels of nesting :(

<script id="table_template" type="text/x-jsrender">
   <table>
        <tbody>
           {{range start=1 end=rows}}
                  <tr>
                   {{range start=1 end=cols}}
                        <td>
                            <input type="text"/>
                        </td>
                   {{/range}}
                  </tr>         
            {{/range}}
        </tbody>
   </table>
</script>
BorisMoore

The reason that it didn't work for you is that JsRender templates are of course data-driven. When you write {{range start=1 end=cols}} it means you are setting the end to the value of the cols as a property of the current data item. But the current data item is not your original data object, since you are in a nested template.

The way range works is either you pass it an array in which case it iterates over the array for the range you specify, or you don't pass it an array in which case it creates its own array - an array of integers from the start to the end you specify.

{{range myArray start=1 end=3}}

So your {{range}} tags are iterating over the generated arrays, and the data item within the block is the current integer from that array.

If you want to get a data-item from higher up you can either use ~root.cols (for example - to get to the cols property of the root data you passed in) or you can use ~foo=someExpression to create a helper variable that can be accessed from nested template blocks at any level.

So based on that, here are a couple of ways to address your scenario above:

Using {{range}}

<script id="myTmpl" type="text/x-jsrender">
  <table>
    <tbody>
      {{range start=1 end=rowCount ~colCount=colCount}}
        <tr>
          {{range start=1 end=~colCount ~rowNo=#index+1}}
            <td>
              <input type="text" id="{{:'r' + ~rowNo + 'c' + (#index + 1)}}" />
            </td>
          {{/range}}
        </tr>
      {{/range}}
    </tbody>
  </table>

rendered against data as follows:

$.templates("#myTmpl").render({rowCount: 10, colCount: 3)

Or Using {{for}}

  <table>
    <tbody>
      {{for rows ~cols=cols}}
        <tr>
          {{for ~cols ~rowNo=#index+1}}
            <td>
              <input type="text" id="{{:'r' + ~rowNo + 'c' + (#index + 1)}}" />
            </td>
          {{/for}}
        </tr>
      {{/for}}
    </tbody>
  </table>
</script>

rendered against data as follows:

$.templates("#myTmpl").render({rows:[1,2,3,4], cols:[1,2]})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jsRender table header rows are lost when template is added

From Dev

HTML Table Manipulation(multiple rows/cols in one <tr> or <td>)

From Dev

Mysql SELECT statement to return only rows where/if a given number of cols are not null

From Dev

Mysql SELECT statement to return only rows where/if a given number of cols are not null

From Dev

Get certain number of rows from a table

From Dev

Select certain number of rows from junction table

From Dev

Select certain number of rows from junction table

From Dev

Split html page into rows and cols

From Dev

Split html page into rows and cols

From Dev

How to enter number of rows and columns, and create a table

From Dev

Style certain rows from database table in html table

From Dev

Select rows which appear more than given number of times in table

From Dev

Select rows which appear more than given number of times in table

From Dev

create table in html as per the condition given for a table to mysql database table

From Dev

Selecting only rows with certain number from data table

From Dev

Hide table when number of child rows is below certain count

From Dev

How to count the number of rows pertaining to a certain year within a table

From Dev

PHP/MySQL stops Updating after table reaches certain number of rows

From Dev

create a foreach table in html email template php

From Dev

Given a table of data and number of occurrences, can I create the underlying dataset?

From Dev

Debug jsrender template

From Dev

Retrieve file information (name, size, rows and cols number) in Shiny R

From Dev

From a table of made up of rows of dates, how to count the number of rows where the date is in a certain date range

From Dev

Add rows and cols without resizing the table width and height

From Dev

Dartlang: Create table rows with different number of radio buttons

From Dev

Dartlang: Create table rows with different number of radio buttons

From Dev

What units of measurement are "rows" and "cols" in `@Html.TextAreaFor`?

From Dev

What units of measurement are "rows" and "cols" in `@Html.TextAreaFor`?

From Dev

The correct way to create given template

Related Related

  1. 1

    jsRender table header rows are lost when template is added

  2. 2

    HTML Table Manipulation(multiple rows/cols in one <tr> or <td>)

  3. 3

    Mysql SELECT statement to return only rows where/if a given number of cols are not null

  4. 4

    Mysql SELECT statement to return only rows where/if a given number of cols are not null

  5. 5

    Get certain number of rows from a table

  6. 6

    Select certain number of rows from junction table

  7. 7

    Select certain number of rows from junction table

  8. 8

    Split html page into rows and cols

  9. 9

    Split html page into rows and cols

  10. 10

    How to enter number of rows and columns, and create a table

  11. 11

    Style certain rows from database table in html table

  12. 12

    Select rows which appear more than given number of times in table

  13. 13

    Select rows which appear more than given number of times in table

  14. 14

    create table in html as per the condition given for a table to mysql database table

  15. 15

    Selecting only rows with certain number from data table

  16. 16

    Hide table when number of child rows is below certain count

  17. 17

    How to count the number of rows pertaining to a certain year within a table

  18. 18

    PHP/MySQL stops Updating after table reaches certain number of rows

  19. 19

    create a foreach table in html email template php

  20. 20

    Given a table of data and number of occurrences, can I create the underlying dataset?

  21. 21

    Debug jsrender template

  22. 22

    Retrieve file information (name, size, rows and cols number) in Shiny R

  23. 23

    From a table of made up of rows of dates, how to count the number of rows where the date is in a certain date range

  24. 24

    Add rows and cols without resizing the table width and height

  25. 25

    Dartlang: Create table rows with different number of radio buttons

  26. 26

    Dartlang: Create table rows with different number of radio buttons

  27. 27

    What units of measurement are "rows" and "cols" in `@Html.TextAreaFor`?

  28. 28

    What units of measurement are "rows" and "cols" in `@Html.TextAreaFor`?

  29. 29

    The correct way to create given template

HotTag

Archive