在Django中更新下拉按钮而无需提交按钮

huangapple go评论67阅读模式
英文:

Update dropdown button without submit button in Django

问题

现在的视图函数是这样的:

@user_login_required
def view_usecase_details(request, ucid):
    usecase_details = Usecase.objects.filter(usecase_id=ucid).all()
    usecase_details = usecase_details.prefetch_related("usecaseids")
    uckpi = Kpi.objects.all()
    context = {'usecase_details': usecase_details, "uckpi": uckpi}
    return render(request, 'UsecaseDetails.html', context)

模板部分是这样的:

{% if usecase_details is not none and usecase_details %}
<div class="card card-body shadow-sm mb-4 mb-lg-0">
    {% for result in usecase_details %}
    <div class="d-flex align-items-center">
        <svg class="mr-svg stc-color" xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-bookmark-fill" viewBox="0 0 16 16">
            <path d="M2 2v13.5a.5.5 0 0 0 .74.439L8 13.069l5.26 2.87A.5.5 0 0 0 14 15.5V2a2 2 0 0 0-2-2H4a2 2 0 0 0-2 2z"></path>
          </svg>
        <h2 class="header-card h5">{{result.usecase_id}} - {{result.usecase_name}}</h2>
    </div>

    <ul class="list-group list-group-flush">
        <li class="list-group-item d-flex align-items-center justify-content-between px-0 border-bottom">
            <div>
                <h3 class="h6 mb-1 mt-15">Usecase description:</h3>
                <p class="small pe-4">{{result.usecase_description}}</p>
            </div>
        </li>
    </ul>
    <ul class="list-group list-group-flush">
        <li class="list-group-item d-flex align-items-center justify-content-between px-0 border-bottom">
            <div class="btn-group">
                <button type="button" class="btn btn-secondary">KPI: {{result.kpi.kpi_name}}</button>
                <button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
                  <svg class="icon icon-xs" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd"></path></svg>
                </button>
                <ul class="dropdown-menu">
                    {% for kpi in uckpi %}
                  <li><a class="dropdown-item">{{kpi.kpi_name}}</a></li>
                  {% endfor %}
                </ul>
              </div>
        </li>
    </ul>
    {% endfor %}
</div>
{% endif %}
英文:

I'm using Django and I have dropdown button that shows the current selected value and the rest of items in dropdown list.

How can I make the user change the selection and reflect that change on the db without having to press update button.

My models are usecase, and kpi (fk of usecase)

my views.py:

@user_login_required
def view_usecase_details(request,ucid):
usecase_details = Usecase.objects.filter(usecase_id=ucid).all()
usecase_details = usecase_details.prefetch_related(&quot;usecaseids&quot;)
uckpi = Kpi.objects.all()
context = {&#39;usecase_details&#39;: usecase_details, &quot;uckpi&quot;: uckpi}
return render(request, &#39;UsecaseDetails.html&#39;, context)

my template:

{% if usecase_details is not none and usecase_details %}
&lt;div class=&quot;card card-body shadow-sm mb-4 mb-lg-0&quot;&gt;
{% for result in usecase_details %}
&lt;div class=&quot;d-flex align-items-center&quot;&gt;
&lt;svg class=&quot;mr-svg stc-color&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; fill=&quot;currentColor&quot; class=&quot;bi bi-bookmark-fill&quot; viewBox=&quot;0 0 16 16&quot;&gt;
&lt;path d=&quot;M2 2v13.5a.5.5 0 0 0 .74.439L8 13.069l5.26 2.87A.5.5 0 0 0 14 15.5V2a2 2 0 0 0-2-2H4a2 2 0 0 0-2 2z&quot;&gt;&lt;/path&gt;
&lt;/svg&gt;
&lt;h2 class=&quot;header-card h5&quot;&gt;{{result.usecase_id}} - {{result.usecase_name}}&lt;/h2&gt;
&lt;/div&gt;
&lt;ul class=&quot;list-group list-group-flush&quot;&gt;
&lt;li class=&quot;list-group-item d-flex align-items-center justify-content-between px-0 border-bottom&quot;&gt;
&lt;div&gt;
&lt;h3 class=&quot;h6 mb-1 mt-15&quot;&gt;Usecase description:&lt;/h3&gt;
&lt;p class=&quot;small pe-4&quot;&gt;{{result.usecase_description}}&lt;/p&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ul class=&quot;list-group list-group-flush&quot;&gt;
&lt;li class=&quot;list-group-item d-flex align-items-center justify-content-between px-0 border-bottom&quot;&gt;
&lt;div class=&quot;btn-group&quot;&gt;
&lt;button type=&quot;button&quot; class=&quot;btn btn-secondary&quot;&gt;KPI: {{result.kpi.kpi_name}}&lt;/button&gt;
&lt;button type=&quot;button&quot; class=&quot;btn btn-secondary dropdown-toggle dropdown-toggle-split&quot; data-bs-toggle=&quot;dropdown&quot; aria-expanded=&quot;false&quot;&gt;
&lt;svg class=&quot;icon icon-xs&quot; fill=&quot;currentColor&quot; viewBox=&quot;0 0 20 20&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z&quot; clip-rule=&quot;evenodd&quot;&gt;&lt;/path&gt;&lt;/svg&gt;
&lt;/button&gt;
&lt;ul class=&quot;dropdown-menu&quot;&gt;
{% for kpi in uckpi %}
&lt;li&gt;&lt;a class=&quot;dropdown-item&quot;&gt;{{kpi.kpi_name}}&lt;/a&gt;&lt;/li&gt;
{% endfor %}
&lt;/ul&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
{% endfor %}
&lt;/div&gt;
{% endif %} 

答案1

得分: 1

你可以在选择项上点击时触发一个事件,该事件将向后端发送一个带有所需数据的请求,然后在那里更新数据库。

英文:

You can make an event on click the item in the select, this event will send a request to the backend with the data you want and there you will update the db.

huangapple
  • 本文由 发表于 2023年3月12日 16:32:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定