1

item.Name をチェックする jsgrid のカスタムバリデーターを作成しようとしました。DB から実際に存在するかどうかを挿入または編集する前に、行の重複を回避します。

jsgrid のドキュメントを 3 回読んだ問題ですが、問題を解決する方法がわかりませんでした。

この作品https://github.com/tabalinas/jsgrid-phpは私を助けすぎて、自分の仕事と mysqli に役立つように変身しました。

アイデア:値が存在するかどうかをDBにチェックする機能を作成し、存在する場合はPHPクラスでtrueを返すかfalseを返します

public function checkName($name){
    try{
        if(!empty($name)){
            $co_conn = $this->CON_conn->open_connect();
            $getsame = mysqli_query($co_conn,"select Name from category where Name = '$name'");
            if($getsame){
                $numit = mysqli_num_rows($getsame);
                if($numit > 0) return true;
                else return false;
            }
            else return true;
            $this->CON_conn->close_connect($co_conn);
        }
        else return true;
    }
    catch(Exception $er){
        return true;
    }
}

そして、この関数を外部ファイル「CheckValue.php」で呼び出しました

<?php require_once '////the director of the class';
$chnow = new NameOfMyClass();
$result = true;
switch($_SERVER["REQUEST_METHOD"]) {
    case "CHGO":
        parse_str(file_get_contents("php://input"), $_CHGO);
        //to ensure if it work i tested it.
        $result = $chnow->checkName($_CHGO["Name"]);
    break;
    default:
        $result = true;
    break;
}
header('Content-type: application/json');
echo json_encode($result);
?>

basic.html で

$(function() {
    $.ajax({
        type: "GET",
        url: "../bin/update.php"
    }).done(function(response) {
        response.unshift({ ID: "0", Name: "" });
        $("#jsGrid").jsGrid({
            height: "50%",
            width: "70%",
            selecting: false,
            filtering: false,
            editing: false,
            sorting: false,
            paging: true,
            autoload: true,
            pageSize: 15,
            pageButtonCount: 5,
            controller: {
                loadData: function(filter) {
                    return $.ajax({type:"GET", url: "../bin/update.php",data:filter});
                },
                updateItem: function(item) {
                    return $.ajax({type:"PUT",url: "../bin/update.php",data: item});
                },
                insertItem: function(item) {
                    /*$.ajax({type: "CHGO",url: "..bin/Checkvalue.php",data:item.Name, dataType: "json",
                    success: function(data){
                        JSON.parse(data);
                        consol.log(data);
                        //alert(data);
                        //I used many ways to find why its not work, then I commented it
                        }
                    });
                    //return $.ajax({type: "POST",url: "../bin/update.php",data: item});
                    //return data;

                            //else sweetAlert("error", "this value is exist!", "error");
                            //here I thought merge sweetalert js will give a good look of work not the normal message.
*/
                }
            },
            fields: [
                { name: "ID", type: "number",width: 50, editing:false, inserting:false },
                { name: "Name", type: "text", width: 50, validate: ["required",{
                        message: "this value is exist",
                        validator: function(value,item){
                            if(value != "")
                            {
                                $.ajax({
                                    type: "CHGO",
                                    url: "..bin/Checkvalue.php",
                                    data: item.Name,
                                    success: function(data){
                                        if(data == true) 
                                        //I assume here return the true/false to activate the error message but nothing work..sorry
                                    }
                                });
                            }
                        }
                    } ]
                },
                { type: "control", modeSwitchButton: false, deleteButton: false }
            ]
       });
    });
    $(".config-panel input[type=checkbox]").on("click", function() {
        var $cb = $(this);
        $("#jsGrid").jsGrid("option", $cb.attr("id"), $cb.is(":checked"));
    });     
});

助けてください。問題を解決するために 2 日間試してみます。jsgrid-1.4.1を使用した検証jsファイルを作成してくれた開発者に感謝します

4

1 に答える 1

2

その理由は、validate同期検証のみをサポートしており、この例の検証には ajax 呼び出しが必要であるため、非同期です。

次の 2 つのオプションがあります。

  1. 事前にデータをクライアントにロードし、クライアントでアイテムの挿入を検証します (したがって、検証は同期されます)。
  2. メソッドへの挿入を検証しinsertItemます。詳細については、次の問題を確認してくださいhttps://github.com/tabalinas/jsgrid/issues/190
于 2016-03-29T17:35:06.607 に答える