简体   繁体   English

将表中的值拆分为不同的文本输入?

[英]Split values from table into different text inputs?

I have a table that is populated by user input. 我有一个由用户输入填充的表。 For example, there is a text input for First Name, and Last Name. 例如,First Name和Last Name有一个文本输入。 John in one input and Smith in the next, will add the the table under the Name column as John Smith, one string of 2 values. John在一个输入中,而Smith在下一个输入中,将在Name列下添加表作为John Smith,一个包含2个值的字符串。 This is working correctly, along with the Address column... but getting the values FROM the table TO the inputs is the issue. 这是正常工作,以及地址列...但是从表到输入的值是问题。 Clicking the corresponding row populates the inputs, but I need to split these values to populate the correct inputs (so that John Smith is split up again to first and last name for example). 单击相应的行会填充输入,但我需要拆分这些值以填充正确的输入(以便将John Smith再次拆分为名字和姓氏)。 Any ideas? 有任何想法吗? Thanks in advance. 提前致谢。

http://jsfiddle.net/Z85QC/6/ http://jsfiddle.net/Z85QC/6/

jQuery jQuery的

/* Add a click handler to the rows - this could be used as a callback */
$("#example tbody tr").click(function (e) {
    if ($(this).hasClass('rowSelected')) {
        $(this).removeClass('rowSelected');
    } else {
        oTable.$('tr.rowSelected').removeClass('rowSelected');
        $(this).addClass('rowSelected');
    }

    var properties; // all td from .row_selected         

    properties = fnGetSelected(oTable).find("td");
    $("#fName").val(properties.eq(0).text());
    $("#email").val(properties.eq(1).text());
    $("#company").val(properties.eq(2).text());
});

I advise you to wrap your data row elements in span with corresponding class names. 我建议您使用相应的类名将数据行元素包装在span中。 Example given for first name and last name, 给出名字和姓氏的例子,

js JS

$('#addRow').click(function () {
    var row =$('#example').dataTable().fnAddData([
    '<span class="fname">'+$("#fName").val()+'</span>&nbsp;<span class="lname">' + $("#lName").val()+'</span>',
    $("#email").val(),

html of the fiddle 小提琴的html

<td><span class='fname'>John</span>&nbsp;<span class='lname'>Smith</span></td>

Then it is straighforward and clear to retrieve the values independent from their textual format. 然后它是直接和清晰的,以独立于其文本格式检索值。

http://jsfiddle.net/Z85QC/10/ http://jsfiddle.net/Z85QC/10/

In the fiddle you will also find code for associating click function logic to new added rows so that they can be selected. 在小提琴中,您还可以找到用于将点击功能逻辑与新添加的行相关联的代码,以便可以选择它们。

$('#addRow').click(function () {
    var row =$('#example').dataTable().fnAddData([
    '<span class="fname">'+$("#fName").val()+'</span>&nbsp;<span class="lname">' + $("#lName").val()+'</span>',
    $("#email").val(),
    $("#company").val() + '<br>' + $('#address').val()]);
        $(oTable.fnGetNodes(row)).click( function( e ) {

    if ($(this).hasClass('rowSelected')) {
        $(this).removeClass('rowSelected');
    } else {
        oTable.$('tr.rowSelected').removeClass('rowSelected');
        $(this).addClass('rowSelected');
    }

    var properties; // all td from .row_selected         

    properties = fnGetSelected(oTable).find("td");
    $("#fName").val(properties.eq(0).find('.fname').text());

            $("#lName").val(properties.eq(0).find('.lname').text());
    $("#email").val(properties.eq(1).text());
    $("#company").val(properties.eq(2).text());

    });

In order to keep your code DRY it is best to place the click function logic inside a function and call that directly, instead of copying the code. 为了保持代码DRY,最好将click函数逻辑放在函数中并直接调用它,而不是复制代码。

If you are 100% sure that the last name and first name are seperated by a space, you can use this code : 如果您100%确定姓氏和名字是由空格分隔的,则可以使用以下代码:

$("#fName").val(properties.eq(0).text().split(' ')[0]);
$("#lName").val(properties.eq(0).text().split(' ')[1]);

For address : 地址:

$("#company").val(properties.eq(2).html().split('<br>')[0].trim());
$("#address").val(properties.eq(2).html().split('<br>').splice(1).join('\n').trim());

Fiddle : http://jsfiddle.net/Z85QC/11/ 小提琴: http//jsfiddle.net/Z85QC/11/

You can do a simple change like: 你可以做一个简单的改变,如:

var properties; // all td from .row_selected         

properties = fnGetSelected(oTable).find("td");
var names = properties.eq(0).text().split(' ');
$("#fName").val(names[0]);
$("#lName").val(names[1]);
$("#email").val(properties.eq(1).text());
$("#company").val(properties.eq(2).text());

JSFiddle Demo JSFiddle演示

But only if you're sure the First and Last name are separated by a constant single space, otherwise you'd have to change it a little bit more... 但是,只有当你确定First和Last名称由一个恒定的单个空格分隔时,否则你必须更改一点......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM