简体   繁体   English

OctoberCMS-如何在javascript中使组件数据可用

[英]OctoberCMS - How to make component data available in javascript

I need to dynamically fill data for a dashboard theme but I am unsure how to do this. 我需要动态填充仪表板主题的数据,但不确定如何执行此操作。 So far my code looks like this. 到目前为止,我的代码看起来像这样。

Component: 零件:

public $orders;

public function onRun() {
    $this->orders = $this->loadOrders();
}

protected function loadOrders() {
    $all_customers = Db::table('customers')->get();

    foreach ($all_customers as $customer) {

    }

    $orders = Db::table('orders')->where('company_name', $customer->company_name)->count();

    return $orders;
}

From this I get the amount of orders in the system for that customer which works fine but its the next part I am struggling with. 由此,我得到了该客户在系统中的订单数量,该订单工作正常,但我接下来要努力解决的问题。 This is my my javascript: 这是我的JavaScript:

var PIECHART = $('#pieChart');
var orders = ** This is where I am lost **;
var quotes = ** This is where I am lost **;
var invoices = ** This is where I am lost **;
var myPieChart = new Chart(PIECHART, {
    type: 'doughnut',
    data: {
        labels: [
            "Orders",
            "Quotes",
            "Invoices"
        ],
        datasets: [
            {
                data: [orders, quotes, invoices],
                borderWidth: [1, 1, 1],
                backgroundColor: [
                    brandPrimary,
                    "rgba(75,192,192,1)",
                    "#FFCE56"
                ],
                hoverBackgroundColor: [
                    brandPrimary,
                    "rgba(75,192,192,1)",
                    "#FFCE56"
                ]
            }]
    }
});

I am pretty sure the solution is fairly straight forward but I am a noob :P 我很确定解决方案是相当简单的,但是我是菜鸟:P

We assume that you are directly using this component in page 我们假设您直接在页面中使用此组件

so what you need to do is just try to use 所以你需要做的就是尝试使用

public function onRun() {
    $this->page['orderCount'] = $this->loadOrders();
}

now in your page you need to make this variable available to the js 现在在您的页面中,您需要将此变量提供给js

<script>
// we don't want to pollute global scope so we add our name space
var myComponent = myComponent || {}
// now we add our value to it 
myComponent['orderCount'] = {{ orderCount }};
</script>

above code will generate, suppose we get counter is 15 then 上面的代码将生成,假设我们得到的计数器为15,则

myComponent['orderCount'] = 15;

now in your javascript you can use this like 现在在您的JavaScript中,您可以像这样使用

var PIECHART = $('#pieChart');
var orders = myComponent['orderCount']; // or myComponent.orderCount
var quotes = ** This is where I am lost **;
var invoices = ** This is where I am lost **;

this works well for number but if you also need to share array to java-script you need to convert it to JSON and then you can use it 这对数字效果很好,但是如果您还需要将数组共享为Java脚本,则需要将其转换为JSON,然后可以使用它

let me know if you need further assistance 让我知道您是否需要进一步的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM