Handsontable在发布之前用密钥替换自动完成值

Cho*_*o87 13 javascript jquery json handsontable

我正在使用HandsOnTable在我的网站上使编辑数据库表更具交互性.

HandsOnTable几乎满足了我的所有要求,只是我的数据库中的某些列实际存储了外键而不是本地字符串值.

在UI中,我希望这些列显示为下拉菜单,其中用户选择映射到前面提到的外键的可读值(即类似HTML名称/值select).

不幸的是,HandsOnTable没有这样的细胞类型.最接近它的是autocomplete.这允许我创建一个下拉列表,但它只包含值; 没有相应的钥匙.以下是它的创建方式:

"source": ["Jebediah", "Bob", "Bill", "Buzz"]
Run Code Online (Sandbox Code Playgroud)

所以我计划从服务器发送两个Json字符串:

一个包含HandsOnTable渲染表所需的参数:

{
    "data": [
        { "ID": 1, "Description": "Crude", "Volume": 204, "Customer": "jebediah" },
        { "ID": 2, "Description": "Hidrogen", "Volume": 513, "Customer": "Bob" },
        { "ID": 3, "Description": "Coal", "Volume": '67', "Customer": "Bill" },
        { "ID": 4, "Description": "Wood", "Volume": '513',  "Customer": "Buzz" }
    ],
    "columns": [
        { "data": "ID", "type": "numeric" },
        { "data": "Description", "type": "text"},
        { "data: "Volume", "type": "numeric" },
        { "data": "color", "type": "autocomplete", "strict": "true",
            "source": ["Jebediah", "Bob", "Bill", "Buzz"]}
    ]
}
Run Code Online (Sandbox Code Playgroud)

第二个映射键到值

{
    "mappings": [
        {"key": 0, "value": "Jebediah"}, 
        {"key": 0, "value": "Bob"},
        {"key": 0, "value": "Bill"}, 
        {"key": 0, "value": "Buzz"}
    ]
}
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.现在是棘手的部分:

HandsOnTable有一个function(getData()),它允许我将表数据检索为准备发送回服务器的Json字符串:

var jdata = myHandsOnTable.getData();
Run Code Online (Sandbox Code Playgroud)

其中jdata看起来像这样:

"data": [
    { "ID": 1, "Description": "Crude", "Volume": 204, "Customer": "jebediah" },
    { "ID": 2, "Description": "Hidrogen", "Volume": 513, "Customer": "Bob" },
    { "ID": 3, "Description": "Coal", "Volume": '67', "Customer": "Bill" },
    { "ID": 4, "Description": "Wood", "Volume": '513',  "Customer": "Buzz" }
]
Run Code Online (Sandbox Code Playgroud)

在发布之前,我想Customermappingsjson字符串中匹配的对键替换节点的值.

我怎样才能在JavaScript/JQuery中实现这一目标?

是否有一个如下工作的功能?:

jdata.replaceNode('node', mappings)
Run Code Online (Sandbox Code Playgroud)

谢谢

Pos*_*ing 8

我有类似的问题,这就是我做的......

对于每个外键列,我在handsontable中存储了2个值; 一个用于id本身,我将其设置为隐藏列,另一个是用户友好的可读文本值作为下拉列表.

每次更改下拉列表的值时,我也会更改相应的隐藏ID.在我的情况下,我在handontable外面有一个下拉列表作为过滤器,我用它来映射键/值对,但你可以使用Hashtables或其他任何东西.

现在代码......

Handsontable配置:

afterChange: function (changes, source) { AfterChange(changes, source); }
Run Code Online (Sandbox Code Playgroud)

更改事件后(每次在表中发生更改时调用):

function AfterChange(Changes, Source) {

    if (Source === 'loadData') {
        return; //don't save this change
    }
    var rowIndex = 0, columnID = 1, oldTextVal = 2, newTextVal = 3, ntv = '', nv = '';
    $(Changes).each(function () {
        if (this[columnID] === 'CategoryID') {
            // Do nothing...
            //To make sure nothing else happens when this column is set through below
        }
        else if (this[columnID] === 'CategoryName') {
            ntv = this[newTextVal];
            //This is where I do my mapping using a dropdown.
            nv = $('#CategoriesFilterDropdown option').filter(function () { return $(this).text() === ntv; }).val();
            //13 is my CategoryID column
            $container.handsontable('setDataAtCell', this[rowIndex], 13, nv);
        }
    });
    }
}
Run Code Online (Sandbox Code Playgroud)

这样,您可以像保存一样更改外键,而无需在保存之前循环显示所有外键.它还可以轻松地将表数据发送回服务器.

总结,

  • 用户与CategoryName列(属于类型autocomplete)进行交互.
  • CatgoryID柱是通过使用列的宽度设置为0对用户隐藏colWidthshandsontable的选项.
  • CategoryName字段更改时,使用afterChangeevent设置相应的CategoryID列.在我的例子中,我使用页面上其他位置的下拉列表来映射Name => ID,但是您可以使用其他方法,例如哈希表.

我希望它有意义......