Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Learning JavaScript, jQuery & JSON

My goal is to present a certain total value of everything under a field of a table in a grid based on specific conditions like date range.

here's what I did to capture the date range from two input fields:

<input type="date" id="startdate"/>
<input type="date" id="enddate"/>

var dateArray = '[$('#startdate').val(),$('#enddate').val()]';
var dateJSON = JSON.stringify(dateArray);

Now my problem is my on my first JSON script. It's not presenting what it should on the grid column where it should.

  {
  "Type": "condition",
  "Data": {
    "Type": "And",
    "Expressions": [{
      "Type": "compare",
      "Data": {
        "Type": "GreaterThan",
        "Left": {
          "Type": "field",
          "Data": {
            "Table": "table",
            "Field": "date"
          }
        },
        "Right": {
          "Type": "constant",
          "Data": "dateArray"
        }
      }
    }, {
      "Type": "compare",
      "Data": {
        "Type": "LessThanOrEqual",
        "Left": {
          "Type": "field",
          "Data": {
            "Table": "table",
            "Field": "date"
          }
        },
        "Right": {
          "Type": "constant",
          "Data": "dateArray"
        }
      }
    }]
  }

Anything that could help would be very much appreciated.


1 Answers

Your code is almost right, it just need a little fix. The right syntax for declaring an array is:

var arr = [value1, value2, ...]

$('#run').click(function () {
    var dateArray = [$('#startdate').val(), $('#enddate').val()];
    var dateJSON = JSON.stringify(dateArray);
    alert(dateJSON);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="date" id="startdate" />
<input type="date" id="enddate" />
<button id="run">Run</button>
like image 55
Huy Hoang Pham Avatar answered Jun 22 '26 17:06

Huy Hoang Pham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!