0

I have the following JSFiddle https://jsfiddle.net/hmv489jd/ and the console.log(selectedRows); of the following click function prints the object.

$('#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';

                     
                    }

                }
                console.log(selectedRows);
                 
                $('#jqxgrid').jqxGrid('clearselection');
                //var selectedRows = $('#jqxgrid').jqxGrid('getselectedrowindex');
                //checkIfThereAreSelectedRows(selectedRows);

            });

For example, if I select the following items:

enter image description here

I see the following object after hitting the button:

[{
  available: true,
  boundindex: 1,
  endyearValue: "2010",
  firstname: "Elio",
  startyearValue: "2012",
  uid: 1,
  uniqueid: "2822-19-21-27-182626",
  visibleindex: 1,
  yeareligible: "Y"
}, {
  available: true,
  boundindex: 3,
  endyearValue: "2013",
  firstname: "Elio",
  startyearValue: "2011",
  uid: 3,
  uniqueid: "2420-19-28-30-241920",
  visibleindex: 3,
  yeareligible: "Y"
}]

Is it possible to validate using javascript the startyearValue and endyearValue from each of the array and in case startyearValue is greater than endYearValue, show an alert to the user with following message:

alert("Some of your selections have start year greater than end year, please check and make sure start year is always less than end year")

Basically, I don’t want to send this object array to an Ajax call if any of the array contains startyearValue greater than endyearValue. For example, in the above example, the first array contains startyearValue of 2012 and endYearValue of 2010.

3 Answers3

0

Yes, you can totally do that. All you need is to generate a delta value (end year minus start year) for every entry using Array.prototype.map, and then ensure that this delta value always have a positive value. To check if all generated delta values meet a certain requirement. Array.prototype.every is a good candidate for that.

In code speak, it is something like this:

data
    .map(entry => {
        return +entry.endyearValue - +entry.startyearValue;
    })
    .every(delta => {
        return delta > 0;
    });

...which can be easily condensed into a one-liner:

data.map(entry => +entry.endyearValue - +entry.startyearValue).every(d => d > 0);

For example, given the data you have provided in your question:

const exampleData = [{
  available: true,
  boundindex: 1,
  endyearValue: "2010",
  firstname: "Elio",
  startyearValue: "2012",
  uid: 1,
  uniqueid: "2822-19-21-27-182626",
  visibleindex: 1,
  yeareligible: "Y"
}, {
  available: true,
  boundindex: 3,
  endyearValue: "2013",
  firstname: "Elio",
  startyearValue: "2011",
  uid: 3,
  uniqueid: "2420-19-28-30-241920",
  visibleindex: 3,
  yeareligible: "Y"
}];

function areStartAndEndYearsValid(data) {
  return data.map(entry => entry.endyearValue - entry.startyearValue).every(d => d > 0);
}

console.log(areStartAndEndYearsValid(exampleData));
Terry
  • 63,248
  • 15
  • 96
  • 118
0

Something like this

  const rows = $('#jqxgrid').jqxGrid('getrows');

  rows.filter(x => x.available).forEach(row => {
    const eligible = row.yeareligible === "Y"
    const areNull = row.startyearValue === null && row.endyearValue === null
    row.startyearValue = eligible && areNull ?  '2015' : -1;
    row.endyearValue = eligible && areNull ? '2021' : -1;
    if (row.startyearValue  > row.endyearValue) {
      alert("Warning: Start is greater than end year")
    }
  }
mplungjan
  • 169,008
  • 28
  • 173
  • 236
-3

Please go on this link and check answer by selftaught91

on this answer you can add if check => if days is greater than zero which means startdate is greater than enddate with moment.js you can get such functionality and then use loop to check array which field is 'checked'

  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. Also loading Moment for something so trivial is a waste of rresources – mplungjan Oct 06 '21 at 12:45
  • Also, since the start and end years are numeric strings you can just perform subtraction directly without needing to import a library... and moment.js has been placed in maintenance mode for quite awhile now: https://momentjs.com/docs/ – Terry Oct 06 '21 at 13:00