我正在試圖製作一個包含模型對象的django-tables2表的Javascript函數,該模型對象將在單擊時顯示相關信息。下面是相關代碼:爲在Django Tables2中格式化的模型對象製作一個Javascript單擊函數,用於顯示相關的div
models.py
class Student(models.Model):
student_id = models.CharField(max_length=128, unique=True, null=True, blank=True)
first_name = models.CharField(max_length=128)
last_name = models.CharField(max_length=128)
ssn = USSocialSecurityNumberField(null=False)
gender = models.CharField(max_length=128, choices=GENDER_CHOICES)
country = CountryField(default='US', blank=True)
primary_phone = models.CharField(max_length=128)
email = models.EmailField(max_length=254, validators=[validate_email])
background = models.CharField(max_length=128, choices=BACKGROUND_CHOICES)
location = models.CharField(max_length=128, choices=LOCATION_CHOICES, default='south_plainfield')
created_at = models.DateTimeField(null=True, auto_now_add=True)
tables.py
class StudentListTable(tables.Table):
name = tables.TemplateColumn('''{{ record.first_name }} {{ record.last_name }}''', verbose_name=u'Name')
manage = tables.TemplateColumn('''<a href="/students/update_student/{{ record.id }}">Update</a>/<a href="/students/delete_student/{{ record.id }}" onclick="return confirm('Are you sure you want to delete this?')">Delete</a>''')
assign = tables.TemplateColumn('''<a href="/students/id={{ record.id }}/add_studentcourse">Courses</a>/<a href="/students/id={{ record.student_id }}/add_studentemployment">Employment</a>/<a href="/students/id={{ record.id }}/add_studentcounselor">Counselor</a>/<a href="/students/id={{ record.id }}/show_student_lists">Show</a>''')
class Meta:
model = Student
fields = ('student_id', 'name', 'gender', 'ssn', 'email', 'primary_phone', 'created_at', 'manage', 'assign')
row_attrs = {
'id': lambda record: record.pk
}
attrs = {'class': 'table table-hover', 'id': 'student_list'}
views.py
def all_My_Student(request):
student_list = Student.objects.order_by('-created_at')
table = StudentListTable(student_list)
RequestConfig(request).configure(table)
return render(request, 'students/all_my_student.html', {'table': table, 'student_list': student_list})
all_my_student.html
{% block main_content %}
<div class="box box-primary" id="student_list_table">
{% if table %}
{% render_table table %}
{% endif %}
</div>
{% for student in student_list %}
<div class="detailed" id="{{ student.id }}">
{{ student.first_name }} {{ student.last_name }}
</div>
{% endfor %}
{% endblock %}
{% block custom_javascript %}
<script>
$(document).ready(function()
{
$('.detailed').hide();
}
);
</script>
{% endblock %}
我想要做的就是製作一個基本如此的Javascript函數:正如您所看到的,「詳細」類div從一開始就是隱藏的。當你點擊模型對象表中的一行時,將會顯示id爲與行匹配的「詳細」class div(或者基本上與表循環和第二個循環中的相同模型對象相對應),並且其餘的將仍然隱藏。我希望這很清楚。
This Works。但是,我也想這樣做,使其他行上的任何點擊都可以使以前的div替換爲新的div,並且能夠再次單擊一行以使div消失。 –
你試圖做到什麼?你知道[$(...).toggle()](http://api.jquery.com/toggle/) – Jieter
是的,我記得在我發表評論後立即切換。雖然我仍然想知道如何隱藏之前顯示的div,當我點擊另一行時。 –