Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...