In laravel 7 /livewire 1.3 / alpinejs 2 project I make crud, when
for adding/editing I include livewire.admin.app-news.form inside of data listing(resources/views/livewire/admin/app-news/container.blade.php) :
in this file adminNewsComponent is defined :
<article class="admin_page_container">
<div class="form-admin-news" x-data="adminNewsComponent()" x-init="initAdminNewsComponent()" >
<div class="">
...
#include('livewire.common.alert_messages')
#if($updateMode=='edit' or $updateMode=='add')
#include('livewire.admin.app-news.form')
#endif
#if($updateMode=='browse')
#if(count($newsDataRows) == 0)
<div class="alert alert-info" role="alert">
No data found
</div>
#endif
#if(count($newsDataRows) > 0)
<div class="table-responsive table-wrapper-for-data-listing" x-data="selectedNewsIdsBoxXData()">
DATA LISTING
</div> <!-- <div class="table-responsive table-wrapper-for-data-listing"> -->
#endif {{-- #if(count($newsDataRows) > 0) --}}
#endif {{-- #if($updateMode=='browse') --}}
</div> <!-- <div class=""> -->
<section class="mt-2">
#if($updateMode=='browse' and count($newsDataRows) > 0)
{{ $newsDataRows->links() }}
#endif
</section>
</div> <!-- <div class=""> -->
</article> <!-- page_content_container -->
#push('styles')
<link href="/css/quill.snow.css" rel="stylesheet">
#endpush
#push('scripts')
<script src="{{ asset('/js/quill.js') }}"></script>
#endpush
#push('scripts')
<script>
function selectedNewsIdsBoxXData() {
console.log('!!!selectedNewsIdsBoxXData::')
...
}
function adminNewsComponent() {
...
} // function adminNewsComponent() {
</script>
#endpush
all methods and data of adminNewsComponent works ok but the problem is that when I open editor
form with line :
#if($updateMode=='edit' or $updateMode=='add')
#include('livewire.admin.app-news.form')
#endif
I can not define similar component inside of resources/views/livewire/admin/app-news/form.blade.php :
<article>
#inject('viewFuncs', 'App\library\viewFuncs')
<form class="form-editor" #if($updateMode=='edit') wire:submit.prevent="update"
#else wire:submit.prevent="store" #endif >
<div class="card" x-data="adminNewsEditorComponent()" x-init="initAdminNewsEditorComponent()" >
<div class="card-body card-block">
...
#section('scripts')
<script>
function adminNewsEditorComponent() {
I got
ReferenceError: adminNewsEditorComponent is not defined.
I suppose the reason is that #include( is used here and actually I would like to different files
and 2 different components defined.
How that coulkd be fixed ?
Thanks!
I've faced a similar issue like this. Let me explain the cause first.
Blade push works for the first render so that when we dynamically load a component with an if condition, the push will not work.
In other words, if the $updateMode = 'edit' or 'add' during the first render, then the push will add the scripts to the layout. If not it will not work with the consecutive renders with livewire.
#if($updateMode=='edit' or $updateMode=='add')
#include('livewire.admin.app-news.form')
#endif
So the only solution is to move the push scripts to the container.blade.php. then this error can be resolved.
Related
What I would like to do is:
When the checkbox is checked ---> change text value to "Yes" and store its value to a component variable
When the checkbox is unchecked ---> change text value to "No" and also store its value to a component variable
Below is the example that I excepted to have but this example uses Javascript.
I would like to implement the same behavior using livewire or/and alpinejs.
function myFunction() {
var checkBox = document.getElementById("toogleA");
var text = document.getElementById("text");
if (checkBox.checked == true) {
document.getElementById("text").innerHTML = "YES";
} else {
document.getElementById("text").innerHTML = "NO";
}
}
/* Toggle A */
input:checked~.dot {
transform: translateX(100%);
background-color: #7456e3;
}
/* Toggle B */
input:checked~.dot {
transform: translateX(100%);
background-color: #7456e3;
}
<link href="https://unpkg.com/tailwindcss#^2/dist/tailwind.min.css" rel="stylesheet" />
<!--Start toggle button-->
<div class="flex items-center justify-center w-auto mb-5">
<!-- label -->
<label for="toogleA" class="flex items-center cursor-pointer">
<!-- toggle -->
<div class="relative">
<!-- input -->
<input wire:model="person" id="toogleA" onclick="myFunction()" type="checkbox" name="test" value="person" class="sr-only" />
<!-- line -->
<div class="w-10 h-4 bg-gray-400 rounded-full shadow-inner"></div>
<!-- dot -->
<div class="absolute w-6 h-6 transition bg-white rounded-full shadow dot -left-1 -top-1"></div>
</div>
<div id="text" class="ml-3 font-medium text-gray-700">NON </div>
</div>
</div>
You can achieve this with just Livewire.
Component
class YourComponent extends Component
{
public bool $toggleA = false;
public function render()
{
return view('livewire.your-component');
}
}
Component view
<label for="toggleA">Toggle</label>
<input wire:click="$toggle('toggleA')" type="checkbox" id="toggleA" />
<p>{{ $toggleA ? 'Yes' : 'No' }}</p>
The $toggle function is a special helper in Livewire specifically for toggling the value of boolean properties.
I am using a Django List View, and I am trying to iterate through multiple objects and display percentage values in a Chart.JS gauge.
However, although I am iterating the names of the gauge id's by using a for loop counter, I am only ever getting the first iteration of the chart.js object rendering on my screen. My initial thoughts are that similar to how I am dynamically creating new canvas ids for the chart.js objects, I should be doing a similar thing for the variable I am trying to pass into the chart object e.g. reduction overall, but I am not having any luck. Your feedback is welcome.
Views.py
class PerformanceDetailView(ListView):
template_name = 'performance_detail.html'
model = Performance
context_object_name = 'performance'
def get_queryset(self, **kwargs):
code = self.kwargs.get('code')
return Performance.objects.filter(code=code)
Performance_detail.html
<section class="projects pt-4 col-lg-12">
{% for item in performance %}
<div class="container-fluid pt-2 col-lg-12">
<!-- Project--><div class="project" >
<div class="row bg-white has-shadow" style="height: 14rem">
<div class="left-col col-lg-2 d-flex align-items-center justify-content-between">
<div class="project-title d-flex align-items-center">
<div class="has-shadow"><img src="{% static 'img/avatar-2.jpg' %}" alt="..." class="img-fluid"></div>
</div>
</div>
<div class="right-col col-lg-2 align-items-center vertical-center">
<div class="text">
<h3 class="h2 pt-2">{{item.brand}} {{item.style}}</h3>
<p class="text-muted">{{item.package_type| capfirst}} package, round {{item.testing_round}}</p>
<p class="text-muted">Item size: {{item.size}}</p>
</div>
</div>
<div class="right-col col-lg-8 align-items-center vertical-center">
<div class="row mt-5">
<div class="col-md-3">
<div class="gauge-container">
<canvas id="gauge1-{{ forloop.counter }}" class="gauge-canvas"></canvas><span id="gauge1Value-{{ forloop.counter }}" class="gauge-label"></span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{{ item.reduction_overall|json_script:"reduction_overall" }}
{{ for_loop_counter|json_script:"for_loop_counter" }}
{% endfor %}
</section>
{% endblock content %}
Block JS within package_detail.html
<!-- Gauge.js-->
<script src="{% static 'vendor/gaugeJS/gauge.min.js' %}"></script>
<script>
$(function () {
var reduction_overall = JSON.parse(document.getElementById('reduction_overall').textContent);
var for_loop_counter = JSON.parse(document.getElementById('for_loop_counter').textContent);
// Gauges
var gauge1 = document.getElementById('gauge1-'+for_loop_counter);
opts.colorStop = "#864DD9";
var gaugeObject1 = new Donut(gauge1).setOptions(opts);
gaugeObject1.maxValue = 100; // set max gauge value
gaugeObject1.setMinValue(0); // set min value
gaugeObject1.set(reduction_overall); // set actual value
gaugeObject1.setTextField(document.getElementById("gauge1Value-" + for_loop_counter));
});
</script>
{% endblock js %}
This was solved by including the JavaScript in the main block content and dynamically naming the gauge elements themselves.
var func_name = "container-{{forloop.counter}}";
func_name = new Donut(gauge1).setOptions(opts);
I posted a full example to a similar problem here Chart JS and Django loop
When you click the "client login" link at the top of this page, the reveal modal should appear. The HTML for it is correct for sure, so a script must be wrong.
In the head of my page, I have these scripts/link:
<!-- jQuery in case needed for Foundation -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Foundation CDN Links -->
<!-- Compressed CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/foundation-sites#6.5.3/dist/css/foundation.min.css" integrity="sha256-GSio8qamaXapM8Fq9JYdGNTvk/dgs+cMLgPeevOYEx0= sha384-wAweiGTn38CY2DSwAaEffed6iMeflc0FMiuptanbN4J+ib+342gKGpvYRWubPd/+ sha512-QHEb6jOC8SaGTmYmGU19u2FhIfeG+t/hSacIWPpDzOp5yygnthL3JwnilM7LM1dOAbJv62R+/FICfsrKUqv4Gg==" crossorigin="anonymous">
<!-- Compressed JavaScript -->
<script src="https://cdn.jsdelivr.net/npm/foundation-sites#6.5.3/dist/js/foundation.min.js" integrity="sha256-mRYlCu5EG+ouD07WxLF8v4ZAZYCA6WrmdIXyn1Bv9Vk= sha384-KzKofw4qqetd3kvuQ5AdapWPqV1ZI+CnfyfEwZQgPk8poOLWaabfgJOfmW7uI+AV sha512-0gHfaMkY+Do568TgjJC2iMAV0dQlY4NqbeZ4pr9lVUTXQzKu8qceyd6wg/3Uql9qA2+3X5NHv3IMb05wb387rA==" crossorigin="anonymous"></script>
The compressed CSS and JavaScript are what Foundation says to use.
I created the project with foundation-cli originally but am unable to deploy, so I copied all the static files/minified CSS into this new GitHub repo and published with GitHub Pages without issue: https://github.com/nataliecardot/zeus-hosting
If I can't simply use CDN links for the reveal modal to work, maybe I could copy the needed files from the original project's repo (created by running foundation new).
Here's the HTML of the reveal modal, although I know it's correct:
<div class="reveal login-modal" id="loginModal" data-reveal>
<h1 class="h3">Client Login</h1>
<form>
<label><input type="text" placeholder="Username"></label>
<label><input type="password" placeholder="Password"></label>
<div class="login-bottom-group">
<button class="button" type="submit">Login</button>
<div class="login-couple">
<span>Remember Me</span>
<div class="switch">
<input class="switch-input" id="rememberSwitch" type="checkbox" name="rememberSwitch">
<label class="switch-paddle" for="rememberSwitch">
<span class="show-for-sr">Remember Me</span>
</label>
</div>
</div>
</div>
<button class="close-button" data-close aria-label="Close modal" type="button">
<span aria-hidden="true">×</span>
</button>
</form>
</div>
Scripts have to be and should always be at the end of <body>, right before </body>.
I am working with Velocity templates, where I need to have multiple pages that needs to follow the same template style, say "about_me", "contact_us" and "home", all these pages will have same header, left menu and only thing different is content.
For example, HTML for all of them would be exactly the same except for one in < div id="page-content" >.
<html>
<head>
.. some common css and js
</head>
<body>
<div id="main-content">
<div id="header">
Main Menu Bar on top
</div>
<div id="left-menu">
Menus on the left
</div>
<div id="page-content">
I could be contact us, or home or about us.
</div>
</div>
</body>
</html>
One of the ways I have done is by having separate .vm file for header and side menu and importing them over on each of aboutus.vm, contactus.vm and home.vm, but with this i have lot of duplicate code. Is there a way to define everything as a template and just have "page-content", defined in my .vm files?
You could parse a velocity file in the middle of your page and define the template name as a variable. That way you could point to a different page-content.vm file for each
In the below example $contentTemplate could equal any of the following:
home/page-content.vm
about/page-content.vm
contact/page-content.vm
See below
<html>
<head>
.. some common css and js
</head>
<body>
<div id="main-content">
<div id="header">
Main Menu Bar on top
</div>
<div id="left-menu">
Menus on the left
</div>
<div id="page-content">
#parse($contentTemplate)
</div>
</div>
</body>
</html>
The below html was provided inside a <cfdocumentitem type="header"> block.
But the output is empty.
<div class="grid">
<div class="span5">
<div class="span5">
Label1
</div>
<div class="span5">
Data1
</div>
</div>
<div class="span5">
<div class="span5">
Label2
</div>
<div class="span5">
Data2
</div>
</div>
<div style="clear:both"></div>
</div>
But when I remove the nested 'class="span5"' divs and put some content there, it is working fine. Is there any problem with nested float in cfdocument???
Unfortunately, CSS support in CFDOCUMENT is kind of hit or miss.
2 rules to follow that might help:
Make sure your HTML validates as XHTML 1.0 Transitional
Import your style sheets using
<style type="text/css" media="screen">#import "style.css";</style>
This same information can be found here: http://rip747.wordpress.com/2007/09/10/cfdocument-it-works-if-you-know-how/