Minggu, 19 Februari 2023

Validasi Parsley Sebelum Submit

 function simpan_create() {

        $("#btn_create").prop('disabled', true);
        $('#form_modal').parsley().reset();
        $('#form_modal').parsley().validate();
        if ($('#form_modal').parsley().isValid()) {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            })
            $.ajax({
                url: "location/insert",
                type: "POST",
                data: $('#form_modal').serialize(),
                dataType: "json",
                success: function(response) {
                    if (response.status == 200) {
                        $("#modalForm").modal('hide');
                        toastr["success"](response.message);
                        locationTable.ajax.reload(null, false);
                        if (response.stay == '1') {
                            setTimeout(function() {
                                show_create();
                            }, 1000);
                        }
                    } else {
                        alert('something wrong!!!');
                    }
                    $("#btn_create").prop('disabled', false);
                },
                statusCode: {
                    422: function(data) {
                        $.each(data.responseJSON.errors, function(key, value) {
                            // Set errors on inputs
                            if (key == 'location') {
                                $('#err_room').html(value);
                                $('#err_room').show();
                                $("#btn_create").prop('disabled', false);
                                //alert(value);
                                // $('#input_kode_location').parsley().addError('customError', {
                                //     message: value
                                // });
                            }

                        });
                        $("#btn_create").prop('disabled', false);
                    }
                }
            });
        } else {
            $("#btn_create").prop('disabled', false);
        }
    }

Jumat, 17 Februari 2023

Select2 Search And Add New Tag


$("#input_location").select2({
                        tags: true,
                        createTag: newtag,
                        matcher: matchCustom,
                        width: '100%',
                        dropdownParent: $('#modalFormLabel'),
                    });

 function newtag(params, data) {

        var term = $.trim(params.term);
        term = term.toUpperCase();
        if (term === '') {
            return null;
        }

        return {
            id: term,
            text: term + " [Tambah Baru]",
            newTag: true // add additional parameters
        }
    }

    function matchCustom(params, data) {
        if ($.trim(params.term) === '') {
            return data;
        }
        if (typeof data.text === 'undefined') {
            return null;
        }
        var original = data.text;
        var term = params.term.toLowerCase();
        if (original.toLowerCase().indexOf(term) > -1) {
            return data;
        }
        if (data.newTag === false) {
            return {
                id: params.term,
                text: params.term,
                newTag: true
            };
        }
        return null;
    }