How to load a view file from within another view w

2019-07-08 11:32发布

I'm struggling to find a solution to this problem, I need to load a view from within another view. I know, usually, i would just have to do :

<?php $this->load->view("smthg");?>

But this time, the file path is passed to a data-file tag's property. I have :

<a data-file="<?php echo site_url('main/loadCocktailRecipient') ?>/{{Id}}" href="#">

and actually, this is used by a javascript function to load the view by itself. so when doing like so, It load the controller instead of the view file. (or it maybe even do not load anything) Let say I've put my view into the app/views folder. how can I make sure this script actually loads this file without the need of the controller ?

If this is not possible, how can I adapt the js script to load, not the file itself (which here, would be the controller first) but the final view returned by CI' controller ?

EDIT: JS script:

$(".cocktail .cocktail-item a").on('click', function(event) {

            event.preventDefault();

            var fileToLoad = $(this).data('file');

            if(portfolioActive) {
                closePortfolio(true, fileToLoad);
            } else {
                loadPortfolio(fileToLoad);
            }

        });

and load portfolio function is :

function loadPortfolio(fileToLoad) {
            $portfolioSingle.load(fileToLoad, function() {
                portfolioSingleH = $portfolioSingle.find('.container').outerHeight();
                $portfolioSingle.css({
                    'top': -portfolioSingleH
                });
                $('#portfolio').animate({ scrollTop: 0 }, "slow");
                $portfolioSingle.stop().animate({
                    'top': 0
                }, 500, 'easeOutCubic');
                $portfolioContainer.stop().animate({
                    'marginTop': portfolioSingleH
                });
                portfolioActive = true;
                bindClosePortfolio();
                bindFancybox();
                setupFlexslider();
            });
        }

if this is too complex, then how can I simply tell him to look for a handlebar script by id and load the content that would be generated dynamically ?

Thanks

1条回答
SAY GOODBYE
2楼-- · 2019-07-08 11:32

Don't do that, is a bad idea. To do that you need to remove the .htaccess file from the views folder.

Instead, use the jquery get function that makes an ajax request. $.get("main/loadCocktailRecipient/1");

The best way is to do a new controller that returns a json object with the data requested. Then, with a $.get you retrieve that json, and finally, you iterate over that json to put the contents whenever you want.

From the jQuery Documentation:

$.ajax({
    url: "main/loadCocktailRecipient", //the url to retrieve the data
    data: { id: "1" }, //you can send the get parameters here
    success: success, //the data returned (your json or html)
    dataType: dataType //html, json, etc... if the dataType returned is not what you specify it will enter on a .error function. check the jquery documentation
});
查看更多
登录 后发表回答