在使用IndexedDB时如何使用JSStore设置dynamic where子句?

时间:2018-03-09 15:05:22

标签: javascript indexeddb

我正在使用JsStore包装器处理IndexedDB。当我在Where块中直接设置列名时,代码中的一切正常,但如果我将其设置为动态,如下面的代码所示,则代码不起作用。

var searchValueType = $('input:radio[name=searchValueType]:checked').val();
var searchValue = $("#searchValue").val();                
var column1 = 'Name';
var whereClause = column1+':'+searchValue;
alert ("where clause >> "+whereClause);
DbConnection.select({
    From: "Student",    
     Where: {
        whereClause
      },
    }, 
    function (students) {
    var HtmlString = "";
    students.forEach(function (student) {
        HtmlString += "<tr ItemId=" + student.Id + "><td>" +
            student.Name + "</td><td>" +
            student.Gender + "</td><td>" +
            student.Country + "</td><td>" +
            student.City + "</td><td>" +
            "<a href='#' class='edit'>Edit</a></td>" +
            "<td><a href='#' class='delete''>Delete</a></td>";
    }, function (error) {
        console.log(error);
    })
    $('#tblGrid tbody').html(HtmlString);
    });

1 个答案:

答案 0 :(得分:0)

问题在代码中清晰可见,其中需要一个对象,但是你在大括号内传递一个字符串。

您需要在变量中创建对象并将其分配给jsstore的Where选项。

请检查以下代码 -

var searchValueType = $('input:radio[name=searchValueType]:checked').val();
var searchValue = $("#searchValue").val();
//var column1 = 'Name';
//var whereClause = column1+':'+searchValue;

//alert ("where clause >> "+whereClause);

// Where clause is now an object
var whereClause = {
    Name: searchValue
}
DbConnection.select({
    From: "Student",
    Where: whereClause,
}, function(students) {
    var HtmlString = "";
    students.forEach(function(student) {
        HtmlString += "<tr ItemId=" + student.Id + "><td>" +
            student.Name + "</td><td>" +
            student.Gender + "</td><td>" +
            student.Country + "</td><td>" +
            student.City + "</td><td>" +
            "<a href='#' class='edit'>Edit</a></td>" +
            "<td><a href='#' class='delete''>Delete</a></td>";
    }, function(error) {
        console.log(error);
    })
    $('#tblGrid tbody').html(HtmlString);
});