Add An Item In Cgridview

5407

add_item_cgridview.png

I want to add a blank line in cgridview to save it as a new object on database and refresh CGridView.

How can I do that ?

Do you have some examples?

Assuming you already have the form fields in place, you need to handle the click event on the button to send a post request with the form fields. If you’ve wrapped the fields in a form which points to the correct action, it could be as simple as putting this script in your view:




<script type="text/javascript">

    $(document).ready(function(){

        $("body").on("click", "#yourButtonId", function(){

            var form = $("#yourFormId");


            $.post(form.attr("action"), form.serialize(), function(){

                $.fn.yiiGridView.update("yourGridId");

            }).fail(function(){

                alert("Something went wrong.");

            });

        });

    });

</script>



Look into jQuery’s post documentation to get a better idea of how you can manage validation errors and such.

I have no problem with the clic action to add an item I have a problem to add a blank line in the CGridView.

Maybe you can put the form fields into the footer of each column? That may preclude you from wrapping it in a form though.