JQueryUIHelpers for MVC5 - DatePicker not rendering in Partial View - jquery-ui

I have a current page which uses JqueryUIHelpers to render JQuery UI DatePicker.
I've followed the tips on:
JqueryUIHelpers - Getting Started
Everything was working fine until I started moving the codes to a partial view.
The datepicker is no longer rendering on click.
I've read around that the same issue happens with normal JQueryUI usage, and I should move the JavaScript that renders the datepicker to the base view (something like $('#date').datepicker()).
However, the JQueryUIHelpers code is like:
#Html.JQueryUI().Datepicker("date")
Can I know if there's anyway to get it working?

Thanks to Kartikeya's suggestion, managed to get the datepicker working.
First I have the DatePicker defined:
#Html.JQueryUI().Datepicker("date")
and in the partial view's document.ready() function, I re-initialized the DatePicker once more:
$('#date').datepicker();
and now everything's working fine.
edit:
thanks to Kartikeya's suggestion again. Including the JQueryUI file in the partial view will eliminate the need to re-initialize the DatePicker once more in the document.ready() function block.
What I did was to include the line:
#Scripts.Render("~/bundles/jqueryui")
in my partial view and then everything clicks.
By the way, now I know "what to do to get it work", can I know "why does it work this way"?

Well i m explaining your problem in answer section because there is no proper space in comments section.
Yes many people think that this might be a bug in partial view in asp.net mvc but it is not so if we write #Html.JQueryUI().Datepicker("date") then somewhere internally the datepicker initialize same way as :
$(document).ready(function(){
$('#date').datepicker();
});
even if we are not required to write above jquery code but datepicker initialize this way only,so this is the case if we use datepicker in views but if we use datepicker in partial view then scenario changes because partial views are generally used for dynamic content for ex :- if we want to change some html dynamicaly then we use partial view and the dynamic html returned by partial view is displayed inside view,so here the problem arises because for partial view no document.ready() event fire to initialise the datepicker so this problem occurs and my two comments and your two answers can solve it.In above lines i m again and again writing dynamic html because this problem is because of this dynamic html only in order to overcome this we can also use event delegation i.e .on() in jquery or intialize datepicker as(give all datepicker textboxes a class say 'textbox') :
$('body').on('focus',".textbox", function(){
$(this).datepicker();
});​
Thanks..!!

Related

jQuery UI resizable/selectable not working in Backbone View

I have a view for my model, and in the initialization I use the following code
initialize: function (){
_.bindAll(this, 'render');
this.listenTo(this.model, "change", this.render);
this.$el.draggable({
opacity: 0.5,
containment: "parent"
});
this.$el.resizable();
this.$el.selectable();
},
Although draggable works, resizable and selectable do not (I haven't tested other jQuery UI interactions to check if they work). I tried placing this.$el.resizable inside the render function, but that wouldn't work either. I'm using jQuery 1.8.3 and jQuery UI 1.9.2 (I do include the necessary jquery-ui.css in my html). When the view renders, I inspect the element in my browser and it does indeed have a class of ui.draggable, ui.resizable and ui.selectable as it is supposed to, yet it's only draggable.
Trying to work around this issue, I've used CSS3 property resize: both in the class of the element I want to resize and it works just fine, but I'd really like to utilize jQuery UI for resizing, and I'm curious why it won't work.
Any ideas what I might be doing wrong?
EDIT: Added jsfiddle example http://jsfiddle.net/IgnorantUser/u7JkX/ the problem described is in the initialization of the ButtonInCanvas backbone view
ok so, apparently using this.$el.html(this.template(this.model.toJSON())); in my render function on the ButtonInCanvas view was causing the problems. The aforementioned gets the "caption" attribute of the model, and passes it into the view as html content.
There solution is to put this.$el.html(this.template(this.model.toJSON())); in the init function.
In case it's needed to be done in the render function, there should be another div, within the one we want to resize, that will get the caption property from the model. In the following JSBIN you can see a solution using a div inside the button. (thanks to user shoky, from the #jquery freenode irc channel for the implementation and his heads up)
http://jsbin.com/uxequq/1/edit
I don't really understand why the problem was occurring though (maybe this.template(this.model.toJSON()) in my render function, was causing a re-rendering of the view?) so feel free to point me out why, and help me understand further!
Cheers!

jquerymobile button's css not applied

I'm trying to add a delete button to a custom div. However when the page is loaded, the jquery mobile button does not take the format of jquery and displays it like a hyperlink.
var currDelButton = $("<a>").attr("href","#").attr("data-role","button").attr("data-icon","delete").attr("data-iconpos","left").text("حذف");
anyone has an idea about this issue?
Best Regards
Ali
If you are adding the button after the page is loaded then you need to refresh the element for example $("#mybutton").button(); should work.
here is a working example with the code you provided: http://jsfiddle.net/ashanova/RQVd8/1/
call the .page for the main wrapper where new buttons are added.
$("#content").page();

jQuery Tabs - Adding reusable next/previous buttons

Im looking to extend the jquery tabs to add next and previous buttons - which i have done using the show event. The problem is that its a big chunk of code added to that event for each page that needs tabs and these buttons. How i go about making it a reusable component?
David
Write a jquery plugin to wire your tabs up by convention. Here's a tutorial: http://docs.jquery.com/Plugins/Authoring
Decorate your tab list with something like , then have your plugin pick up on ul's with class "scrollable". Write all your logic in there. You shouldn't need to call any function explicitly for each page.

Way to override where jQuery-UI dialog places things in markup?

I am trying to get a simple jQuery UI dialog to work in an ASP.Net project. I have some buttons inside the dialog's <div>, but they weren't posting back. Upon closer inspection, for whatever reason when making the <div> a panel it moves it in the DOM so that it is the last tag before </body>. This would all be fine and dandy except for it also moves it outside of the <form> tag which is needed so that the buttons can do a postback.
What is the purpose of this behavior and how can I override it?
An example is here: http://jsbin.com/olumu4/
Looking at Firebug though I get this:
alt text http://img80.imageshack.us/img80/9049/dialog.png
This is a common problem with jQuery/ASP.NET.
After you assign your modal like this
$(".modal-window").dialog({
modal: true
});
do this
$(".modal-window").parent().appendTo($("form:first"));
If you have more than one modal on a page, I've found it doesn't work as expected, so in that case when you call the open method on the modal, do this:
$('.modal-window').dialog('open').parent().appendTo($('form:first'));
If you want to have it auto-open. You can append it right after the model is assigned.
$(".modal-window").dialog({
modal: true
}).parent().appendTo($('form:first'));
Hope this helps,
Marko
I have had this problem before, an option is to setup an even on the $(form).submit that appends what is in the dialog to the form.

binding fields in a dialog popup in my view

i have an html table in my asp.net mvc view an i am running into some real estate issues with screen space.
I think in one area of my view i am going to have a button in a column of an html table that says "Details" which, when clicked, loads up some dialog ui. what i am trying to get my head around is that i want the fields in the dialog to also be part of data binding object in the overall form which i am passing to the controller when i submit the form.
is there anything that i should be worried about or anything that you need to do special if you have a form where inside your form you have a button that create a popup with some more details elements. I am just trying to see from a data binding view if there are any issues.
also any examples of doing anything similar would be great.
EDIT
So i tried doing this an ran into a binding issue. i have a follow up question with the specifics about this binding issue with jquery ui dialog here:
why does jquery ui dialog break asp.net mvc's default model binding .
There shouldn't be any issues if you are binding elements from your popup dialog to corresponding hidden elements in your main view. These hidden elements will bind correctly like any other control in your main form.
Of course, you might be POSTing the form elements from your popup form to its own controller method directly, and that is also a perfectly good approach.

Resources