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