简体   繁体   English

通过点击数据表行从下拉列表中选择一个选项

[英]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.

相关问题 数据表问题编辑每一行 - data-table problems editing each row 如何在表中该行的其他可用下拉列表中显示从下拉列表中取消选择的选项-jQuery - how to show the deselected option from the dropdown list in the other available dropdown list of that row in a table - jquery 在数据表中以编程方式添加行时选择不呈现 - Select not being rendered when adding a row programmatically within Data-Table 如何在单击时刷新数据表并更改行背景色 - How to refresh data-table on click & change the row background color 如何在 vuetify 数据表的整行中添加工具提示 - How to add a tooltip in a entire row in vuetify data-table 我如何将按钮单击事件与 vuetify 数据表中一行内的行单击事件分开 - how i can separate the buttons click event from row click event inside a row in a vuetify data-table 使用数据表在 v-for 列表中添加或删除项目 - Add or Remove Items in a v-for list using data-table 在 angular 中的下拉列表 onclick 表格行中填充数据(基于表格行数据) - Fill data in dropdown onclick table row (based on table row data) in angular 从嵌套的json中进行Angular 4数据表排序 - Angular 4 data-table sorting from nested json 在下拉菜单中添加表格行,选择并删除行。 仅从特定下拉列表添加行一次 - Add table row on dropdown select & delete row. Only once add the row from particular dropdown select
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM