简体   繁体   English

如何在 javascript 的一个 select 下拉列表中显示所有父值和子值

[英]How to show all parent and child values in one select dropdown in javascript

Hi guys i have topic table where i am storing all topics along with sub topics..and subtopic id i am storing in parent_id column.大家好,我有一个主题表,我在其中存储所有主题以及子主题..以及我存储在 parent_id 列中的子主题 ID。

Here is my db structure:这是我的数据库结构: 在此处输入图像描述

i am getting all topics in dropdown like this.我正在像这样的下拉列表中获取所有主题。 But i would like to show my subtopics list like this但我想像这样显示我的子主题列表

fractions 
fractions>introductions to fractions
fractions>Fractions as division

So far i have create childTopics functions in my model:到目前为止,我已经在 model 中创建了 childTopics 函数:

public function childTopics()
{
    return $this->hasMany(Topic::class, 'parent_id');
}

And i used in my create function in controller like this:我在 controller 中创建 function 时使用如下:

 $topics = Topic::with('childTopics')->parent()->get(['id', 'name']);
   

Here is my ajax call where i am showing all my topics in one place.这是我的 ajax 通话,我在一个地方展示了我的所有主题。

function getSubjectsTopics(subject_id)
{
    if(subject_id) {
        loading_show();
    axios.get("/master/topic/get-topic-by-subject-id/" + subject_id)
        .then(function(response) {
            var optionHtml = '<option value="0">Parent</option>';
            if(response.data.status) {
                $.each(response.data.subject_topics, function(i,v) {
                    optionHtml += `<option value="${v.id}">${v.name}</option>`;
                 });
            }

            $("#ddl_topic_type").html(optionHtml).attr('disabled', false).select2();
            loading_hide();
        })
        .catch(function(error) {
            loading_hide();
            console.log(error);
            Swal.fire({
                type: 'error',
                title: 'Oops...',
                text: 'Something went wrong!'
            })
        })
    } else {
        $("#ddl_topic_type").attr('disabled', true);
    }
}

In this ajax call itself i would like to show my subtopics with parent topic name itself.在这个 ajax 调用本身中,我想用父主题名称本身显示我的子主题。

Can anyone help me how can i show it.任何人都可以帮助我如何展示它。 Thanks in advance.提前致谢。

Edit: Here is my response output编辑:这是我的回复 output 在此处输入图像描述

Here is functions to get topics based on subject:以下是根据主题获取主题的函数:

public function getTopicsBySubjectID($subject_id)
{
    $topics = Topic::where("subject_id", $subject_id)->get(['id', 'name']);

    return response()->json(['status' => 'success', 'subject_topics' => $topics], 200);
}

You can use optGroup to group your sub option in a group where optGroup will have the name of the subject name .Your current ajax response show all subject_topics together so if the first value fractions is subject name you can put condition inside each loop to check if i (position) is 0 then append optgroup .您可以使用optGroup将您的子选项分组到optGroup将具有subject name名称的组中。您当前的subject_topics ajax response将所有主题主题一起显示,因此如果第一个值fractions是主题名称,您可以在each循环中放置条件以检查是否i (位置)为0然后 append optgroup

Demo code :演示代码

 var response = { 'data': { 'status': 'success', 'subject_topics': [{ 'id': 0, 'name': 'fractions' }, { 'id': 1, 'name': 'fractions of booksss of subject' }, { 'id': 2, 'name': 'fractions of sub' }] } }; var optionHtml = '<option>Select....</option>'; if (response.data.status) { $.each(response.data.subject_topics, function(i, v) { if (i == 0) { optionHtml += `<optGroup label="${v.name}">` //considering 1st id is subject name } else { optionHtml += `<option value="${v.id}">${v.name}</option>`; } }); optionHtml += `<optGroup>` //close optgroup } $("#ddl_topic_type").html(optionHtml).attr('disabled', false).select2(); //or var response = { 'data': { 'status': 'success', 'subject': 'somesubjectname', //return subject name as well 'subject_topics': [{ 'id': 0, 'name': 'fractions' }, { 'id': 1, 'name': 'fractions of booksss of subject' }, { 'id': 2, 'name': 'fractions of sub' }] } }; var optionHtml1 = '<option>Select....</option>'; if (response.data.status) { //append subject name optionHtml1 += `<optGroup label="${response.data.subject}">` $.each(response.data.subject_topics, function(i, v) { optionHtml1 += `<option value="${v.id}">${v.name}</option>`; }); //subject name.. optionHtml1 += `<optGroup>` } $("#ddl_topic_type1").html(optionHtml1).attr('disabled', false).select2();
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://rawgit.com/select2/select2/master/dist/css/select2.min.css"> <script src="https://rawgit.com/select2/select2/master/dist/js/select2.js"></script> <select id="ddl_topic_type"></select> <select id="ddl_topic_type1"></select>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在kendo TreeList中选择父节点时选择所有子节点? javascript - How to select all child nodes on selecting parent in kendo TreeList? javascript 如何通过JavaScript隐藏下拉列表并显示某个仅选择“其他”下拉列表的值4 - How to Hide DropDown Through JavaScript and Show when Some One Select Other dropdown only value 4 如何仅显示父级和子级元素? Java脚本 - How to show only Parent and Child element? Javascript javascript / jquery选择父元素内的所有子元素 - javascript/jquery select all child elements inside a parent element JavaScript:使用通配符从PARENT元素中选择所有子元素 - JavaScript: Use a wildcard to select all child elements from a PARENT element Javascript数组在不知道父键的情况下获取所有子值 - Javascript array get all child values without knowing the parent key 如何使用 querySelectorAll 获取父级的所有子级,不包括 javascript 中的一个子级 - How to use querySelectorAll to get all children of a parent excluding only one child in javascript angular 6. 我想要 select 父复选框需要隐藏所有子复选框的层次结构下拉列表 - Hierarchy dropdown with nth child in angular 6. I want select parent checkbox need hide all it children checkbox 通过Javascript和/或Jquery验证下拉值是否彼此不同? - Validate that dropdown values are all distinct from one another with Javascript and/or Jquery? Javascript-存储父母和孩子的价值观 - Javascript - Storing Parent and Child Values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM