Yii2 modal with composite key

2019-06-13 08:05发布

问题:

Can anyone help me with the composite key? I am not able to function properly.

function init_click_handlers(){
  $(".button-endereco").click(function(e) {
            var fcodigo = $(this).closest("tr").data("codigo");
            var fcodigopessoa = $(this).closest("tr").data("codigopessoa");
            var map = {codigo: $(this).closest("tr").data("codigo"), codigopessoa: $(this).closest("tr").data("codigopessoa")};
            $.get(
                "update ",
                {
                    codigo: fcodigo
                    codigopessoa: fcodigopessoa
                },
                function (data)
                {
                    $("#endereco-modal").find(".modal-body").html(data);
                    $(".modal-body").html(data);
                    $("#endereco-modal").modal("show");
                }
            );
        });

}

init_click_handlers(); //first run
$("#endereco_id").on("pjax:success", function() {
  init_click_handlers(); //reactivate links in grid after pjax update
});

$url = Yii::$app->urlManager->createUrl('../endereco/update?codigo='.$dataProvider->codigo.'&codigopessoa='.$dataProvider->codigopessoa);

回答1:

If you want refer to the data inside a $dataProvider you firts need to get the model you need. In dataProvider all the models relate to your query are available and you can get from the models array the specific model accessing by the a proper index eg:

myModel = $dataProvider->models[yourIndex]

myValue = myModel->myField

In your case foe example you could get the vaue in this way

myModel = $dataProvider->models[0]:
myValue = myModel->codigo;


回答2:

I got 90% of the response, but could not catch my composite key (password, codigopessoa), forced the values to test the function and it worked. So lack I get the column values (composite key).

function init_click_handlers(){
  $(".button-endereco").click(function(e) {
             fcodigo = $(this).closest("tr").data("codigo");
             fcodigopessoa = $(this).closest("tr").data("codigopessoa");
             $.ajax({
                url: "'.Yii::$app->urlManager->createUrl('endereco/update').'",
                type: "GET",
                data: {"codigo": parseInt(17), "codigopessoa":parseInt(8)},
                dataType: "html",
                success: function(data) {
                        $("#endereco-modal").find(".modal-body").html(data);
                        $(".modal-body").html(data);
                        $("#endereco-modal").modal("show");
                }
            }); 
        });
}

init_click_handlers(); //first run
$("#endereco_id").on("pjax:success", function() {
  init_click_handlers(); //reactivate links in grid after pjax update
});


回答3:

[100% working] Finally got it, for those who want to use the Gridview (kartik) with composite key follows the code:

function init_click_handlers(){
  $(".button-endereco").click(function(e) {
             chave = $(this).closest("tr").data("key");
             $.ajax({
                url: "'.Yii::$app->urlManager->createUrl('endereco/update').'",
                type: "GET",
                data: {"codigo": parseInt(chave["codigo"]), "codigopessoa":parseInt(chave["codigopessoa"])},
                //data: {keylist: parseInt(keys)},
                dataType: "html",
                success: function(data) {
                        $("#endereco-modal").find(".modal-body").html(data);
                        $(".modal-body").html(data);
                        $("#endereco-modal").modal("show");
                }
            }); 
        });
}

init_click_handlers(); //first run
$("#endereco_id").on("pjax:success", function() {
  init_click_handlers(); //reactivate links in grid after pjax update
});