Search code examples
pythonhtmlweb2py

How to count rows from a html table web2py


actually i have a table in the html and i want to know how i can get like tabe.rows.count on python in web2py

here is my code:

<table id="TablaMateriales" name="TablaMateriales" class=" table table-responsive order-list">
    <thead>
        <tr class="table-light">
            <td>Item</td>
            <td>Descripcion</td>
            <td>Unidad</td>
            <td>Cantidad</td>
            <td>Proveedor</td>
        </tr>
    </thead>
    <tbody>
       <tr>
            <td>
                <input type="number" name="Item0" class="form-control" style="width:45px" value="1" disabled />
            </td>
<td>
                <input type="text" name="Unidad0" style="width:100px" class="form-control"/>
            </td>
            <td>
                <input type="number" min="0" id="Cantidad0" name="Cantidad0" style="width:75px" value="0" class="form-control" onFocus="this.select()" onsubmit="if(this == ''){$this.val('0');}"/>
            </td>
            <td>
                <input type="text" name="Proveedor0" style="width:250px" class="form-control"/>
            </td>
     </tr>
    </tbody>
</table>

and code behind:

def crearCotizacion():
    materialesT = request.vars.get('TablaMateriales')
    rowsCount = materialesT.rows.count
return dict(rowsCount = rowsCount)

Thank you!


Solution

  • It seems like you are trying to access a client side element ('TablaMateriales') on the server side. You can only do that if you explicitly pass the element back to the server.

    And then you would need to manipulate it using something like http://web2py.com/books/default/chapter/29/05/the-views#Server-side-DOM-and-parsing

    You could just count the number of rows using JavaScript from inside the view: JavaScript to get rows count of a HTML table