I have some jquery that inputs text into a database and then clears the textarea. However the textarea is not being cleared. Below is the code indicating which line doenst work. When i replace this line with alert(commentbox) I get the value of the comment box so i know that the variable is working. I just don't know how to clear the variable.
Jquery:
<script type='text/javascript'>
$('document').ready(function () {
$('.commentContainer').load('../writecomment.php');
$("form").on("submit", function (e) {
e.preventDefault();
var $form = $(this);
var commentbox = $(this).children('.commentBox').val();
$.ajax({
"url": $form.attr("action"),
"data": $form.serialize(),
"type": $form.attr("method"),
success: function () {
$('.commentContainer').load('../writecomment.php');
commentbox = ""; //this line doesnt work
}
});
});
});
I should also mention that when i replace the bad line with $('.commentBox').val(''); the values clear. the problem is that it clears all the textareas, not just the one that i use .children() to find.
</script>
var commentboxElem = $(this).children('.commentBox');
and in your success:
commentboxElem.val('');
var $form = $(this);
var commentboxObj = $(this).children('.commentBox');
var commentBoxVal=commentboxObj.val();
$.ajax({
"url": $form.attr("action"),
"data": $form.serialize(),
"type": $form.attr("method"),
success: function () {
$('.commentContainer').load('../writecomment.php');
commentboxObj.val(""); //this will work
}
});
with commentbox="", you were trying to update the content of a javascript variable, not the actual textbox.
Related
i try to change the navbar properties on a jqgrid in a callback function without succes.
The grid is display afeter user is chosing a period. Depend on either the period is open or close user can or cannot edit, add, delete rows. So the navbar need to change properties dynamically.
My code look like that:
$('#mygrid').jqGrid({
// some properties of my grid that works fine
pager : '#gridpager'
});
$("#mygrid").bind("jqGridLoadComplete",function(){
$.ajax({
url: 'checkifperiodopen.php',
data: {
$("#period").val()
},
success: function(data){
if(period==='open'){
jQuery("#mygrid").jqGrid('navGrid','#gridpager',{add:false,edit:false,del:true,search:true,refresh:true});
}
if(period==='close'){
jQuery("#mygrid").jqGrid('navGrid','#gridpager',{add:true,edit:true,del:true,search:true,refresh:true});
}
}
});
});
$('#validChossenPeriod').click(function () {
ajax call to get data on choosen period
success:function(data){
$("#mygrid").jqGrid('clearGridData');
$("#mygrid").jqGrid('setGridParam', { datatype: 'local'});
$("#mygrid").jqGrid('setGridParam', { data: data});
$("#mygrid").trigger('reloadGrid');
}
});
I finally found the answer by show or hide the div that include the navgrid button:
grid = $("#mygrid");
gid = $.jgrid.jqID(grid[0].id);
var $tdadd = $('#add_' + gid);
var $tdedit = $('#edit_' + gid);
var $tddel = $('#del_' + gid);
$("#mygrid").jqGrid('navGrid','#gridpager',{add:true,edit:true,del:true,search:true,refresh:true});
condition if false =
$tdadd.hide();
$tdedit.hide();
$tddel.hide();
if true =
$tdadd.show();
$tdedit.show();
$tddel.show();
Why so complex? There is a other clear way to do this
var view_buttons = true;
if(condition_to_hide) {
view_buttons = false;
}
$("#mygrid").jqGrid('navGrid','#gridpager', { add:view_buttons, edit:view_buttons, del:view_buttons, search:true, refresh:true});
I ahve an xml store that calls an url and fetch data from database. It has to be displayed in the data grid when there is data returned from the database. When there is no data, it has to show appropriate error message.
I have the following js file.
require(["dojo/date/locale","dojox/grid/DataGrid","dojox/data/XmlStore","dijit/registry", "dojo/fx","dijit/form/ValidationTextBox", "dojo/dom-form", "dojo/dom", "dojo/on", "dojo/request","dojox/xml/parser", "dojo/ready","dojo/domReady!"],
function(locale,DataGrid,XmlStore,registry,coreFx,dijit, domForm, dom, on, request, parser, ready){
var format;
var siteId;
var phoneNum;
var grid;
var dataGrid=new DataGrid({
autoWidth:true,
autoHeight:true,
clientSort:true,
structure: [
{name:"Order ID", field:"orderId"},
{name:"Sender", field:"senderName"},
{name:"Recipient", field:"recipientName"},
{name:"Phone Number", field:"phone"},
{name:"Gift Amount", field:"amount"}
]
},"showGrid");
dataGrid.startup();
on(dom.byId("submitButton"), "click", function(){
ready(function()
{
submitValue();
});
});
on(dom.byId("printGiftcard"), "click", function(){
ready(function()
{
window.print();
});
});
function submitValue(){
grid=registry.byId("showGrid");
grid.set("store", null);
grid.filter();
document.getElementById("outcomeMessage").innerHTML="";
var orderNumber= document.getElementById("orderNumber");
var num=orderNumber.value;
if(num==""){
document.getElementById("outcomeMessage").innerHTML="Please enter Order number";
return;
}
var myStore= new XmlStore({
url:"/hello/"+num,
urlPreventCache : false,
query:{},
queryOptions:{},
onComplete:sizeCount
});
var sizeCount = function(items,request){
console.log(items.length);
if(items.length == 0){
document.getElementById("outcomeMessage").innerHTML="data not found";
}
}
var request = myStore.fetch({query:{},queryOptions:{},onComplete:sizeCount});
grid=registry.byId("showGrid");
grid.set("store", myStore);
//grid.filter();
}
});
The problem is it is calling database twice now in order to check the number of items returned.Once to set the store for the grid and other to check if the data returned is null.Can someone give me a simplified solution for this.
Thanks
I finally resolved my issue.
dojo.connect(dataGrid,"_onFetchComplete",function(items){
console.log(datagrid.rowCount);
}
})
Thanks!
I am making a dynamic GridContainer. However, an error occurred. console.log output a correct value is outputted. However, the execution error. i don't understand this situation.
my code.
define([ "dojo/parser", "dojo/dom", "dojo/dom-style",
"dijit/registry","dojo/on", "dojo/request", "dojo/dom-construct","dojo/json",
"dojo/_base/array", "dijit/Dialog","dijit/focus" ,"dijit/form/Button","dijit/form/Select","dijit/form/TextBox",
"dijit/form/CheckBox", "dojo/query","dojox/layout/GridContainer","dojox/widget/Portlet","dijit/layout/BorderContainer",
"dijit/layout/TabContainer", "dijit/layout/ContentPane"], function(parser, dom, domStyle,
registry,on, request, domConstruct,json, arrayUtil, Dialog,focus,Button, CheckBox,query,GridContainer,Portlet) {
function addTab(formCount,num){//make gridContainer ajax
var xhrArgs = {
url: '/checkData',
handleAs: "json",
content:{
seq:num
},
load:function(data){
var cont='';
++gridCounter;
cont+='<div dojoType="dojox.layout.GridContainer" class="test" doLayout="true" id="gc'+gridCounter+'" region="center" hasResizableColumns="false" opacity="0.3" nbZones="1" allowAutoScroll="false" withHandles="true" dragHandleClass="dijitTitlePaneTitle" minChildWidth="200" minColWidth="10" style="height:50%;">';
..........
}
}
function addGridContainer(id){
alert('vidgetAddgridNo:'+id);
var result='';
var xhrArgs = {
url: '/checkVidget',
handleAs: "json",
content:{
id:id
},
load: function(data){
++addPorNum;
result+=data;
var portletContent2 = [
domConstruct.create('div', {innerHTML: result})
];
var portlet2 = Portlet({
id: 'dynPortlet'+addPorNum,
closable: false,
title: ''+result,
content: portletContent2
});
makeGrid(portlet2);
}
}//end xhrArgs
var deferred = dojo.xhrGet(xhrArgs);
function makeGrid(por){
console.log('makeGrid in');
var selectedTab=registry.byId('tabContainer').get('selectedChildWidget');
var tabs=registry.byId("tabContainer");
var cPane=tabs.get("selectedChildWidget");
var grid=cPane.getChildren()[0];
var id=grid.id;
alert(registry.byId(id));=>[Widget dojox.layout.GridContainer,gc1]
registry.byId(id).addChild(por,0,0);=>Error:Cannot set property 'id' of undefined
}//end makeGrid()
}
plz help me..T.T
In your define() module list you defined the following modules:
"dijit/form/Button", "dijit/form/Select", "dijit/form/TextBox",
"dijit/form/CheckBox"
However, in your callback you only have:
Button, CheckBox
So, you're missing the Select and TextBox module here, meaning that the parameter CheckBox actually contains the module dijit/form/Select. Which means every module is shifted and none of them contain the actual value, try fixing that first.
I need to pass textbox input value and dropdownlist selected value to Controller in MVC4 ????????? For Eg : I have a Form with Textbox and Dropdownlist and a ( PartialView For Webgrid) . When i give datetime input to Textbox and Selected the "DoctorName" in Dropdown . Then i need to pass textbox input value and dropdownlist values as parameters to the controller ??????? My Controller is used to bind the webgrid in PartialView... The Code i tried which is not working......#doctortype is dropdownlist,#AppointmentDate is Textbox input datetime.
jquery:
<script type="text/javascript">
$(document).ready(function () {
$('#doctorType').change(function (e) {
e.preventDefault();
var url = '#Url.Action("Filter")';
$.get(url, { doctorname: $(this).val() }, { AppointmentDate: $('#AppointmentDate').val() }, function (result) {
$('#FilterWebgrid').html(result);
});
});
});
</script>
is not
$.get(url, `{ doctorname: $(this).val() }, { AppointmentDate: $('#AppointmentDate').val() }`, function (result) {
$('#FilterWebgrid').html(result);
});
You are sending two object. You have to send a single object like this:
$.get(url, { doctorname: $(this).val(), AppointmentDate: $('#AppointmentDate').val() }, function (result) {
$('#FilterWebgrid').html(result);
});
2) check the console and network tab and check if there is any error.
3) The value you are passing must be the same type as the parameter expected in the Action
This is just simple method to post data. If your controller returns data back to view, try $.getJSon();
<input type="text" value="doctor name" id="txtDoctor"/>
<select id="ddlDoctor"><option>SomeSelectedData</option></select>
<script>
$(document).ready(function() {
var txtValue = $('#txtDoctor').val();
var ddlValue = $('#ddlDoctor').val();
$.ajax({
url: '/controller/action',
data: { doc: txtValue, name: ddlValue },
traditional: true,
success: function(result) {
alert(result.status);
}
});
});
</script>
to know more about getJSon and JSON Result, check these links.
GetJson with parameters
JSON result in view
I have built a Dojo Widget for creating a list by entering values. the widget code is:
define(["dojo/_base/declare", "dijit/_WidgetBase", "dijit/_TemplatedMixin", 'dojo/text!apps/orders/templates/multiAddList.html', "dojo/dom", "dojo/on", "dojo/dom-construct", "dojo/dom-class", "dojo/query", "dijit/focus"],
function (declare, WidgetBase, TemplatedMixin, html, dom, on, domConstruct, domClass, query, focusUtil) {
return declare([WidgetBase, TemplatedMixin], {
templateString: html,
postCreate: function () {
this.inherited(arguments);
var that = this;
},
_checkIfEnter: function (e) {
if (e.which == 13) {
this._addUser();
}
},
_addUser: function () {
domClass.remove(this.ulAdded, "hidden");
var textToAdd = this.userTextToAdd.value;
var li = domConstruct.create("li", {}, this.ulAdded);
domConstruct.create("span", {innerHTML: textToAdd}, li);
var spanX = domConstruct.create("span", {class: 'icon-x right'}, li);
this.itemsArray.push(textToAdd);
this.userTextToAdd.value = "";
focusUtil.focus(this.userTextToAdd);
var that = this;
on(spanX, "click", function () {
domConstruct.destroy(li);
that.itemsArray.splice(that.itemsArray.indexOf(textToAdd), 1);
if (that.itemsArray.length == 0) {
domClass.add(that.ulAdded, "hidden");
}
});
},
itemsArray: []
});
});
It is all OK. However - when I instantiate it twice on same dialog like this:
allowedDomains = new MultiAddList();
allowedDomains.placeAt(dom.byId('allowedDomains'), 0);
pdlEmails = new MultiAddList();
pdlEmails.placeAt(dom.byId('pdlEmails'), 0);
and then asking for allowedDomains.itemsArray() or pdlEmails.itemsArray() - I get the same list (as if it is the same instance) - althought in the UI presentation - he adds the list items separately and correctly.
Obviously, I am doing something wrong although I followed Dojo examples.
Does anyone know what I should do in order to make it work?
Thanks
When you make a dojo class using declare, object and array members are static, meaning they are shared across instances, so I would suggest doing itemsArray: null and then this.itemsArray = [] in the constructor or postCreate somewhere.
Everything else looks fine, although I too would have a preference for using hitch, your solution is perfectly fine.
Sorry for just giving you a hint, but you might want to look at the dojo.hicth()-function, as an alternative to the "this-that" contruction
on(spanX, "click", dojo.hitch(this, function () {
domConstruct.destroy(li);
this.itemsArray.splice(this.itemsArray.indexOf(textToAdd), 1);
if (this.itemsArray.length == 0) {
domClass.add(this.ulAdded, "hidden");
}
}));
The on-construct is a good one, but just testing this kind of construct might tell you whether that is the problem or not.
_addUser: function () {
.....
.....
dojo.connect(spanX, "click", this, this.spanClicked);
or
dojo.connect(spanX, "click", dojo.hitch(this, this.spanClicked);
},
spanClicked: function(args) {
domConstruct.destroy(li); //need to keep some reference to li
this.itemsArray.splice(this.itemsArray.indexOf(textToAdd), 1);
if (that.itemsArray.length == 0) {
domClass.add(this.ulAdded, "hidden");
}
}