2
Personal I have an excerpt of a code referring to image below
In the first div shows a list of students. In the second the students that the user chooses for his group, as he clicks on each student I have a function jquery that inserted the student in the group div and limits him to insert a maximum of 5 (outside the user himself) and the reverse process to remove the students from the group, every click sends him back to div students. I need to apply a business rule where necessarily the user should choose two students of each course, so I thought to put the ID of each course in the ID of li
, but I don’t know how to read these id and allow only two students of each course in your group.
I tried to use .length
with a if
right after you check the maximum amount to make the comparisons, but it did not happen. ideas or examples to help please?
Code JQUERY:
$(function(){
$(".aluno").click(function(){ //EVENTO CLICK
var c = 1; //CONTADOR
$("#grupo .aluno").each(function(){
c++;
});
if(($(this).parent().attr("id") == "lista") && c <= 5) { //VERIFICA EM QUAL DIV PERTENCE E QUANT. MÁXIMA
$("#grupo").append(this); //ANEXA NA DIV GRUPO
$("#grupo .aluno").addClass("selecionado"); //INSERIR CLASSE COM A COR DIFERENTE
} else {
$("#lista").append(this); //ANEXA NA DIV ALUNOS
$("#lista .aluno").removeClass("selecionado"); //REMOVER CLASSE
}
});
});
HTML:
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="style.css">
<div class="lista-alunos">
<h3></i>Alunos</h3>
<ol id="lista">
<li class="aluno" id="1">Gabriel | ENG.</li>
<li class="aluno" id="1">Rafael | ENG.</li>
<li class="aluno" id="1">Samuel | ENG.</li>
<li class="aluno" id="1">Alex | ENG.</li>
<li class="aluno" id="1">Ricardo | ENG.</li>
<li class="aluno" id="2">Felipe | ADS.</li>
<li class="aluno" id="2">Cesar | ADS.</li>
<li class="aluno" id="2">Pedro | ADS.</li>
<li class="aluno" id="2">Maria | ADS.</li>
<li class="aluno" id="2">Ana | ADS.</li>
<li class="aluno" id="3">Lucas | ARQ.</li>
<li class="aluno" id="3">Miguel | ARQ.</li>
<li class="aluno" id="3">David | ARQ.</li>
<li class="aluno" id="3">Julia | ARQ.</li>
<li class="aluno" id="3">Alice | ARQ.</li>
</ol>
</div>
<div class="lista-grupo">
<h3>Grupo</h3>
<ol id="grupo">
<li class="lider" id="1">Eu | ENG.</li>
</ol>
</div>
CSS:
.lista-alunos, .lista-grupo{
position: relative;
float: left;
display: block;
width: 270px;
height: 270px;
padding: 10px;
margin-right: 10px;
border: 1px solid #ccc;
background-color: #F7F7F7;
}
#lista{
overflow-y: scroll;
height: 205px;
}
#lista, #grupo{
padding-left: 20px;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.aluno, .lider{
margin: 3px;
padding: 4px;
cursor: pointer;
}
.aluno{
background: #CACACA;
}
.selecionado, .lider{
background: #A1B3A1;
}
"I need to apply a business rule where the user must choose two students from each course" - you want to select two of each time or prevent selecting more than two of each course?
– Sergio
prevent selecting more than two from each course
– Gabriel Godoy
Buddy, I don’t understand very well, do you want to tell which Ivs? Answers me here that when I do I edit with the result.
– Raphael Caldas
In the Group div must have two students of each course mandatory to validate. Example of a valid group: two ENG students, two ARQ and two ADS students
– Gabriel Godoy
Can you explain the logic of
selecionado
? when and what is selected when you click?– Sergio
It was a way that I found to change the color of the students entered in the group, and remove it in case it goes back to the student list. That is to say, it has a class in CSS . selected with green backgroud, when it goes to the group it receives this class, when it leaves the group it removes the class. I tried to add directly by style, but they changed the two lists when clicked
– Gabriel Godoy
The leader also counts as a student in duplicates?
– Sergio
Buddy, the html and javascript that you provided not well formatted, it is complicated to tidy, please put the real code, so that I can adapt your code with the counter
– Raphael Caldas
yes the leader counts, it already comes when the page is loaded. in case it is the user himself
– Gabriel Godoy