[英]Select an option from a dropdown list by onClick on data-table row
I have a data-table when user click on the table row it will get the {{ content.id }}
and pop up another child table. 当用户单击表行时,我有一个数据表,它将得到
{{ content.id }}
并弹出另一个子表。
<table id="project-content-datatable" class="display table table-hover table-responsive" style="width: 100%">
<thead>
<tr>
<th class="small text-muted text-uppercase"><strong>ID</strong></th>
<th class="small text-muted text-uppercase"><strong>Name</strong></th>
<th class="small text-muted text-uppercase"><strong>Description</strong></th>
</tr>
</thead>
<tbody>
{% for content in content_list %}
{% if content.search_type.name == searchtype.name %}
<tr class="text-primary">
<td class="text-left" style="cursor: pointer"
onclick='load_workorder({{ content.id }});'>
{{ content.id }}
</td>
<td class="text-left" style="cursor: pointer" onclick='load_workorder({{ content.id }});'>
{{ content.name }} </td>
<td class="text-left"> {{ content.description }} </td>
</tr>
{% endif %}
{% endfor content_list %}
</tbody>
<script>
function load_workorder(content_id) {
workorder_path = "/dashboard/workorder_list/" + content_id + "/?format=json";
workorder_table.api().ajax.url(workorder_path).load();
</script>
Inside the child table I have a button to pop up a modal form to create a new content in it, one of its field is a dropdown ForeignKey
field. 在子表中,我有一个按钮,用于弹出模式窗体以在其中创建新内容,它的字段之一是下拉的
ForeignKey
字段。
<div class="modal-body">
<div class="field">
<label>Project Type</label>
<select name="parent_project_content" required="" id="id_parent_project_content">
<option value="">---------</option>
<option value="1" selected="">Iron man suit</option>
<option value="2">SEQ 001</option>
<option value="4">SEQ 002</option>
</select>
</div>
The <option>
value is equal to the {{ content.id }}
.I want to hide this field from user so how can I change the <option>
to selected based on the {{ content.id }}
click by user. <option>
值等于{{ content.id }}
。我想向用户隐藏此字段,因此如何根据用户单击的{{ content.id }}
将<option>
更改为选定的字段。
Any help is much appreaciated, Thanks 任何帮助都非常感谢,谢谢
So if the content.id value matches that of the option in the select dropdown the jQuery for that is pretty straightforward. 因此,如果content.id值与select下拉列表中的选项匹配,则jQuery非常简单。
Here's a little example but all you need to do is call the selectOption()
function and pass through your content.id value: 这是一个小示例,但您需要做的就是调用
selectOption()
函数并传递您的content.id值:
// my simplified example to grab a value from the table
$('td').click(function(){
var value = $(this).text();
selectOption(value);
});
// pass the value to this function
function selectOption(v){
$('#id_parent_project_content option[value="'+v+'"]').prop('selected', true);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.