jQuery UI Widgets › Forums › Grid › Deciding the value based on the flag
This topic contains 17 replies, has 2 voices, and was last updated by walker1234 3 years, 3 months ago.
-
Author
-
Please take a look at my JSFiddle. I have a
YearEligible
column withY
andN
values in the cell for each record. Based on this I want to decide what values does the Year column sends upon clickingGet rows
button.Explanation in detail regarding my requirement:
Let’s say If I have checked the very first row ( as shown in the image below):
When I click on the Get rows button, I can see the following in
console.log
which looks good:[{ available: true, boundindex: 0, firstname: "Mayumi", lastname: "Nagase", price: 3.25, productname: "Espresso con Panna", quantity: 6, total: 19.5, uid: 0, uniqueid: "2128-24-28-17-311629", visibleindex: 0, yeareligible: "Y", yearValue: "2011" }]
However, let’s say if I select a record where the
Year Eligible
column has a value ofN
and click on theGet rows
button, I see the following in console.log:[{ available: true, boundindex: 1, firstname: "Regina", lastname: "Davolio", price: 3.3, productname: "Doubleshot Espresso", quantity: 8, total: 26.4, uid: 1, uniqueid: "2917-25-23-18-212828", visibleindex: 1, yeareligible: "N" }]
My observation:
The above console.log is not returning anything for
yearValue
, which makes sense since I didn’t select anything.My Questions:
1. In above scenario, is it possible to return a default value of
yearValue
as -1? Basically, I want to includeyearValue
as -1 when a user selects a record withYear Eligible
value set toN
.2. Display
N/A
on the cell underYear
column for which theYear Eligible
column has a value ofN
?Hi walker1234,
Based on the code example that you have shared there is quick and easy JavaScript solution for your scenario.
This is the modified method from your code example:
$('#jqxbutton').click(function () { var rows = $('#jqxgrid').jqxGrid('getrows'); var selectedRows = rows.filter(x => x.available) for(let i = 0; i < selectedRows.length; i++) { if(selectedRows[i].yeareligible === "N" && selectedRows[i].yearValue === undefined) { selectedRows[i].yearValue = 'N/A'; } else if (selectedRows[i].yeareligible === "N" && selectedRows[i].yearValue !== undefined || null ) { selectedRows[i].yearValue = -1; } } console.log(selectedRows) });
Let me know if that works for you!
Please, do not hesitate to contact us if you have any additional questions.
Best Regards,
Yavor Dashev
jQWidgets team
https://www.jqwidgets.comHi Yavor,
The following code did what I was looking for as far as my question 1 is concerned from my original post. :
for(let i = 0; i < selectedRows.length; i++) { if(selectedRows[i].yeareligible === "N" && selectedRows[i].yearValue === undefined) { selectedRows[i].yearValue = '-1'; } }
As far as my second question is concerned:
Display N/A on the cell under Year column for which the Year Eligible column has a value of N?
I want to display
N/A
on the grid if theYear Eligible
column has a value ofN
. Any idea, how can I achieve this?Hi walker1234,
You can use the
setcellvalue
method in order to set the value in the cell according to certain statement.I have modified the code snippet so that it showcases how to implement the
setcellvalue
method:$('#jqxbutton').click(function () { var rows = $('#jqxgrid').jqxGrid('getrows'); var selectedRows = rows.filter(x => x.available) for(let i = 0; i < selectedRows.length; i++) { if(selectedRows[i].yeareligible === "N" && selectedRows[i].yearValue === undefined) { selectedRows[i].yearValue = 'N/A'; //Using setcellvalue method $("#jqxgrid").jqxGrid('setcellvalue', selectedRows[i].boundindex, "yeareligible", "N/A"); } else if (selectedRows[i].yeareligible === "N" && selectedRows[i].yearValue !== undefined || null ) { selectedRows[i].yearValue = -1; } } });
Let me know if that works for you!
Please, do not hesitate to contact us if you have any additional questions.
Best Regards,
Yavor Dashev
jQWidgets team
https://www.jqwidgets.comHi Yavor,
Thanks. Your approach works fine when I click on the
Get rows
button and I can see it working in this JSFiddle and it shows like this:However, I would like to clarify my requirement. Sorry if I wasn’t clear enough in my earlier posts.
1. I want to display
N/A
when the jqxgrid loads and not when I click onGet rows
button. So, basically, just like it is displaying in the image above, I want to display exactly the same when the grid loads for the first time. I tried moving the logic outside$('#jqxbutton').click(function () {
function but it didn’t work. I am wondering if something needs to happen insidecreateeditor
function?2. For cell values where we have
yearEligible
=N
, can we just disable the jqxDropdownlist completely and displayN/A
as explained in point #1 above as soon as the grid loads/initializes?Hi walker1234,
Thank you for the additional information!
Now I guess I completely understand you use case and I have modified the code so that it may suit your needs according to your requirements.
For your first requirement its best to use cellsrenderer callback property of the column and for the second one we can use initeditor callback to disable the jqxDropDownList according to the year value.
Your modified fiddle from your last post: https://jsfiddle.net/13q6bcud/1/
The modified code:{ text: 'Year',width: 120,datafield:'yearValue' , columntype: 'dropdownlist', editable: true, cellsrenderer: function (row, columnfield, value, defaulthtml, columnproperties) { var yearEligibleValue = $('#jqxgrid').jqxGrid('getcellvalue', row, "yeareligible"); if(yearEligibleValue == "N") { return "N/A"; } else { return '' + value + ''; } }, createeditor: function (row, column, editor) { var list = ['2010', '2011', '2012' ,'2013','2014','2015','2016','2017','2018','2019','2020','2021']; editor.jqxDropDownList({ autoDropDownHeight: true, source: list, selectedIndex: 0 }); }, initeditor: function (row, cellvalue, editor, celltext, pressedChar) { var yearEligibleValue = $('#jqxgrid').jqxGrid('getcellvalue', row, "yeareligible"); if (yearEligibleValue == "N") { editor.jqxDropDownList({selectedIndex: null, disabled: true, placeHolder:"N/A" }); } else { editor.jqxDropDownList({ selectedIndex: 0, disabled: false}); } } }
Let me know if that works for you!
Please, do not hesitate to contact us if you have any additional questions.
Best Regards,
Yavor Dashev
jQWidgets team
https://www.jqwidgets.comHi Yavor,
Excellent. That works. One quick question – Do you think I can force user to select the dates if they haven’t for the cells where we have jqxDropdownlist available?
Like when they click on
Get Rows
button, I want to validate whether they have selected a year from the dropdown list or not and if they haven’t , then I should not allow them to clickGet rows
button, maybe.Thanks
Hi walker1234,
Yes, this functionality can be achieved using the
validation
property of the column.Again, I have modified the jsFiddle so that it may suit your needs: https://jsfiddle.net/wojnxz1p/2/
The added code as well:
validation: function (cell, value) { var yearEligibleValue = $('#jqxgrid').jqxGrid('getcellvalue', cell.row, "yeareligible"); if (yearEligibleValue != "N"){ if (value === null || value.length == 0) { document.getElementById('jqxbutton').disabled =true; return { result: false, message: "You must select a year!" }; } else { document.getElementById('jqxbutton').disabled =false; return true; } } }
Let me know if that works for you!
Please, do not hesitate to contact us if you have any additional questions.
Best Regards,
Yavor Dashev
jQWidgets team
https://www.jqwidgets.comHi Yavor,
I tried putting your validation code in the JSFiddle, and for some reason, it doesn’t seem to be working (button is not hidden and upon clicking the button it’s not doing any validation). Here is the modified JSFiddle with your code: https://jsfiddle.net/rk9085pt/
Please let me know if I am missing anything here?
Thanks,
walker1234Hi walker1234,
I have modified again the js fiddle so that it may suit your needs.
When you click the ‘Get Rows’ button it will validate the cells in the year column and it will display a popup message and after you change the cells value it will remove the popup.
Link to the modified fiddle: https://jsfiddle.net/hme3wnts/
Let me know if that works for you!
Please, do not hesitate to contact us if you have any additional questions.
Best Regards,
Yavor Dashev
jQWidgets team
https://www.jqwidgets.comThanks Yavor,
Quick questions after looking at your JS Fiddle – https://jsfiddle.net/hme3wnts/:
1. I can see that clicking
Get rows
method is showingInvalid Value
if date is not selected. Do you think that it’s possible to show this only for checked rows and not for all other rows withYear Eligible
asY
? I mean right now, it is showingInvalid Value
for those rows as well for which I didn’t select the check box.2. Is it possible to show some different customized message instead of
Invalid Value
? Like – Please select a year for your selected rowThanks,
Walker1234Hi walker1234,
I have modified the code example that I have shared with you the last time so that it may suit your needs.
Modified jsfiddle: https://jsfiddle.net/2zq0ykt9/
Also if you want to change the message of the validation pop up you can change the last parameter of the ‘showvalidationpopup’ method according to your needs like so:
$("#jqxgrid").jqxGrid('showvalidationpopup', rowindex, "datafield", "Another validation message");
Let me know if that works for you!
Please, do not hesitate to contact us if you have any additional questions.
Best Regards,
Yavor Dashev
jQWidgets team
https://www.jqwidgets.comHi Yavor,
Thanks for your inputs.
I have modified JSFiddle to include two date column
Start Year
andEnd Year
. I am wondering if there is any JQXDropdownlist way of making sure that before user hits theGet Rows
button, if it is possible to make sure that theStart Year
is always less thanEnd Year
? For example, I want to display an error message if user selectsStart Year
as2020
andEnd Year
as2012
and doesn’t want user to clickGet Rows
button in that case.My updated JSFiddle is below:
https://jsfiddle.net/walker123/pxb3jLvw/93/
Brief Update on my requirements:
My requirements changed a little bit so I am no longer validating if user doesn’t select anything for the dates column but I am sending default date values if a user check marks a row with year Eligible column asY
. I am sending2015
as default start year and2021
as default end year. Also, in addition to adding two dates colums I have removed the last name column for brevity.Thanks,
walker1234Hi walker1234,
Thank you for the update!
I have created a quick code snippet for you to add so that it populates the cells with the year values according to your needs using again the ‘setcellvalue’ method.
The modified code from your jsfiddle:
$('#jqxbutton').click(function () { var rows = $('#jqxgrid').jqxGrid('getrows'); var selectedRows = rows.filter(x => x.available) for(let i = 0; i < selectedRows.length; i++) { if (selectedRows[i].yeareligible === "N" && selectedRows[i].startyearValue === null && selectedRows[i].endyearValue === null ) { selectedRows[i].startyearValue = -1; selectedRows[i].endyearValue = -1; } else if (selectedRows[i].yeareligible === "Y" && selectedRows[i].startyearValue === null && selectedRows[i].endyearValue === null ) { selectedRows[i].startyearValue = '2015'; selectedRows[i].endyearValue = '2021'; $("#jqxgrid").jqxGrid('setcellvalue', selectedRows[i].boundindex, "startyearValue", "2015"); $("#jqxgrid").jqxGrid('setcellvalue', selectedRows[i].boundindex, "endyearValue", "2021"); } } console.log(selectedRows) });
Let me know if that works for you!
Please, do not hesitate to contact us if you have any additional questions.
Best Regards,
Yavor Dashev
jQWidgets team
https://www.jqwidgets.comHi Yavor,
Thanks, I think there was some confusion while answering my last question. My main question is regarding the dates. Please find my question below:
I am wondering if we can make sure that before user hits the
Get Rows
button, if it is possible to make sure that theStart Year
is always less thanEnd Year
? For example, I want to display an error message if the user selectsStart Year
as2020
andEnd Year
as2012
and doesn’t want user to click Get Rows button in that case.https://jsfiddle.net/walker123/pxb3jLvw/93/
Please take a look.
Thanks,
Walker1234 -
AuthorPosts
You must be logged in to reply to this topic.