Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
614 views
in Technique[技术] by (71.8m points)

javascript - pass value to bootstrap modal form with django

When I use bootstrap modal for my form its only show first value.

here my template.html

{% for company in companys %}
<tr>
    <td>{{ company.name }}</td>
    <td>{{ company.desc }}</td>
    <td align="center">
        <button type="button" class="btn btn-warning margin-bottom" data-toggle="modal" data-target="#modal-default2">
            delete
        </button>

        <div class="modal fade" id="modal-default2">
            <div class="modal-dialog">

                <form method="post" action="{% url 'system:company_delete' pk=company.pk %}">
                {% csrf_token %}

                <div class="modal-content">    
                    <div class="modal-body">
                        <input type="text" name="name" maxlength="100" required="" id="id_name" value="{{ company.pk }}">
                        <input type="submit" class="btn btn-primary" value="Delete">
                    </div>
                </div>

                </form>
            </div>
        </div>
    </td>
</tr>

{% endfor %}

its loop all the data, when click delete confirm form will popup. but its return same value.

but if without modal-bootstrap its work fine.

example: template.html

{% for company in companys %}
<tr>
    <td>{{ company.name }}</td>
    <td>{{ company.desc }}</td>
    <td align="center">
        <form method="post" action="{% url 'system:company_delete' pk=company.pk %}">
            {% csrf_token %}
            <input type="text" name="name" maxlength="100" required="" id="id_name" value="{{ company.pk }}">
            <input type="submit" class="btn btn-primary" value="Delete">
        </form>
    </td>
</tr>

{% endfor %}

it's work fine.

what I should do to make it work?...

update

views.py

# Company Crud
class CompanyListView(ListView):
    context_object_name = 'companys'
    model = models.Company

class CompanyCreateView(CreateView):
    fields = ('name', 'desc')
    model = models.Company

class CompanyUpdateView(UpdateView):
    fields = ('name', 'desc')
    model = models.Company

class CompanyDeleteView(DeleteView):
    model = models.Company
    success_url = reverse_lazy("system:company_list")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your ajax modal will always return the same value inside modal because:
- Modal has this data-target="#modal-default2" as the target, however, your loop contains the modal body, with the id id="modal-default2", which will render modal as much as your loop goes.

So what you can do is to define a unique ID for each modal with the ID of each company modal-default{{company.id}}:

{% for company in companys %}
    ''' rest of codes '''
    <button type="button" class="btn btn-warning margin-bottom" data-toggle="modal" data-target="#modal-default{{company.id}}">
        delete
    </button>
    ''' rest of codes '''

    <div class="modal fade" id="modal-default{{company.id}}">
        <div class="modal-dialog">
        </div>
    </div>
    ''' rest of codes '''
{% endfor %}

But this method is not effective if you have a lot of data, it will render lots of html codes.

Another option

With AJAX and one modal.

Your html would be:

{% for company in companys %}
    <td>{{ company.name }}</td>
    <td>{{ company.desc }}</td>

    <button data-id="{{company.id}}" type="button" class="btn btn-warning margin-bottom delete-company" >
        delete
    </button> <!-- be aware of class 'delete-company' -->
{% endfor %}
{% csrf_token %}


<div class="modal fade" id="modal-default">
    <div class="modal-dialog">
        {% if company %} <!-- this company instance will come from AJAX -->
            <form method="post" action="{% url 'system:company_delete' pk=company.pk %}">
            {% csrf_token %}

            <div class="modal-content">    
                <div class="modal-body">
                    <input type="text" name="name" maxlength="100" required="" id="id_name" value="{{ company.pk }}">
                    <input type="submit" class="btn btn-primary" value="Delete">
                </div>
            </div>
            </form>
        {% endif %}
    </div>
</div>

AJAX

$(document).on('click','.delete-company',function(){
    var id = $(this).data('id');

    $.ajax({
        url:'',
        type:'POST',
        data:{
            'id':id,
            'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val(),
        },
        success:function(data){
            $('#modal-default .modal-dialog').html($('#modal-default .modal-dialog',data));
            $('#modal-default').modal('show');
        },
        error:function(){
            console.log('error')
        },
    });
});

And your views would be:

change your url from CompanyListView.as_view() to companyListView

def companyListView(request):
    context = {}
    companys = models.Company.objects.all()
    if request.method == 'POST' and request.is_ajax():
        ID = request.POST.get('id')
        company = companys.get(id=ID) # So we send the company instance
        context['company'] = company
    context['companys'] = companys
    return render(request,'template.html',context)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...