如何使用Codeigniter从另一个视图加载视图文件,而无需通过控制器? [英] How to load a view file from within another view with Codeigniter WITHOUT having to pass through a controller?

查看:219
本文介绍了如何使用Codeigniter从另一个视图加载视图文件,而无需通过控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在努力寻找解决这个问题的方法,我需要从另一个视图中加载视图。我知道,通常,我只需要做:

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

但这次,文件路径被传递到数据文件标记的属性。
我有:

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

实际上,这是由javascript函数使用来加载视图本身。所以当这样做时,它加载控制器而不是视图文件。 (或者甚至可能不加载任何东西)
让我说我把我的视图到app / views文件夹。我如何确保这个脚本实际加载这个文件而不需要控制器?



如果这是不可能的,我如何适应js脚本加载,而不是文件本身(这里是控制器第一),但是CI控制器返回的最终视图?



EDIT:JS脚本:

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

event .preventDefault();

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

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

});

并且载入投资组合函式为:

  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 ;
});
}

如果这太复杂了,

解决方案

/ div>

不要这样做,是一个坏主意。为此,您需要从views文件夹中删除.htaccess文件。



而是使用jquery get函数来创建ajax请求。
$ .get(main / loadCocktailRecipient / 1);



最好的方法是做一个新的控制器,返回一个json对象, 。然后,使用$ .get检索json,最后,你遍历该json,随时随地放置内容。



从jQuery 文档

  $。ajax({
url:main / loadCocktailRecipient,//检索数据的url
data:{id:1},//可以在这里发送get参数
success:success,//返回的数据(你的json或html)
dataType:dataType // html,json等等...如果返回的dataType不是你指定的, .error函数检查jquery文档
});


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

解决方案

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
});

这篇关于如何使用Codeigniter从另一个视图加载视图文件,而无需通过控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆