【问题标题】:convert table cells to form input text with django使用 django 将表格单元格转换为输入文本
【发布时间】:2014-10-28 16:36:27
【问题描述】:

我有一个用 django-tables2 渲染的 html 表,我需要从三个第一列中获取值并将其作为参数传递给另一个模板。

{% block table %}
 <table id="myTable" class="table table-striped table-hovered table-bordered table-condensed"{% if table.attrs %} {{ table.attrs.as_html }}{% endif %}>
    {% block table.thead %}
    <thead>
        <tr>
        {% for column in table.columns %}
            {% if column.orderable %}
            <th {{ column.attrs.th.as_html }}><a href="{% querystring table.prefixed_order_by_field=column.order_by_alias.next %}">{{ column.header }}</a></th>
            {% else %}
            <th {{ column.attrs.th.as_html }}>{{ column.header }}</th>
            {% endif %}
        {% endfor %}
        </tr>
    </thead>
    {% endblock table.thead %}
    {% block table.tbody %}
    <tbody>
        {% for row in table.page.object_list|default:table.rows %} {# support pagination #}
        {% block table.tbody.row %}
        <tr class="{% cycle 'odd' 'even' %}">
            {% for column, cell in row.items %}
                <td {{ column.attrs.td.as_html }}>{{ cell }}</td>
            {% endfor %}
        </tr>
        {% endblock table.tbody.row %}

        {% endfor %}
    </tbody>
    {% endblock table.tbody %}
    {% block table.tfoot %}
    <tfoot></tfoot>
    {% endblock table.tfoot %}
 </table>
 {% if table.page %}
 {% block pagination %}
   {% bootstrap_pagination table.page %}
 {% endblock pagination %}
{% endif %}
{% endblock table %}

我需要从前三列中获取值并将其作为参数传递。 在第四列中,我插入了一个带有 javascript 函数的下拉按钮,我可以使用 JS 函数插入输入文本,但我不知道如何将单元格的值传递给输入单元格。 如果改变这一行

<td {{ column.attrs.td.as_html }}>{{ cell }}</td>

喜欢这个

<td {{ column.attrs.td.as_html }}><input type="text" value="{{ cell }}"</td>

有效,但所有单元格都转换为输入文本,我只需要转换前三列

【问题讨论】:

    标签: javascript django django-forms django-tables2


    【解决方案1】:

    你可以这样试试:

    {% for row in table.page.object_list|default:table.rows %}
            {% block table.tbody.row %}
            <tr class="{% cycle 'odd' 'even' %}">
                {% for column, cell in row.items %}
                    {% if forloop.counter < 4 %}
                       <td {{ column.attrs.td.as_html }}><input type="text" value="{{ cell }}"</td>
    
                    {% else %}
    
                       <td {{ column.attrs.td.as_html }}>{{ cell }}</td>
    
                    {% endif %}
    
                {% endfor %}
            </tr>
            {% endblock table.tbody.row %}
    
            {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      • 2019-12-16
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 2018-10-30
      • 1970-01-01
      相关资源
      最近更新 更多