|
518 | 518 |
|
519 | 519 | for (var i = 0; i < dt.context[0].aoColumns.length; i++) { |
520 | 520 | columnDefs.push({ |
521 | | - title: dt.context[0].aoColumns[i].sTitle |
| 521 | + title: dt.context[0].aoColumns[i].sTitle, |
| 522 | + dataType: dt.context[0].aoColumns[i].sType, |
| 523 | + isPrimary: dt.context[0].aoColumns[i].isPrimary, |
| 524 | + value : "", |
522 | 525 | }) |
523 | 526 | } |
524 | 527 |
|
525 | 528 |
|
526 | | - var data = ""; |
527 | | - |
528 | | - data += "<form name='altEditor-form' role='form'>"; |
529 | | - |
530 | | - for (var j in columnDefs) { |
531 | | - data += "<div class='form-group'><div class='col-sm-3 col-md-3 col-lg-3 text-right' style='padding-top:7px;'><label for='" + columnDefs[j].title + "'>" + columnDefs[j].title + ":</label></div><div class='col-sm-9 col-md-9 col-lg-9'><input type='text' id='" + columnDefs[j].title + "' name='" + columnDefs[j].title + "' placeholder='" + columnDefs[j].title + "' style='overflow:hidden' class='form-control form-control-sm' value=''></div><div style='clear:both;'></div></div>"; |
532 | | - |
533 | | - } |
534 | | - data += "</form>"; |
| 529 | + var data = ""; |
| 530 | + |
| 531 | + data += "<form class='form-horizontal' name='altEditor-form' role='form'>"; |
| 532 | + |
| 533 | + for (var j in columnDefs) { |
| 534 | + var inputSectionHTML = "<div class='form-group'><label for='__INPUT_NAME__' class='col-sm-4 control-label'>__INPUT_NAME__</label>__INPUT_HTML__</div>"; |
| 535 | + |
| 536 | + var inputHTML = "<div class='col-sm-7'><input data-type='__INPUT_DATA_TYPE__' __INPUT_READ_ONLY_ATTRIBUTE__ type='__INPUT_TYPE__' id='__INPUT_NAME__' name='__INPUT_NAME__' placeholder='__INPUT_NAME__' style='overflow:hidden' class='form-control' value='__INPUT_VALUE__'></div>"; |
| 537 | + |
| 538 | + var option1Checked = ""; |
| 539 | + var option2Checked = "checked"; |
| 540 | + if (columnDefs[j].dataType == "boolean") { |
| 541 | + if(JSON.parse(columnDefs[j].value)) { |
| 542 | + option1Checked = "checked"; |
| 543 | + option2Checked = ""; |
| 544 | + } |
| 545 | + inputHTML = "<div class='col-sm-7'><div class='checkbox'><label><label class='radio-inline'><input data-type='__INPUT_DATA_TYPE__' __OPTION_1_CHECKED__ type='radio' name='__INPUT_NAME__' id='__INPUT_NAME__' value='1'>true</label><label class='radio-inline'><input data-type='__INPUT_DATA_TYPE__' __OPTION_2_CHECKED__ type='radio' name='__INPUT_NAME__' id='__INPUT_NAME__' value='0'>false</label></div></div>" |
| 546 | + } |
| 547 | + //set input type |
| 548 | + var inputType = "text"; |
| 549 | + var inputReadOnlyAttribute = ""; |
| 550 | + switch (columnDefs[j].dataType) { |
| 551 | + case 'num': |
| 552 | + inputType = "number"; |
| 553 | + break; |
| 554 | + case 'string': |
| 555 | + inputType = "text"; |
| 556 | + break; |
| 557 | + } |
| 558 | + //set input to read-only if it is a primary key |
| 559 | +// if (columnDefs[j].isPrimary) { |
| 560 | +// inputReadOnlyAttribute = "readonly" |
| 561 | +// } |
| 562 | + |
| 563 | + //append input html |
| 564 | + inputSectionHTML = inputSectionHTML.replace(/__INPUT_HTML__/g, inputHTML); |
| 565 | + inputSectionHTML = inputSectionHTML.replace(/__INPUT_READ_ONLY_ATTRIBUTE__/g, inputReadOnlyAttribute); |
| 566 | + inputSectionHTML = inputSectionHTML.replace(/__INPUT_TYPE__/g, inputType); |
| 567 | + inputSectionHTML = inputSectionHTML.replace(/__INPUT_DATA_TYPE__/g, columnDefs[j].dataType); |
| 568 | + inputSectionHTML = inputSectionHTML.replace(/__INPUT_VALUE__/g, columnDefs[j].value); |
| 569 | + inputSectionHTML = inputSectionHTML.replace(/__INPUT_NAME__/g, columnDefs[j].title); |
| 570 | + inputSectionHTML = inputSectionHTML.replace(/__OPTION_1_CHECKED__/g, option1Checked); |
| 571 | + inputSectionHTML = inputSectionHTML.replace(/__OPTION_2_CHECKED__/g, option2Checked); |
| 572 | + data += inputSectionHTML; |
| 573 | + } |
| 574 | + data += "</form>"; |
535 | 575 |
|
536 | 576 |
|
537 | 577 | $('#altEditor-modal').on('show.bs.modal', function() { |
|
553 | 593 | var data = []; |
554 | 594 |
|
555 | 595 | $('form[name="altEditor-form"] input').each(function(i) { |
556 | | - data.push($(this).val()); |
| 596 | + var addToList = true; |
| 597 | + var value = $(this).val(); |
| 598 | + if($(this).attr('type') == "radio" && $(this).prop('checked') == false) { |
| 599 | + addToList = false; |
| 600 | + } |
| 601 | + value = $(this).attr('type') == "radio" ? $(this).val() == "1" : value; |
| 602 | + if (addToList){ |
| 603 | + data.push({ |
| 604 | + "value": value, |
| 605 | + "dataType": $(this).attr('data-type') |
| 606 | + }); |
| 607 | + } |
557 | 608 | }); |
| 609 | + var editButtonCurrentText = $("#editRowBtn").text(); |
| 610 | + $("#addRowBtn").addClass('disabled'); |
| 611 | + $("#addRowBtn").text("Saving.."); |
558 | 612 |
|
559 | | - $('#altEditor-modal .modal-body .alert').remove(); |
| 613 | + console.log(JSON.stringify(data)); |
560 | 614 |
|
561 | | - var message = '<div class="alert alert-success" role="alert">\ |
562 | | - <strong>Success!</strong> This record has been added.\ |
563 | | - </div>'; |
| 615 | + that._emitEvent("add-row", [ |
| 616 | + JSON.stringify(data), |
| 617 | + function(isAdded) { |
564 | 618 |
|
565 | | - $('#altEditor-modal .modal-body').append(message); |
566 | 619 |
|
567 | | - dt.row.add(data).draw(false); |
| 620 | + //set error message and other properties based on whether update is successfull or not |
| 621 | + var alertAdditionClasses = "alert-success"; |
| 622 | + var alertMessage = "This record has been added"; |
| 623 | + var alertHeading = "Success"; |
| 624 | + if (!isAdded) { |
| 625 | + alertAdditionClasses = "alert-danger"; |
| 626 | + alertMessage = "Error occurred while adding this record"; |
| 627 | + alertHeading = "Error"; |
| 628 | + } |
| 629 | + |
| 630 | + //create alert element html and append it to modal |
| 631 | + var messageHTML = '\ |
| 632 | + <div class="alert __ALERT_ADDITION_CLASSES__" role="alert">\ |
| 633 | + <strong>__ALERT_HEADING__!</strong>\ |
| 634 | + __ALERT_MESSAGE__.\ |
| 635 | + </div>\ |
| 636 | + '; |
| 637 | + messageHTML = messageHTML.replace(/__ALERT_ADDITION_CLASSES__/g, alertAdditionClasses); |
| 638 | + messageHTML = messageHTML.replace(/__ALERT_HEADING__/g, alertHeading); |
| 639 | + messageHTML = messageHTML.replace(/__ALERT_MESSAGE__/g, alertMessage); |
| 640 | + $('#altEditor-modal .modal-body').append(messageHTML); |
568 | 641 |
|
| 642 | + //update datatable, if update is successfull |
| 643 | + if (isAdded) { |
| 644 | + dt.row().data(data); |
| 645 | + //remove existing alert elements |
| 646 | + $('#altEditor-modal').modal('hide'); |
| 647 | + } |
| 648 | + $("#addRowBtn").removeClass('disabled'); |
| 649 | + $("#addRowBtn").text(editButtonCurrentText); |
| 650 | + } |
| 651 | + ]); |
569 | 652 | }, |
570 | 653 |
|
571 | 654 | _getExecutionLocationFolder: function() { |
|
0 commit comments