Click here to Skip to main content
15,892,517 members
Home / Discussions / JavaScript
   

JavaScript

 
RantJavascript , a devil spawn language. Pin
Armando de la Torre22-Oct-15 6:12
Armando de la Torre22-Oct-15 6:12 
SuggestionRe: Javascript , a devil spawn language. Pin
Shmuel Zang24-Oct-15 20:58
Shmuel Zang24-Oct-15 20:58 
GeneralRe: Javascript , a devil spawn language. Pin
Armando de la Torre28-Oct-15 10:52
Armando de la Torre28-Oct-15 10:52 
GeneralRe: Javascript , a devil spawn language. Pin
Jacobclay716-Nov-15 10:03
Jacobclay716-Nov-15 10:03 
GeneralRe: Javascript , a devil spawn language. Pin
W Balboos, GHB27-Oct-15 8:19
W Balboos, GHB27-Oct-15 8:19 
GeneralRe: Javascript , a devil spawn language. Pin
Armando de la Torre28-Oct-15 11:02
Armando de la Torre28-Oct-15 11:02 
AnswerRe: Javascript , a devil spawn language. Pin
ZurdoDev23-Nov-15 3:05
professionalZurdoDev23-Nov-15 3:05 
QuestionButton in the tree is not fired Pin
Member 785420721-Oct-15 18:34
Member 785420721-Oct-15 18:34 
I want to use buttons in tree. So I used the article Working on JSON objects in jQuery and MVC given by Dr. Song Li in 24 Nov 2010. I have taken button instead of textnode. The buttons work fine first time. But these buttons do not fire after addExamBySession or addExamByjson is clicked.

 var dateDeserialize = function (dateStr) {
            var dt = new Date(parseInt(dateStr.substr(6)));
            return prependZero((dt.getMonth() + 1))
                + "/" + prependZero(dt.getDate())
                + "/" + dt.getFullYear()
                + " " + prependZero(dt.getHours())
                + ":" + prependZero(dt.getMinutes())
                + ":" + prependZero(dt.getSeconds());
        };

        var FixDateinJson = function (JsonStudents) {
            $.each(JsonStudents, function (i, JsonStudent) {
                $.each(JsonStudent.ClassesTaken, function (i, JsonClass) {
                    $.each(JsonClass.ExamesTaken, function (i, JsonExam) {
                        JsonExam.ExamTime = dateDeserialize(JsonExam.ExamTime);
                    });
                });
            });

            return JsonStudents;
        }

        var buildStudentTree = function (students) {

            var createTextNode = function (text,id) {
                var span = document.createElement("span");
                span.setAttribute("style", "margin-left: 2px");
                ($('<input>').attr('type', 'button').attr('id',id)
                .attr('value', text)).appendTo(span);
                return span;
            };

            var root = document.createElement("ul");
            root.id = "StudentTreeRoot";
            root.setAttribute("style", "margin: 15px");
            root.className = "filetree";
            $.each(students, function (i, student) {
                var studentNode = document.createElement("li");
                studentNode.className = "closed";
                var span = document.createElement("span");
                span.className = "folder";
                span.appendChild(createTextNode(student.Name, 's'+(i+1)));
                studentNode.appendChild(span);

                var classesNode = document.createElement("ul");
                $.each(student.ClassesTaken, function (j, aClass) {
                    var classNode = document.createElement("li");
                    classNode.className = "closed";
                    span = document.createElement("span");
                    span.className = "folder";
                    span.appendChild(createTextNode(aClass.Name, 'c' + (i+1)));
                    classNode.appendChild(span);

                    var examesNode = document.createElement("ul");
                    examesNode.className = "folder";

                    $.each(aClass.ExamesTaken, function (k, aExam) {
                        var examNode = document.createElement("li");
                        examNode.className = "closed";
                        span = document.createElement("span");
                        span.className = "folder";
                        span.appendChild(createTextNode(aExam.Description, 'e' + (i + 1)));
                        examNode.appendChild(span);

                        var detailNode = document.createElement("ul");
                        var examTime = document.createElement("li");
                        span = document.createElement("span");
                        span.className = "file";
                        span.appendChild(createTextNode(aExam.ExamTime,'et'+(i+1)));
                        examTime.appendChild(span);
                        detailNode.appendChild(examTime);

                        var score = document.createElement("li");
                        span = document.createElement("span");
                        span.className = "file";
                        span.appendChild(createTextNode(aExam.Score, 'es'+(i+1)));
                        score.appendChild(span);
                        detailNode.appendChild(score);

                        examNode.appendChild(detailNode);

                        examesNode.appendChild(examNode);
                    });

                    classNode.appendChild(examesNode)

                    classesNode.appendChild(classNode);
                });

                studentNode.appendChild(classesNode);
                root.appendChild(studentNode);
            });

            $("#StudentTree").html("").append(root);
            $("#StudentTreeRoot").treeview();
        };

        $(document).ready(function () {
            $("#StudentTree").html("");
            $.ajax({
                cache: false,
                type: "POST",
                async: false,
                url: GetStudentsURL
                    + "/?NoOfStudents=" + NoOfStudents
                    + "&NoOfClasses=" + NoOfClasses
                    + "&NoOfExams=" + NoOfExams,
                dataType: "json",
                success: function (students) {
                    StudentJson = FixDateinJson(students);
                    buildStudentTree(StudentJson);
                }
            });

            $("#btnAddAnExamJson").click(function () {
                $("#StudentTree").html("Loading ...");

                $.ajax({
                    cache: false,
                    type: "POST",
                    url: AddAnExamByJsonURL,
                    contentType: 'application/json',
                    dataType: "json",
                    data: JSON.stringify(StudentJson),
                    success: function (students) {
                        StudentJson = FixDateinJson(students);
                        buildStudentTree(StudentJson);
                    }
                });
            });

            $("#btnAddAnExamSession").click(function () {
                $("#StudentTree").html("Loading ...");

                $.ajax({
                    cache: false,
                    type: "POST",
                    url: AddAnExamBySessionURL,
                    dataType: "json",
                    success: function (students) {
                        StudentJson = FixDateinJson(students);
                        buildStudentTree(StudentJson);
                    }
                });
            });

            $("#s1, #s2, #s3, #c1, #c2, #c3, #e1, #e2, #e3").click(function () {
                alert($(this).attr('id') + ' Clicked');
                $.ajax({
                    cache: false,
                    type: "POST",
                    //url: AddAnExamBySessionURL,
                    dataType: "json",
                    success: function (students) {
                        StudentJson = FixDateinJson(students);
                        buildStudentTree(StudentJson);
                        
                    }
                });
            });
        });
    </script>
</head>

<body>
    <div id="TitleContainer"><%= ViewData["ApplicationTitle"]%></div>
    <div id="MainContainer">
        <div id="StudentTree"></div>
        <div id="ButtonContainer">
            <button id="btnAddAnExamSession" class="ButtonStyle">
                Add an exam to students using session</button>
            <button id="btnAddAnExamJson" class="ButtonStyle">
                Add an exam to students by posting Json</button>
        </div>
    </div>
</body>
</html>


modified 22-Oct-15 1:18am.

AnswerRe: Button in the tree is not fired Pin
F-ES Sitecore21-Oct-15 23:35
professionalF-ES Sitecore21-Oct-15 23:35 
GeneralRe: Button in the tree is not fired Pin
Member 785420722-Oct-15 19:54
Member 785420722-Oct-15 19:54 
QuestionNeed js help! Pin
Member 1197204321-Oct-15 15:33
Member 1197204321-Oct-15 15:33 
AnswerRe: Need js help! Pin
Wombaticus22-Oct-15 0:42
Wombaticus22-Oct-15 0:42 
QuestionYouTube API - upload videos by search term Pin
Member 1207417120-Oct-15 9:49
Member 1207417120-Oct-15 9:49 
QuestionHow to control the do / while infinite loop? Pin
Ted.K-117-Oct-15 1:06
Ted.K-117-Oct-15 1:06 
AnswerRe: How to control the do / while infinite loop? Pin
Wombaticus18-Oct-15 11:52
Wombaticus18-Oct-15 11:52 
GeneralRe: How to control the do / while infinite loop? Pin
Ted.K-123-Oct-15 21:43
Ted.K-123-Oct-15 21:43 
AnswerRe: How to control the do / while infinite loop? Pin
Blikkies18-Oct-15 22:02
professionalBlikkies18-Oct-15 22:02 
GeneralRe: How to control the do / while infinite loop? Pin
Ted.K-123-Oct-15 21:43
Ted.K-123-Oct-15 21:43 
AnswerRe: How to control the do / while infinite loop? Pin
W Balboos, GHB20-Oct-15 7:45
W Balboos, GHB20-Oct-15 7:45 
GeneralRe: How to control the do / while infinite loop? Pin
Ted.K-123-Oct-15 21:42
Ted.K-123-Oct-15 21:42 
QuestionHow to deal with Uncaught TypeError? Pin
Ted.K-117-Oct-15 0:59
Ted.K-117-Oct-15 0:59 
AnswerRe: How to deal with Uncaught TypeError? Pin
Wombaticus18-Oct-15 21:40
Wombaticus18-Oct-15 21:40 
GeneralRe: How to deal with Uncaught TypeError? Pin
Ted.K-124-Oct-15 19:44
Ted.K-124-Oct-15 19:44 
Questionhow to check the control is available or not? Pin
HoneyBee14-Oct-15 20:45
HoneyBee14-Oct-15 20:45 
AnswerRe: how to check the control is available or not? Pin
Blikkies15-Oct-15 4:49
professionalBlikkies15-Oct-15 4:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.